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 7f477f3

Browse files
authored
Merge pull request #67 from arpitjain22june/patch-2
added new problem in arrays
2 parents 25a4b69 + bcd5615 commit 7f477f3

File tree

3 files changed

+66
-0
lines changed

3 files changed

+66
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import java.util.Scanner;
2+
3+
public class MaximumCircularSum {
4+
//form the maximum continous sum possible in circular array
5+
public static void main(String[] args) {
6+
// TODO Auto-generated method stub
7+
Scanner sc = new Scanner(System.in);
8+
int n = sc.nextInt();
9+
int[] a = new int[n];
10+
for (int i = 0; i < n; i++) {
11+
a[i] = sc.nextInt();
12+
}
13+
14+
// circular sum = Math.max(max subarray,total sum-minimum sum)
15+
16+
// max subarray= if the maximum lies in between i.e. no point of circular array
17+
// use normal kadne theorem
18+
int mxc = a[0];
19+
int mxg = a[0];
20+
for (int i = 1; i < n; i++) {
21+
mxc = Math.max(a[i], mxc + a[i]);
22+
if (mxc > mxg) {
23+
mxg = mxc;
24+
}
25+
}
26+
27+
//if subarray is circular i.e. wrapped therefore find lowest sum with the help of same array and subtract from total
28+
// min subarray
29+
int mic = a[0];
30+
int mig = a[0];
31+
for (int i = 1; i < n; i++) {
32+
mic = Math.min(a[i], mic + a[i]);
33+
if (mic < mig) {
34+
mig = mic;
35+
}
36+
}
37+
38+
39+
int to = 0;
40+
for(int i= 0;i<n;i++) {
41+
to+=a[i];
42+
}
43+
System.out.println(Math.max(mxg, to-mig));
44+
}
45+
46+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
You are provided n numbers (both +ve and -ve). Numbers are arranged in a circular form. You need to find the maximum sum of consecutive numbers.
2+
3+
Input Format
4+
it contains an integer n which is the size of array and next line contains n space separated integers denoting the elements of the array.
5+
6+
Constraints
7+
1<=n<=1000
8+
|Ai| <= 10000
9+
10+
Output Format
11+
Print the maximum circular sum
12+
13+
Sample Input
14+
7
15+
8 -8 9 -9 10 -11 12
16+
Sample Output
17+
22
18+
Explanation
19+
Maximum Circular Sum = 22 (12 + 8 - 8 + 9 - 9 + 10)

‎Practice Problems/Arrays/README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ All questions added here are taken from various sites like GeeksForGeeks, LeetCo
55
- [Knight's Possible Moves](https://github.com/srsandy/Data-Structures-and-Algorithms-in-Java-2nd-Edition-by-Robert-Lafore/tree/master/Practice%20Problems/Arrays/Knight's%20Possible%20Moves)
66
- [Find All Numbers Disappeared In An Array](https://github.com/srsandy/Data-Structures-and-Algorithms-in-Java-2nd-Edition-by-Robert-Lafore/tree/master/Practice%20Problems/Arrays/Find%20All%20Numbers%20Disappeared%20In%20An%20Array)
77
- [Maximum Subarray](https://github.com/srsandy/Data-Structures-and-Algorithms-in-Java-2nd-Edition-by-Robert-Lafore/tree/master/Practice%20Problems/Arrays/Maximum%20Subarray)
8+
- [MaximumCircularSum]()
89
- [Non Decreasing Array](https://github.com/srsandy/Data-Structures-and-Algorithms-in-Java-2nd-Edition-by-Robert-Lafore/tree/master/Practice%20Problems/Arrays/Non%20Decreasing%20Array)
910
- [Product Array](https://github.com/srsandy/Data-Structures-and-Algorithms-in-Java-2nd-Edition-by-Robert-Lafore/tree/master/Practice%20Problems/Arrays/Product%20Array)
1011
- [Sock Merchant](https://github.com/srsandy/Data-Structures-and-Algorithms-in-Java-2nd-Edition-by-Robert-Lafore/tree/master/Practice%20Problems/Arrays/Sock%20Merchant)

0 commit comments

Comments
(0)

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