-
Notifications
You must be signed in to change notification settings - Fork 127
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #70 from mephi007/master
Maximum circular subarray sum Issue #65, tested OK
- Loading branch information
Showing
2 changed files
with
90 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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--; | ||
} | ||
} | ||
|
||
} |