Skip to content

Commit 6cc868d

Browse files
committed
Superincreasing
1 parent e819b93 commit 6cc868d

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

src/easy/Superincreasing.java

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package easy;
2+
3+
/**
4+
* Have the function Superincreasing(arr) take the array of numbers stored in arr
5+
* and determine if the array forms a superincreasing sequence
6+
* where each element in the array is greater than the sum of all previous elements.
7+
* The array will only consist of positive integers.
8+
* For example: if arr is [1, 3, 6, 13, 54] then your program
9+
* should return the string "true" because it forms a superincreasing sequence.
10+
* If a superincreasing sequence isn't formed, then your program
11+
* should return the string "false"
12+
*/
13+
public class Superincreasing {
14+
15+
/**
16+
* Superincreasing function.
17+
*
18+
* @param arr input array
19+
* @return "true" if is a superincreasing sequence
20+
*/
21+
private static String superincreasing(int[] arr) {
22+
int sum = arr[0];
23+
for (int i = 1; i < arr.length; i++) {
24+
if (arr[i] > sum) {
25+
sum += arr[i];
26+
} else {
27+
return "false";
28+
}
29+
}
30+
return "true";
31+
}
32+
33+
/**
34+
* Entry point.
35+
*
36+
* @param args command line arguments
37+
*/
38+
public static void main(String[] args) {
39+
var result1 = superincreasing(new int[]{1, 3, 6, 13, 54});
40+
System.out.println(result1);
41+
var result2 = superincreasing(new int[]{3, 3});
42+
System.out.println(result2);
43+
}
44+
45+
}

0 commit comments

Comments
 (0)