An Super array is defined to be an array in which each element is greater than sum of all elements before that. For example:
{2, 3, 6, 13}
is a Super array. (2 < 3
,2+3 < 6
,2 + 3 + 6 < 13
){2, 3, 5, 11}
is a NOT a Super array.2 + 3 < 5
is false.
I wrote following code to find the array is Super Array or not .
public class SuperArray {
public static void main(String args[]) {
System.out.println("The result is: " + isSupper(new int[] {2, 3, 6, 13}));
System.out.println("The result is: " + isSupper(new int[] {2, 3, 5, 11}));
}
public static int isSupper(int[] a){
int result = 0;
int sum = 0;
int nextValue = 0;
for(int i = 0; i < a.length; i++) {
nextValue = a[i];
if(nextValue > sum) {
result = 1;
} else {
result = 0;
break;
}
System.out.println("sum is: " + sum);
System.out.println("next Value is: " + nextValue);
sum += a[i];
}
return result;
}
}
Is this a good way to find Super array? First a assign sum = 0 and check that sum to first number of an array and so on.