Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 6ce551f

Browse files
new problems
1 parent 055a51d commit 6ce551f

File tree

5 files changed

+311
-2
lines changed

5 files changed

+311
-2
lines changed

‎src/revision/Assessment.java

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
package revision;
2+
3+
/*
4+
* // Q1. Approach : divided array into 3 slots(0|1|2) and using index pointer mid,low,high moved
5+
* 0,1,2 to // the respective slot by swapping. first took mid and low pointer in the starting for
6+
* 0,1 and high // for 2. Then check if current element is 1 then just increment mid pointer as 1
7+
* lies in mid and if // element is 0 then swapped mid and low to move 0 in the beginning. similarly
8+
* for 2, did swap of // mid and high(pointing to last index of array) to push 2 in the end.
9+
*
10+
* ***
11+
*
12+
* Q2. Approach: to print in the wave form. As there can be many possible solutions. here i
13+
* traversed on even indexed elements and checked for prev and next element if it is greater than
14+
* current even indexed element or not if it is then just swap the current element with that prev
15+
* and next element. only single traversal is needed. we can also traverse on odd indexed element
16+
* and perform the same operation to sort elements in wave like form.
17+
*
18+
*/
19+
20+
21+
22+
public class Assessment {
23+
24+
public static void helper(int[] input) { // Q1. Time complexity: O(length of input array)
25+
26+
int mid = 0, low = 0, high = input.length - 1;
27+
28+
while (mid <= high) {
29+
30+
if (input[mid] == 1) {
31+
mid++;
32+
33+
} else if (input[mid] == 0) {
34+
int temp = input[low];
35+
input[low] = input[mid];
36+
input[mid] = temp;
37+
low++;
38+
mid++;
39+
} else if (input[mid] == 2) {
40+
int temp = input[high];
41+
input[high] = input[mid];
42+
input[mid] = temp;
43+
mid++;
44+
high--;
45+
}
46+
47+
}
48+
49+
}
50+
51+
public static void swapHelper(int first, int second, int input[]) {
52+
53+
int temp = input[first];
54+
input[first] = input[second];
55+
input[second] = temp;
56+
57+
58+
59+
}
60+
61+
62+
public static void helper2(int[] input) { // Q2. Time complexity: O(length of input array)
63+
64+
int n = input.length - 1;
65+
for (int i = 0; i <= n; i += 2) {
66+
67+
if (i < n && input[i] < input[i + 1]) {
68+
69+
swapHelper(i, i + 1, input);
70+
}
71+
72+
if (i > 0 && input[i] < input[i - 1]) {
73+
swapHelper(i, i - 1, input);
74+
}
75+
76+
77+
}
78+
79+
80+
81+
}
82+
83+
84+
public static int[] sortArray(int[] input) {
85+
86+
// helper(input); //Q1.
87+
helper2(input); // Q2.
88+
return input;
89+
90+
91+
}
92+
93+
public static void main(String[] args) {
94+
95+
int arr[] = {20, 10, 8, 6, 4, 2};
96+
// 20, 10, 8, 6, 4, 2
97+
// int input[] = {0, 1, 0, 2, 0, 1, 2, 0};
98+
int res[] = sortArray(arr);
99+
for (int i = 0; i < res.length; i++) {
100+
System.out.print(res[i] + " ");
101+
}
102+
103+
}
104+
105+
}

‎src/revision/Test.java

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package revision;
2+
3+
import java.util.ArrayList;
4+
import java.util.Collection;
5+
import java.util.Collections;
6+
import java.util.HashMap;
7+
import java.util.HashSet;
8+
import java.util.List;
9+
import java.util.Map;
10+
import java.util.Scanner;
11+
12+
public class Test {
13+
14+
15+
16+
// • dishes = [["Salad", "Tomato", "Cucumber", "Salad", "Sauce"],
17+
// • ["Pizza", "Tomato", "Sausage", "Sauce", "Dough"],
18+
// • ["Quesadilla", "Chicken", "Cheese", "Sauce"],
19+
// • ["Sandwich", "Salad", "Bread", "Tomato", "Cheese"]]
20+
21+
22+
23+
public static int helper(int arr[],int x,int l,int h){
24+
25+
//out of bound
26+
if(l>h){
27+
28+
return 0;
29+
}
30+
31+
//not possible
32+
if(x<0){
33+
34+
return -1;
35+
}
36+
37+
if (x == 0) {
38+
39+
return 1;
40+
}
41+
42+
int waysleft = 0;
43+
int waysright=0;
44+
if(arr[l]<=x){
45+
46+
waysleft+= helper(arr,x-arr[l],l+1,h); //o
47+
}
48+
49+
else if(arr[h]<=x){
50+
51+
waysright+=helper(arr,x-arr[h],l,h-1); //1
52+
}
53+
54+
55+
System.out.println("left:"+waysleft+"righ:"+waysright);
56+
return Math.min(waysleft,waysright);
57+
58+
}
59+
60+
61+
public static int ways(int arr[], int x) {
62+
63+
64+
return helper(arr, x, 0, arr.length - 1);
65+
66+
}
67+
68+
69+
70+
public static void main(String[] args) {
71+
72+
73+
74+
75+
int arr[] = {1, 1, 4, 2, 3};
76+
77+
System.out.println(ways(arr,5));
78+
79+
80+
81+
}
82+
83+
84+
85+
}
86+
87+

‎src/revision/pp.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package revision;
2+
3+
import java.util.Arrays;
4+
import java.util.Scanner;
5+
6+
public class pp {
7+
8+
9+
10+
public static void main(String[] args) {
11+
12+
System.out.println("hello world");
13+
14+
}
15+
16+
}

‎src/stacks/maxRectangleArea.java

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
package stacks;
2+
3+
import java.util.Stack;
4+
5+
/**
6+
* maxRectangleArea
7+
*/
8+
public class maxRectangleArea {
9+
10+
public static int maxhist(int arr[]) {
11+
12+
Stack<Integer> stack = new Stack<>();
13+
14+
15+
int mx = 0;
16+
int i = 0;
17+
for (; i < arr.length;) {
18+
19+
if (stack.isEmpty() || arr[i] >= arr[stack.peek()]) {
20+
stack.push(i++);
21+
22+
23+
} else {
24+
int topvalue = arr[stack.peek()];
25+
stack.pop();
26+
if (!stack.isEmpty()) {
27+
28+
29+
mx = Math.max(mx, (topvalue * (i - stack.peek() - 1)));
30+
} else {
31+
32+
33+
mx = Math.max(mx, (topvalue * i));
34+
}
35+
36+
37+
}
38+
39+
}
40+
41+
while (!stack.isEmpty()) {
42+
43+
int topvalue = arr[stack.peek()];
44+
stack.pop();
45+
if (!stack.isEmpty()) {
46+
47+
48+
mx = Math.max(mx, (topvalue * (i - stack.peek() - 1)));
49+
} else {
50+
51+
52+
mx = Math.max(mx, (topvalue * i));
53+
}
54+
55+
56+
57+
}
58+
59+
60+
return mx;
61+
62+
63+
64+
}
65+
66+
67+
68+
public static void main(String[] args) {
69+
70+
71+
int mat[][]=new int[10][10];
72+
int mx = 0;
73+
74+
for (int i = 1; i < n; i++) {
75+
76+
for (int j = 0; j < m; j++) {
77+
78+
if (mat[i][j] == 1) {
79+
mat[i][j] = mat[i][j] + mat[i - 1][j];
80+
}
81+
}
82+
83+
mx = Math.max(mx, maxhist(mat[i]));
84+
85+
}
86+
87+
if (m != 0)
88+
mx = Math.max(mx, maxhist(mat[0]));
89+
90+
91+
System.out.print(mx);
92+
93+
94+
95+
}
96+
97+
98+
99+
}

‎src/test/Check.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ public class Check {
55
public static void main(String[] args) {
66

77
String arr[] = {"JSN|Fuse", "JSN|TransID101", "JSN|TransID102", "RECON_RESULT"};
8-
int count=0;
8+
int count = 0;
99
for (String obj : arr) {
1010
if (obj.equals("JSN|Fuse")) {
1111

@@ -15,9 +15,11 @@ public static void main(String[] args) {
1515
}
1616
}
1717

18-
System.out.println("count:"+count);
18+
System.out.println("count:" + count);
1919

2020
}
2121

22+
23+
2224

2325
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /