-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpractice.java
More file actions
588 lines (520 loc) · 15.4 KB
/
Copy pathpractice.java
File metadata and controls
588 lines (520 loc) · 15.4 KB
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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
import java.util.*;
public class practice {
public static void reverseNo() {
System.out.print("Enter a number : ");
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
while (n > 0) {
int r = n % 10;
System.out.print(r + " ");
n = n / 10;
}
}
;
// PATTURNS
public static void rightTriangle() {
int n = 4;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
System.out.print("*");
}
System.out.println("");
}
}
// i=1 j=4
// i=2 j=3
public static void invertedRightTriangle() {
int n = 4;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n - i + 1; j++) {
System.out.print("*");
}
System.out.println("");
}
}
// Void Rectangle
public static void voidRectangle() {
int n = 4;
for (int i = 1; i <= n; i++) {
if (i == 2 || i == 3) {
System.out.print("* *");
} else {
System.out.print("* * * *");
}
System.out.println("");
}
}
// NUMBER's TRANGLE
public static void numTraingle() {
int n = 4;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(j);
}
System.out.println();
}
}
// char triangle
public static void charTriangle() {
int n = 4;
char ch = 'A';
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(ch);
ch++;
}
System.out.println("");
}
}
// FACTORIAL
public static int factorial(int n) {
int fact = 1;
for (int i = 1; i <= n; i++) {
fact = fact * i;
}
return fact;
}
// BINARY COERFFICIENT
public static int binCoff(int n, int r) {
int nfact = factorial(n);
int rfact = factorial(r);
int n_rfact = factorial(n - r);
int binCoff = nfact / (rfact * n_rfact);
return binCoff;
}
// Check Prime
public static boolean isPrime(int n) {
if (n == 2) {
return true;
}
for (int i = 2; i <= Math.sqrt(n); i++) {
if (n % i == 0) {
return false;
}
}
return true;
}
public static int binaryToDecimal(int binNum) {
int pow = 0;
int dec = 0;
while (binNum > 0) {
int lastDigit = binNum % 10;
dec += lastDigit * (int) Math.pow(2, pow);
pow++;
binNum /= 10;
}
return dec;
}
public static int decimalToBinary(int n) {
int pow = 0;
int binNum = 0;
while (n > 0) {
int rem = n % 2;
binNum += rem * Math.pow(10, pow);
pow++;
n /= 2;
}
return binNum;
}
/*
ADVANCED PATTURN
* * * * *
* *
* *
* * * * *
*/
public static void hollow_Rectangle(int rows, int col) {
for (int i = 0; i <= rows; i++) {
for (int j = 0; j <= col; j++) {
if (i == 1 || i == rows || j == 1 || j == col) {
System.out.print(" * ");
} else {
System.out.print(" ");
}
}
System.out.println();
}
}
/*
INVERTED ROTATED HALF PYRAMID
* space = 4 star = 1
* * space = 3 star = 2
* * * space = 2 star = 3
* * * * space = 1 star = 4
* * * * * sapce = 0 star = 5
*/
public static void irhPyramid(int n) {
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n - i; j++) {
System.out.print(" ");
}
for (int k = 1; k <= i; k++) {
System.out.print(" * ");
}
System.out.println("");
}
}
/*
1 2 3 4 5 num = 1-5 space = 0
1 2 3 4 num = 1-4 space = 1
1 2 3 num = 1-3 space = 2
1 2 num = 1-2 space = 3
1 num = 1 space = 4
*/
public static void iNumHPyramid(int n) {
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n - i + 1; j++) {
System.out.print(j + " ");
}
System.out.println(" ");
}
}
/* FLOYD'S TRIANGLE
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
*/
public static void floydsTriangle(int n) {
int counter = 10;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(counter + " ");
counter++;
}
System.out.println("");
}
}
/* 0-1 Triangle
1 1,1
0 1 2,2 2,1
1 0 1 3,1|3,3 3,2
0 1 0 1 4,2|4,4 4,1|4,3
1 0 1 0 1
*/
public static void binaryTriangle(int n) {
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
if ((i + j) % 2 == 0) {
System.out.print(" 1 ");
} else {
System.out.print(" 0 ");
}
}
System.out.println("");
}
}
;
/* BUTTERFLY (n=4)
* * 6 4 1
* * * * 4 4 2
* * * * * * 2 4 3
* * * * * * * * 0 4 4
* * * * * * * *
* * * * * *
* * * *
* *
*/
public static void butterfly(int n) {
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(" * ");
}
for (int j = 1; j <= 2 * (n - i); j++) {
System.out.print(" ");
}
for (int j = 1; j <= i; j++) {
System.out.print(" * ");
}
System.out.println("");
}
for (int i = n; i >= 1; i--) {
for (int j = 1; j <= i; j++) {
System.out.print(" * ");
}
for (int j = 1; j <= 2 * (n - i); j++) {
System.out.print(" ");
}
for (int j = 1; j <= i; j++) {
System.out.print(" * ");
}
System.out.println("");
}
}
;
/* Solid Rhombus
* * * * *
* * * * *
* * * * *
* * * * *
* * * * *
*/
public static void Rhombus(int n) {
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n - i; j++) {
System.out.print(" ");
}
for (int j = 0; j <= n; j++) {
System.out.print(" * ");
}
System.out.println();
}
}
/* Hollow Rhombus
* * * * *
* *
* *
* *
* * * * *
*/
public static void hRhombus(int n) {
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n - i; j++) {
System.out.print(" ");
}
for (int j = 1; j <= n; j++) {
if (i == 1 || i == n || j == 1 || j == n) {
System.out.print(" * ");
} else {
System.out.print(" ");
}
}
System.out.println();
}
}
/* Diamond
* s = 3 st = 1
*** s = 2 st = 3
***** s = 1 st = 5
******* s = 0 st = 7
*****
***
*
*/
public static void diamond(int n) {
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n - i; j++) {
System.out.print(" ");
}
for (int j = 1; j <= 2 * i - 1; j++) {
System.out.print(" * ");
}
System.out.println();
}
for (int i = n; i >= 1; i--) {
for (int j = 1; j <= n - i; j++) {
System.out.print(" ");
}
for (int j = 1; j <= 2 * i - 1; j++) {
System.out.print(" * ");
}
System.out.println();
}
}
/*
ARRAYS
- Largest in Array
- Creating an Array
- Input Array Elements
- Linear search
1. BINARY SEARCH
2. REVERSE AN ARRAY
3. PAIRS OF NUMBER
4. Print SUBARRAYS
5. Min/Max SUBARRAY SUM
6. Min/Max SUBARRAY SUM - Prefix Array Meathod
7. Min/Max SUBARRAY SUM - Kadane's ALGO
*/
public static int largestInArray(int num[]) {
int lar = Integer.MIN_VALUE;
for (int i = 0; i < num.length; i++) {
if (num[i] > lar) {
lar = num[i];
}
}
return lar;
}
// CREATE AN ARRAY
public static int[] createArray() {
int arr[] = new int[5];
Scanner sc = new Scanner(System.in);
for (int i = 0; i < arr.length; i++) {
arr[i] = sc.nextInt();
}
for (int i = 0; i < arr.length; i++) {
System.out.print(+arr[i] + " ");
}
return arr;
}
// LINEAR SEARCH
public static int linearSearch(int num[], int key) {
for (int i = 0; i < num.length; i++) {
if (num[i] == key) {
return i;
}
}
System.out.println("Not Found!");
return -1;
}
// BINARY SEARCH
public static int binarySearch(int arr[], int key) {
int start = 0;
int end = arr.length - 1;
while (start <= end) {
int mid = (start + end) / 2;
if (arr[mid] == key) {
return mid;
} else if (arr[mid] > key) {
end = mid - 1;
} else if (arr[mid] < key) {
start = mid + 1;
}
}
return -1;
}
// REVERSE AN ARRAY
public static void reverseArray(int num[]) {
int start = 0;
int end = num.length - 1;
while (start <= end) {
int temp = num[start];
num[start] = num[end];
num[end] = temp;
start++;
end--;
}
for (int i = 0; i < num.length; i++) {
System.out.print(num[i] + ", ");
}
}
// PAIRS OF NUM IN AN ARRAY
public static void pairsOfNum(int num[]) {
int tp = 0;
for (int i = 0; i < num.length; i++) {
for (int j = i + 1; j < num.length; j++) {
System.out.println("(" + num[i] + "," + num[j] + ")");
tp++;
}
}
System.out.println("Total pairs: " + tp);
}
public static void printSubarray(int arr[]) {
for (int i = 0; i < arr.length; i++) {
for (int j = i; j < arr.length; j++) {
for (int k = i; k <= j; k++) {
System.out.print(arr[k] + " ");
}
System.out.println("");
}
System.out.println("");
}
}
public static void maxSubArraySum(int arr[]) {
int maxSum = Integer.MIN_VALUE;
int currSum = 0;
int miniSum = Integer.MAX_VALUE;
for (int i = 0; i < arr.length; i++) {
for (int j = i; j < arr.length; j++) {
currSum = 0;
for (int k = i; k <= j; k++) {
currSum = currSum + arr[k];
}
System.out.print(currSum + " ");
if (currSum > maxSum) {
maxSum = currSum;
}
if (currSum < miniSum) {
miniSum = currSum;
}
System.out.println();
}
System.out.println();
}
System.out.println("Max Sub-ArraySum : " + maxSum);
System.out.println("Mini Sub-ArraySum : " + miniSum);
}
// Max SubArray Sum - Prefix Array Meathod
public static void prefixSubArraySum(int arr[]) {
int maxSum = Integer.MIN_VALUE;
int currSum = 0;
int prefix[] = new int[arr.length];
prefix[0] = arr[0];
for (int i = 1; i < arr.length; i++) {
prefix[i] = prefix[i - 1] + arr[i];
}
for (int i = 0; i < arr.length; i++) {
for (int j = i; j < arr.length; j++) {
currSum = i == 0 ? prefix[j] : prefix[j] - prefix[i - 1]; // tertiary operator
// here [i] is [start] and [j] is [end]
}
if (currSum > maxSum) {
maxSum = currSum;
}
}
System.out.print("Max Sub-Array Sum using Prefix Array: " + maxSum);
}
// Max SubArray Sum - KADANE's ALGO
public static void kadaneSubArraySum(int arr[]) {
int maxSum = Integer.MIN_VALUE;
int currSum = 0;
for (int i = 0; i < arr.length; i++) {
currSum = currSum + arr[i];
if( currSum < 0){
currSum = 0;
}
maxSum = Math.max(currSum,maxSum);
}
System.out.println("Max SubArray Sum using Kadan's Algo: " + maxSum);
}
public static void main(String args[]) {
// for (int i = 1; i <= 4; i++) {
// System.out.println("****");
// }
// reverseNo();
// rightTriangle();
// invertedRightTriangle();
// voidRectangle();
// numTraingle();
// charTriangle();
// factorial(20);
// System.out.print(binCoff(4,2));
// System.out.print(isPrime(11));
// System.out.print(binaryToDecimal(0010));
// System.out.print(decimalToBinary(400));
// irhPyramid(7);
// iNumHPyramid(7);
// floydsTriangle(5);
// binaryTriangle(7);
// butterfly(7);
// hRhombus(5);
// diamond(4);
int arr1[] = {2, 4, 6, 8, 10, 12};
// System.out.println(largestInArray(arr));
// createArray();
{ // LINERAR SEARCH
// int result = linearSearch(arr, 10);
// if(result == -1){
// System.out.println("Not found");
// }else{
// System.out.println("the required value is at index: " + result);
// }
}
{ // BINARY SEARCH
// int num[] = {44, 46, 47, 49, 62, 66, 69, 78, 95, 96, 97, 98, 99};
// int result = binarySearch(num, 66);
// if (result == -1) {
// System.out.println("Item Not Found");
// } else {
// System.out.println("The key is at index: " + result);
// }
}
// pairsOfNum(arr);
// printSubarray(arr);
// reverseArray(arr);
// maxSubArraySum(arr);
// prefixSubArraySum(arr);
int arr[] = {-1,-2,-3,-4};
kadaneSubArraySum(arr);
}
}