Skip to content

Commit

Permalink
Merge pull request #70 from mephi007/master
Browse files Browse the repository at this point in the history
Maximum circular subarray sum Issue #65, tested OK
  • Loading branch information
akshitagupta15june authored Sep 30, 2020
2 parents 4fb1e49 + 23432ba commit 928ae08
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 0 deletions.
42 changes: 42 additions & 0 deletions Array/Max_sum_circular_array.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package hacktober;

import java.util.Arrays;
import java.util.Scanner;

public class Max_sum_circular_array {

public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
System.out.println("Enter number of element to be in array");
int n = sc.nextInt();
int[] arr = new int[n];
System.out.println("Enter elements to the array");
for (int i = 0; i < n; i++) {
arr[i] = sc.nextInt();
}

System.out.println(maxSumCircular(arr));
}
private static int maxSumCircular(int[] A) {
// TODO Auto-generated method stub
int curMax = 0;
int max = Integer.MIN_VALUE;
int curMin = 0;
int min = Integer.MAX_VALUE;
int sum = 0;
for (int i : A) {
sum += i;
curMin = Math.min(curMin + i, i);
min = Math.min(curMin, min);
curMax = Math.max(curMax + i, i);
max = Math.max(curMax, max);
}
if (sum == min) {
return max;
}
return Math.max(max, sum - min);

}

}
48 changes: 48 additions & 0 deletions Array/Rotate_an_array_to_the_left_by_3_position.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package Array;

import java.util.Arrays;
import java.util.Scanner;

public class Rotate_an_array_to_the_left_by_3_position {

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter number of element to be in array");
int n = 0;
do {
n = sc.nextInt();
if(n < 3) {
System.out.println("Please enter number of element to be greater than 3");

}
}while(n<3);

int[] arr = new int[n];
System.out.println("Enter elements to the array");
for (int i = 0; i < n; i++) {
arr[i] = sc.nextInt();
}

rotateby3(arr,3);
System.out.println(Arrays.toString(arr));
}

public static void rotateby3(int[] arr, int k) {
int n = arr.length;
rotate(arr,0,k-1);
rotate(arr,k,n-1);
rotate(arr,0,n-1);

}

public static void rotate(int[] arr, int start, int end) {
while(start <= end) {
int temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
start++;
end--;
}
}

}

0 comments on commit 928ae08

Please sign in to comment.