4
\$\begingroup\$

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.

\$\endgroup\$
1
  • \$\begingroup\$ related:PPCG \$\endgroup\$
    – JayCe
    Aug 28, 2018 at 18:09

3 Answers 3

4
\$\begingroup\$

Method name and return types

public static int isSupper(int[] a)

As mentioned in @200_success's answer, a method that begins with is, by Java convention, is (pun unintended) usually expected to return a boolean. Looking at this method, it should tell me if the argument is a set of supper times 'super array' or not.

Edge cases

Your implementation also implicitly rejects negative values, as you start with sum = 0 as the comparison. Is { -100, 0, 100 } really not a 'super array'?

How about when we are adding very large numbers then?

System.out.println(isSupper(new int[] {Integer.MAX_VALUE - 1, Integer.MAX_VALUE, 0 }));

// output
sum is: 0
next Value is: 2147483646
sum is: 2147483646
next Value is: 2147483647
sum is: -3
next Value is: 0
1
\$\endgroup\$
7
\$\begingroup\$

It's easier to use the enhanced for loop like this:

    for(int value : a) {
        if(value <= sum) 
            return 0;
        sum += value;       
    }
    return 1; 

This also clears up some unnecessary lines like checking if the function should break and return. When you return a value, it immediately leaves the function. Therefore, you don't need to break then return. If all value pass the for loop, then it will return 1.

Also, you might want to return boolean instead of int in your method because it's just true or false.

\$\endgroup\$
7
\$\begingroup\$

Watch your spelling: you spelled "super" correctly in the class name, but not the function name.

A function that produces a yes/no response is called a predicate, and it should return a boolean rather than an int. (If the exercise requires an int return value, then I would consider it a poorly written exercise.)

Consider changing the function signature to public static boolean isSuper(int... a). Then you have the option to call the function using either

isSuper(2, 3, 6, 13)

or

isSuper(new int[] { 2, 3, 6, 13 })

Note that the class has an implicit SuperArray() constructor. While it is harmless, I recommend suppressing it by defining

public class SuperArray {
    // Suppress the default constructor
    private SuperArray() {}

    public static boolean isSuper(int... a) {
        /* Insert what @TheProgrammerG said.  It's good advice. */
    }
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.