Skip to content

Commit 285377a

Browse files
committed
practice
1 parent b6d4847 commit 285377a

20 files changed

+1296
-0
lines changed

src/Prep/AllPossibleSubstring.java

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package Prep;
2+
3+
import java.util.Scanner;
4+
5+
public class AllPossibleSubstring {
6+
7+
8+
public static void helper(String str, int i, String out) {
9+
10+
11+
if (i == str.length()) {
12+
13+
System.out.println(out);
14+
return;
15+
}
16+
17+
for (int j = str.length() - 1; j >= i; j--) {
18+
19+
String temp = '[' + str.substring(i, j + 1) + ']';
20+
21+
helper(str, j + 1, out + temp);
22+
}
23+
24+
25+
26+
}
27+
28+
29+
public static void main(String[] args) {
30+
31+
32+
Scanner sc = new Scanner(System.in);
33+
34+
String input = sc.nextLine();
35+
int n = input.length();
36+
37+
System.out.println((int)Math.pow(2, n - 1));
38+
39+
helper(input, 0, "");
40+
41+
42+
43+
}
44+
45+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package Prep;
2+
3+
import java.util.Arrays;
4+
import java.util.Scanner;
5+
6+
public class DistributeSweetsHappy {
7+
8+
9+
10+
public static void main(String[] args) {
11+
12+
13+
Scanner sc = new Scanner(System.in);
14+
15+
int n = sc.nextInt();
16+
17+
int min[] = new int[n];
18+
int max[] = new int[n];
19+
20+
for (int i = 0; i < n; i++) {
21+
22+
min[i] = sc.nextInt();
23+
max[i] = sc.nextInt();
24+
25+
26+
}
27+
28+
29+
Arrays.sort(min);
30+
Arrays.sort(max);
31+
32+
int curr = 0, res = 0;
33+
int sweets = 0;
34+
int i = 0, j = 0;
35+
36+
while (i < n && j < n) {
37+
38+
39+
if (min[i] <= max[j]) {
40+
41+
curr += 1;
42+
if (curr > res) {
43+
res = curr;
44+
sweets = min[i];
45+
}
46+
47+
i++;
48+
49+
} else {
50+
51+
curr -= 1;
52+
j++;
53+
54+
}
55+
56+
57+
58+
}
59+
60+
61+
System.out.println(sweets + " " + res);
62+
63+
64+
65+
}
66+
67+
}

src/Prep/FloorCeil.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package Prep;
2+
3+
public class FloorCeil {
4+
5+
6+
// binary search approach
7+
8+
// other
9+
public static void helper(int arr[], int x) {
10+
11+
12+
int fInd = -1, cInd = -1;
13+
int n = arr.length;
14+
15+
int fDist = Integer.MAX_VALUE, cDist = Integer.MAX_VALUE;
16+
17+
for (int i = 0; i < n; i++) {
18+
19+
if (arr[i] >= x && cDist > (arr[i] - x)) {
20+
cInd = i;
21+
cDist = arr[i] - x;
22+
}
23+
24+
25+
if (arr[i] <= x && fDist > (x - arr[i])) {
26+
fInd = i;
27+
fDist = x - arr[i];
28+
}
29+
}
30+
31+
System.out.println("floor" + arr[fInd] + " ceil" + arr[cInd]);
32+
}
33+
34+
public static void main(String[] args) {
35+
36+
37+
38+
}
39+
40+
}

src/Prep/GoodBadString.java

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package Prep;
2+
3+
import java.util.Scanner;
4+
5+
public class GoodBadString {
6+
7+
8+
public static String helper(String str) {
9+
10+
String ans = "";
11+
for (int i = 0; i < str.length(); i++)
12+
{
13+
14+
int j = i;
15+
16+
while (j < str.length() && str.charAt(i) == str.charAt(j))
17+
{
18+
j++;
19+
}
20+
21+
ans += str.charAt(i);
22+
i=j-1;
23+
24+
}
25+
26+
27+
return ans;
28+
29+
30+
}
31+
32+
33+
public static void main(String[] args) {
34+
35+
Scanner sc = new Scanner(System.in);
36+
37+
int n = sc.nextInt();
38+
39+
while (n-- > 0) {
40+
41+
String input = sc.next();
42+
43+
String ans = helper(input);
44+
45+
System.out.println(ans);
46+
47+
}
48+
49+
}
50+
51+
}

src/Prep/LargestForest.java

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package Prep;
2+
3+
import java.util.Scanner;
4+
5+
public class LargestForest {
6+
7+
public static int helper(int i, int j, String mat[], int n) {
8+
9+
if (i < 0 || i >= n || j < 0 || j >= n) {
10+
return 0;
11+
}
12+
if (mat[i].charAt(j) == 'X' || mat[i].charAt(j) == 'W') {
13+
return 0;
14+
}
15+
16+
// mat[i][j] = 'X';
17+
18+
int x[] = {0, 0, 1, -1};
19+
int y[] = {1, -1, 0, 0};
20+
int res = 1;
21+
for (int k = 0; k < 4; k++) {
22+
23+
int xn = x[k] + i;
24+
int yn = y[k] + j;
25+
res += helper(xn, yn, mat, n);
26+
}
27+
28+
29+
return res;
30+
31+
}
32+
33+
public static void main(String[] args) {
34+
35+
36+
Scanner sc = new Scanner(System.in);
37+
int n = sc.nextInt();
38+
String mat[] = new String[n];
39+
40+
for (int i = 0; i < n; i++) {
41+
42+
mat[i] = sc.nextLine();
43+
44+
}
45+
46+
47+
48+
int ans = 0;
49+
for (int i = 0; i < n; i++) {
50+
51+
52+
for (int j = 0; j < n; j++) {
53+
if (mat[i].charAt(j) == 'T') {
54+
55+
ans = Math.max(ans, helper(i, j, mat, n));
56+
}
57+
}
58+
}
59+
60+
61+
System.out.print(ans);
62+
63+
}
64+
65+
}

src/Prep/MaxSumNonAdj.java

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package Prep;
2+
3+
public class MaxSumNonAdj {
4+
5+
6+
public static int helper2(int arr[], int i, int prev) {
7+
8+
9+
if (i == arr.length) {
10+
11+
return 0;
12+
}
13+
14+
int ex = helper2(arr, i + 1, prev);
15+
16+
int in = 0;
17+
18+
if (prev + 1 != i) {
19+
20+
in = helper2(arr, i + 1, i) + arr[i];
21+
22+
}
23+
24+
25+
return Math.max(ex, in);
26+
27+
28+
29+
}
30+
31+
32+
public static int helper(int arr[]) {
33+
34+
int inc = arr[0];
35+
int exc = 0;
36+
int temp;
37+
38+
for (int i = 1; i < arr.length; i++) {
39+
40+
temp = Math.max(inc, exc);
41+
42+
inc = exc + arr[i];
43+
exc = temp;
44+
45+
46+
47+
}
48+
49+
50+
return Math.max(inc, exc);
51+
52+
53+
54+
}
55+
56+
public static void main(String[] args) {
57+
58+
59+
int arr[] = {-2, 1, 3, -4, 5};
60+
61+
System.out.println(helper(arr));
62+
63+
64+
}
65+
66+
}

src/Prep/MaxdistBtwOccur.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package Prep;
2+
3+
import java.util.HashMap;
4+
5+
public class MaxdistBtwOccur {
6+
7+
8+
public static void main(String[] args) {
9+
10+
int arr[] = {3, 2, 1, 2, 1, 4, 5, 8, 6, 7, 4, 2};
11+
12+
HashMap<Integer, Integer> mp = new HashMap<>();
13+
int max = 0;
14+
15+
for (int i = 0; i < arr.length; i++) {
16+
17+
if (!mp.containsKey(arr[i])) {
18+
19+
mp.put(arr[i], i);
20+
} else {
21+
22+
max = Math.max(max, i - mp.get(arr[i]));
23+
}
24+
25+
}
26+
27+
System.out.println(max);
28+
29+
30+
}
31+
32+
33+
}

0 commit comments

Comments
 (0)