Skip to content

Commit c95fdc1

Browse files
committed
newProblems
1 parent b78e72e commit c95fdc1

15 files changed

+1734
-13
lines changed

src/arrays/intersection.java

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,20 +36,10 @@ public static void main(String[] args) {
3636
}
3737

3838
}
39-
40-
41-
4239

4340
}
4441

4542

46-
47-
48-
49-
5043
}
5144

52-
53-
54-
5545
}

src/arrays/practiceProblems.java

Lines changed: 256 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,256 @@
1+
package arrays;
2+
3+
public class practiceProblems {
4+
5+
public static void sumBoundariesdiagonal(int n) {
6+
7+
int arr[][]=new int[n][n];
8+
9+
for(int i=0;i<n;i++){
10+
11+
for(int j=0;j<n;j++){
12+
13+
// arr[i][j]=sc.nextInt();
14+
15+
}
16+
}
17+
18+
int sum=0;
19+
for(int i=0;i<n;i++){
20+
21+
for(int j=0;j<n;j++){
22+
23+
24+
if(i==0 || j==0 || i==n-1 || j==n-1 || i==j || (i+j)==n-1){
25+
26+
sum+=arr[i][j];
27+
}
28+
29+
}
30+
31+
32+
}
33+
34+
System.out.print(sum);
35+
36+
}
37+
38+
// input - input 2D array
39+
public static void wavePrint(int input[][]){
40+
41+
for(int j=0;j<input[0].length;j++){
42+
43+
44+
if((j&1)>0){
45+
46+
47+
for(int i=input.length-1;i>=0;i--){
48+
49+
System.out.print(input[i][j]+" ");
50+
51+
}
52+
53+
}
54+
else{
55+
56+
for(int i=0;i<input.length;i++){
57+
58+
System.out.print(input[i][j]+" ");
59+
}
60+
61+
}
62+
63+
}
64+
65+
}
66+
public static void spiralPrint(int matrix[][]){
67+
68+
int r=matrix.length;
69+
int c=matrix[0].length;
70+
71+
int sc=0,ec=c-1;
72+
int sr=0,er=r-1;
73+
74+
while(sc<=ec && sr<=er){
75+
76+
77+
// print from left to right
78+
for(int j=sc;j<=ec;j++){
79+
80+
System.out.print(matrix[sr][j]+" ");
81+
}
82+
83+
sr++;
84+
85+
// print top to bottom
86+
for(int i=sr;i<=er;i++){
87+
88+
System.out.print(matrix[i][ec]+" ");
89+
90+
}
91+
92+
ec--;
93+
94+
// print from right to left
95+
if(sr<=er){
96+
for(int j=ec;j>=sc;j--){
97+
98+
System.out.print(matrix[er][j]+" ");
99+
}
100+
101+
er--;
102+
}
103+
104+
if(sc<=ec){
105+
// print from bottom to top..
106+
for(int i=er;i>=sr;i--){
107+
108+
System.out.print(matrix[i][sc]+" ");
109+
110+
}
111+
112+
sc++;
113+
}
114+
}
115+
116+
}
117+
118+
// Leader, if all the elements following it (i.e. present at its right) are less than or equal to A[i].
119+
public static void leaders(int[] input) {
120+
int n=input.length;
121+
int max=input[n-1];
122+
System.out.print(max+" ");
123+
for(int i=n-2;i>=0;i--){
124+
125+
126+
if(input[i]>=max){
127+
System.out.print(input[i]+" ");
128+
max=input[i];
129+
}
130+
131+
}
132+
133+
}
134+
135+
public static String minLengthWord(String input){
136+
137+
int n=input.length();
138+
int min=Integer.MAX_VALUE;
139+
int start=0;
140+
int pos=-1;
141+
for(int i=0;i<n;i++){
142+
143+
144+
if(input.charAt(i)==' ' || i==input.length()-1){
145+
146+
147+
if(i==input.length()-1){
148+
149+
150+
int end=input.length()-1;
151+
int curlen=end-start+1;
152+
153+
if(curlen<min){
154+
min=curlen;
155+
pos=start;
156+
}
157+
158+
}
159+
else{
160+
161+
int end=i-1;
162+
int curlen=end-start+1;
163+
164+
if(curlen<min){
165+
min=curlen;
166+
pos=start;
167+
}
168+
169+
start=i+1;
170+
171+
}
172+
173+
174+
175+
}
176+
177+
178+
179+
}
180+
181+
String ans="";
182+
while(pos<n && input.charAt(pos)!=' ')
183+
{
184+
185+
ans+=input.charAt(pos)+"";
186+
pos++;
187+
188+
189+
}
190+
191+
192+
return ans;
193+
194+
195+
196+
}
197+
198+
//two sorted array increasing order find intersection path maxsum
199+
public static long maximumSumPath(int[] input1, int[] input2) {
200+
201+
int n=input1.length;
202+
int m=input2.length;
203+
204+
int i=0,j=0;
205+
long one=0,two=0,sum=0;
206+
207+
while(i<n && j<m){
208+
209+
210+
if(input1[i]<input2[j]){
211+
212+
one+=input1[i];
213+
i++;
214+
}
215+
216+
else if(input2[j]<input1[i]){
217+
218+
two+=input2[j];
219+
j++;
220+
}
221+
else if(input1[i]==input2[j]){
222+
223+
one+=input1[i];
224+
two+=input2[j];
225+
i++;
226+
j++;
227+
228+
229+
sum+=Math.max(one,two);
230+
231+
one=0;
232+
two=0;
233+
}
234+
235+
236+
}
237+
238+
while(i<n){
239+
240+
one+=input1[i];
241+
i++;
242+
243+
}
244+
245+
while(j<m){
246+
247+
two+=input2[j];
248+
j++;
249+
}
250+
251+
sum+=Math.max(one,two);
252+
253+
return sum;
254+
255+
}
256+
}

src/arrays/starter.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
public class starter {
44

5-
public static void main(String[] args) {
6-
System.out.println("hello world");
7-
}
5+
86

97
}

src/backtracking/NQueen.java

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
package backtracking;
2+
3+
public class NQueen {
4+
5+
6+
public static boolean issafe(int board[][],int i,int j,int n){
7+
8+
for(int row=0;row<i;row++){
9+
10+
if(board[row][j]==1){
11+
return false;
12+
}
13+
14+
}
15+
16+
int x=i;
17+
int y=j;
18+
19+
while(x>=0 && y>=0){
20+
21+
22+
if(board[x][y]==1){
23+
return false;
24+
}
25+
x--;
26+
y--;
27+
28+
}
29+
30+
x=i;
31+
y=j;
32+
33+
34+
while(x>=0 && y<=n){
35+
36+
if(board[x][y]==1){
37+
return false;
38+
}
39+
x--;
40+
y++;
41+
}
42+
return true;
43+
44+
}
45+
46+
public static boolean solve(int board[][],int i,int n){
47+
48+
if(i==n+1){
49+
50+
// board[n][n]=1;
51+
for(int p=0;p<=n;p++){
52+
53+
for(int q=0;q<=n;q++){
54+
55+
System.out.print(board[p][q]+" ");
56+
}
57+
}
58+
59+
System.out.println();
60+
return false;
61+
}
62+
for(int j=0;j<=n;j++){
63+
64+
if(issafe(board,i,j,n)){
65+
66+
board[i][j]=1;
67+
boolean result=solve(board,i+1,n);
68+
if(result){
69+
70+
return true;
71+
}
72+
73+
board[i][j]=0;
74+
75+
}
76+
77+
}
78+
79+
return false;
80+
81+
}
82+
83+
public static void placeNQueens(int n){
84+
85+
int board[][]=new int[n][n];
86+
boolean ans= solve(board,0,n-1);
87+
88+
}
89+
90+
}

0 commit comments

Comments
 (0)