-
Notifications
You must be signed in to change notification settings - Fork 0
/
javaclass.java
372 lines (341 loc) · 10.2 KB
/
javaclass.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
import java.util .*;
import javax.xml.stream.events.Characters;
public class javaclass {
public static void mergesort(int arr[], int start, int end) {
if (start >= end) {
return;
}
int mid = start + (end - start) / 2;
mergesort(arr, start, mid);
mergesort(arr, mid + 1, end);
merging(arr, start, mid, end);
}
public static void merging(int arr[], int start, int mid, int end) {
int s = start;
int m = mid + 1;
int e = end;
int i = 0;
int temp[] = new int[(end - start) + 1];
while (s <= mid && m <= e) {
if (arr[s] < arr[m]) {
temp[i] = arr[s++];
} else {
temp[i] = arr[m++];
}
i++;
}
while (s <= mid) {
temp[i++] = arr[s++];
}
while (e >= m) {
temp[i++] = arr[m++];
}
for (i = 0, s = start; i < temp.length; i++, s++) {
arr[s] = temp[i];
}
}
public static void quicksort(int arr[],int start,int end){
if(start>=end){
return;
}
int pivet = pivet(arr,start,end);
quicksort(arr, start, pivet-1); quicksort(arr, pivet+1, end);
}
public static int pivet(int arr[], int start,int end){
int piv = arr[end];
int s = start-1;
for(int i = start;i<end;i++){
if(arr[i]<=piv){
s++;
int temp = arr[i];
arr[i]= arr[s];
arr[s]= temp;
}
}s++;
int temp = piv;
arr[end]= arr[s];
arr[s]= temp;
return s;
}
public static int sraa(int arr[],int target,int start,int end){
if(start>end){
return -1;
}
int mid = start +(end -start)/2;
if(target==arr[mid]){
return mid;
}
if(arr[start]<=arr[mid]){
if(arr[start]<=target&&target<=arr[mid]){
return sraa(arr, target, start, mid-1);
}else{
return sraa(arr, target, mid+1, end);
}
}else{
if(arr[mid]<=target&&target<=arr[end]){
return sraa(arr, target, mid+1, end);
}
else{
return sraa(arr, target, start, mid-1);
}
}
}
public static void subset(String str, String ans,int i){
if(i==str.length()){
if(ans.length()==0){
System.out.print("null");
}
System.out.println(ans);
return;
}
subset(str, ans+str.charAt(i),i+1);
subset(str, ans, i+1);
}
public static void permutation (String str , String ans){
if(str.length()==0){
System.out.println(ans);
return;
}
for(int i = 0;i<str.length();i++){
char ch = str.charAt(i);
String newStr = str.substring(0, i)+str.substring(i+1);
permutation(newStr, ans+ch);
}
}
static int count;
//nqueens
public static boolean issafe(char board[][] , int row , int col){
for (int i = row -1,j = col-1;i>=0&&j>=0;i--,j--){
if(board[i][j]=='Q'){
return false;
}
}
for(int i = row-1;i>=0;i--){
if(board[i][col]=='Q'){
return false;
}
}
for(int i = row -1,j =col+1;i>=0&&j<board.length;i--,j++){
if(board[i][j]=='Q'){
return false;
}
}
return true;
}
public static boolean nQueen(char board[][],int row){
if(row == board.length){
count++;
return true;
}
for(int i = 0;i<board.length;i++){
if(issafe(board, row, i)){
board[row][i] ='Q';
if(nQueen(board, row+1)){
return true;
}
board[row][i] ='X';
}
}
return false;
}
public static void printnQueen(char board[][]){
System.out.println ("----------------");
for(int i = 0;i<board.length;i++){
for (int j = 0;j<board.length;j++){
System.out.print(board[j][i]);
}System.out.println();
}
}
public static int grid (int i , int j, int n , int m){
if(i==n-1&j==m-1){
return 1;
}else if(i==n||j==m){
return 0;
}
int row = grid(i+1, j, n, m);int col = grid(i, j+1, n, m);
return row +col;
}
public static int factorial (int n){
if(n==0){
return 1;
}
return n*factorial(n-1);
}
public static int gridfact(int i,int j){
int row = factorial(i-1);
int col = factorial(j-1);
int tot = factorial((i-1)+(j-1));
return tot/(row*col);
}
public static boolean isSafesudo(int arr[][],int row,int col , int digit){
for(int i = 0 ; i<9;i++){
if(arr[row][i]==digit){
return false;
}
}
for(int i = 0 ; i<9;i++){
if(arr[i][col]==digit){
return false;
}
}
int sr = (row/3)*3; int sc = (col/3)*3;
for(int i= sr;i<sr+3;i++ ){
for(int j = sc ; j<sc+3;j++){
if(arr[i][j]==digit){
return false;
}
}
}return true;
}
public static boolean sudoku(int arr[][],int row , int col){
if(row == 9){
return true;
}
int newrow = row;int newcol = col+1;
if(col+1==9){
newrow++;
newcol = 0;
}
if(arr[row][col]!=0){
return sudoku(arr, newrow, newcol);
}
for(int i = 1;i<=9;i++){
if(isSafesudo(arr, row, col, i)){
arr[row][col] = i;
if(sudoku(arr, newrow, newcol)){
return true;
}
arr[row][col] =0;
}
}return false;
}
public static void printsudoku(int su[][]){
for(int i = 0;i<9;i++){
for(int j = 0;j<9;j++){
System.out.print(su[i][j]+" ");
}
System.out.println();
}
}
public static void printSolution(int sol[][]){
for(int i = 0;i<sol.length;i++){
for(int j = 0;j<sol.length;j++){
System.out.print(" "+sol[i][j]+" ");
}
System.out.println();
}
}
public static boolean issafe(int maze[][],int row,int col){
return(row>=0&&row<maze.length&&col>=0&&col<maze.length&&maze[row][col]==1);
}
public static boolean solvmaze(int maze[][],int row , int col,int sol[][]){
if(row == maze.length-1&col == maze.length-1&&maze[row][col]==1){
sol[row][col]=1;
return true;
}
if(issafe(maze, row, col)==true){
if(sol[row][col]==1){
return false;
}
sol[row][col]=1;
if(solvmaze(maze, row+1, col, sol)){
return true;
}
if(solvmaze(maze, row, col+1, sol)){
return true;
}
sol[row][col]=0;
return false;
}
return false;
}
public static boolean maze(int maze[][]){
int n = maze.length;
int ans[][]= new int [n][n];
if(solvmaze(maze, 0, 0, ans)==false){
System.out.println("no solution");
return false;
}
printSolution(ans);
return true;
}
final static char[][]L= {{},{},{'a','b','c'},{'d','e','f'},{'g','h','i'},{'j','k','l'},{'m','n','o'},{'p','q','r','s'},{'t','u','v'},{'w','x','y','z'}};
public static void lettercombination(String D){
int len = D.length();
if(len==0){
System.out.println("");
return;
}
bfs(0, len,new StringBuilder(), D);
}
public static void bfs(int pos,int len,StringBuilder sb , String D){
if(pos == len){
System.out.println(sb.toString());
}
else{
char letter[]= L[Character.getNumericValue(D.charAt(pos))];
for(int i = 0;i<letter.length;i++){
bfs(pos+1, len, new StringBuilder(sb).append(letter[i]), D);
}
}
}
public static int containwater(ArrayList<Integer>hight){
int i = 0;
int j = hight.size()-1;
int waterheight = 0;
while(i<j){
int h = Math.min(hight.get(i),hight.get(j));
int with = j-i;
int result = with*h;
waterheight= Math.max(waterheight,result);
if(hight.get(i)<hight.get(j)){
i++;
}else{
j--;
}
}return waterheight;
}
public static boolean pair1(ArrayList<Integer>n,int key){
int s = 0;
int e = n.size()-1;
while(s!=e){
if(n.get(s)+n.get(e)==key){
return true;
}
if(n.get(s)+n.get(e)<key){
s++;
}else{
e--;
}
}return false;
}
public static boolean par2(ArrayList<Integer>n,int key){
int bp = -1;
for(int i = 0;i<n.size();i++){
if(n.get(i)>n.get(i+1)){
bp=i;
break;
}
}
int r = bp;
int l = bp +1;
while(l!=r){
if(n.get(l)+n.get(r)==key){
return true;
}
if(n.get(l)+n.get(r)<key){
l = (l+1)%n.size();
}else{
r=(r-1)%n.size();
}
}return false;
}
public static void main(String arg[]){
ArrayList<Integer> height = new ArrayList<Integer>();
height.add(8);height.add(9);
height.add(1);height.add(2);height.add(3);
height.add(4);height.add(5);height.add(6);
height.add(7);
System.out.print(pair1(height, 101));
}
}