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 49ea8c2

Browse files
committed
p
1 parent 705a3de commit 49ea8c2

File tree

8 files changed

+245
-115
lines changed

8 files changed

+245
-115
lines changed

‎Core Java/practice.class‎

1.41 KB
Binary file not shown.

‎Core Java/practice.java‎

Lines changed: 116 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,7 @@ public static int largestInArray(int num[]) {
379379
return lar;
380380
}
381381

382+
// CREATE AN ARRAY
382383
public static int[] createArray() {
383384
int arr[] = new int[5];
384385
Scanner sc = new Scanner(System.in);
@@ -391,6 +392,7 @@ public static int[] createArray() {
391392
return arr;
392393
}
393394

395+
// LINEAR SEARCH
394396
public static int linearSearch(int num[], int key) {
395397
for (int i = 0; i < num.length; i++) {
396398
if (num[i] == key) {
@@ -403,6 +405,7 @@ public static int linearSearch(int num[], int key) {
403405
return -1;
404406
}
405407

408+
// BINARY SEARCH
406409
public static int binarySearch(int arr[], int key) {
407410
int start = 0;
408411
int end = arr.length - 1;
@@ -427,13 +430,106 @@ public static void reverseArray(int num[]) {
427430
int start = 0;
428431
int end = num.length - 1;
429432
while (start <= end) {
430-
int temp = start;
431-
start = end;
432-
end = temp;
433+
int temp = num[start];
434+
num[start] = num[end];
435+
num[end] = temp;
433436
start++;
434437
end--;
435438
}
436-
439+
for (int i = 0; i < num.length; i++) {
440+
System.out.print(num[i] + ", ");
441+
}
442+
}
443+
444+
// PAIRS OF NUM IN AN ARRAY
445+
public static void pairsOfNum(int num[]) {
446+
int tp = 0;
447+
for (int i = 0; i < num.length; i++) {
448+
for (int j = i + 1; j < num.length; j++) {
449+
System.out.println("(" + num[i] + "," + num[j] + ")");
450+
tp++;
451+
}
452+
}
453+
System.out.println("Total pairs: " + tp);
454+
}
455+
456+
public static void printSubarray(int arr[]) {
457+
for (int i = 0; i < arr.length; i++) {
458+
for (int j = i; j < arr.length; j++) {
459+
for (int k = i; k <= j; k++) {
460+
System.out.print(arr[k] + " ");
461+
}
462+
System.out.println("");
463+
}
464+
System.out.println("");
465+
}
466+
}
467+
468+
public static void maxSubArraySum(int arr[]) {
469+
int maxSum = Integer.MIN_VALUE;
470+
int currSum = 0;
471+
int miniSum = Integer.MAX_VALUE;
472+
473+
for (int i = 0; i < arr.length; i++) {
474+
for (int j = i; j < arr.length; j++) {
475+
currSum = 0;
476+
for (int k = i; k <= j; k++) {
477+
currSum = currSum + arr[k];
478+
}
479+
System.out.print(currSum + " ");
480+
if (currSum > maxSum) {
481+
maxSum = currSum;
482+
}
483+
if (currSum < miniSum) {
484+
miniSum = currSum;
485+
}
486+
System.out.println();
487+
}
488+
System.out.println();
489+
}
490+
System.out.println("Max Sub-ArraySum : " + maxSum);
491+
System.out.println("Mini Sub-ArraySum : " + miniSum);
492+
}
493+
494+
// Max SubArray Sum - Prefix Array Meathod
495+
public static void prefixSubArraySum(int arr[]) {
496+
int maxSum = Integer.MIN_VALUE;
497+
int currSum = 0;
498+
int prefix[] = new int[arr.length];
499+
500+
prefix[0] = arr[0];
501+
for (int i = 1; i < arr.length; i++) {
502+
prefix[i] = prefix[i - 1] + arr[i];
503+
}
504+
505+
for (int i = 0; i < arr.length; i++) {
506+
for (int j = i; j < arr.length; j++) {
507+
currSum = i == 0 ? prefix[j] : prefix[j] - prefix[i - 1]; // tertiary operator
508+
// here [i] is [start] and [j] is [end]
509+
}
510+
511+
if (currSum > maxSum) {
512+
maxSum = currSum;
513+
}
514+
}
515+
System.out.print("Max Sub-Array Sum using Prefix Array: " + maxSum);
516+
}
517+
518+
// Max SubArray Sum - KADANE's ALGO
519+
public static void kadaneSubArraySum(int arr[]) {
520+
int maxSum = Integer.MIN_VALUE;
521+
int currSum = 0;
522+
523+
for (int i = 0; i < arr.length; i++) {
524+
currSum = currSum + arr[i];
525+
526+
if( currSum < 0){
527+
currSum = 0;
528+
}
529+
maxSum = Math.max(currSum,maxSum);
530+
}
531+
532+
System.out.println("Max SubArray Sum using Kadan's Algo: " + maxSum);
437533
}
438534

439535
public static void main(String args[]) {
@@ -458,7 +554,7 @@ public static void main(String args[]) {
458554
// butterfly(7);
459555
// hRhombus(5);
460556
// diamond(4);
461-
int arr[] = {2, 4, 6, 8, 10, 12};
557+
int arr1[] = {2, 4, 6, 8, 10, 12};
462558
// System.out.println(largestInArray(arr));
463559
// createArray();
464560

@@ -472,15 +568,21 @@ public static void main(String args[]) {
472568
}
473569

474570
{ // BINARY SEARCH
475-
int num[] = {44, 46, 47, 49, 62, 66, 69, 78, 95, 96, 97, 98, 99};
476-
int result = binarySearch(num, 66);
477-
478-
if (result == -1) {
479-
System.out.println("Item Not Found");
480-
} else {
481-
System.out.println("The key is at index: " + result);
482-
}
483-
571+
// int num[] = {44, 46, 47, 49, 62, 66, 69, 78, 95, 96, 97, 98, 99};
572+
// int result = binarySearch(num, 66);
573+
574+
// if (result == -1) {
575+
// System.out.println("Item Not Found");
576+
// } else {
577+
// System.out.println("The key is at index: " + result);
578+
// }
484579
}
580+
// pairsOfNum(arr);
581+
// printSubarray(arr);
582+
// reverseArray(arr);
583+
// maxSubArraySum(arr);
584+
// prefixSubArraySum(arr);
585+
int arr[] = {-1,-2,-3,-4};
586+
kadaneSubArraySum(arr);
485587
}
486588
}
File renamed without changes.

0 commit comments

Comments
(0)

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