1

Write a function that takes an array as input and returns an array of 2 numbers. The returned array contains the sum of even numbers and sum of odd numbers from the input.

If any of the input is null it should be treated as an empty array

Example: Input:
[30, 18, 2, 83, 20, 71]
Output:
[70, 154]

Input:
[14, 11, 10, 67, 41]
Output:
[24, 119]

Input: [36, 24, -82, 29, 44, -3, -100, -5, 49] Output: [-78, 70]

The function that I have written is

public int[] getSumOfEvensAndOdds(int[] input) {
        
        int x[] = input;
        int even = 0, odd = 0;
        for (int i = 0; i < x.length; i++) {
            if (x[i] % 2 == 0)
                even += x[i];
            else
                odd += x[i];
        }
        int[] ans={even, odd};
        return ans;
    }

But how should I incorporate the part of the empty array?

3
  • 1
    What do you mean with "How should I incorporate the part of the empty array"? Commented Aug 3, 2018 at 6:48
  • 1
    What would be the reault of an empty array? How many elements can you sum up? Commented Aug 3, 2018 at 6:48
  • simplest: if (input == null) return new int[2]; some better, just do the for if (input != null) (Note: why do you need x?) Commented Aug 3, 2018 at 6:56

6 Answers 6

4

Check if input is null first. If it is, work on an empty array instead:

int x[] = input == null ? new int[0] : input;
Sign up to request clarification or add additional context in comments.

3 Comments

I'm not sure. I think the result has always to have two elements: the sum of even and the sum of odd elements.
@LuCio it will. x is just the input to the summing functionality.
Ok ... i see ... it's not the result.
2

if any of the input is null it should be treated as an empty array

Why not just check for null value?

public int[] getSumOfEvensAndOdds(int[] input) {
    int even = 0, odd = 0;
    if(null != input){
        for (int i: input) {
            if (0 == i % 2){
                even += i;
            } else{
                odd += i;
            }
        }
    }
    return new int[]{even, odd};
}

Comments

0

You need something like this:

public class Test {

public static int[] getSumOfEvensAndOdds(int[] input) {
    int[]   def = {0,0};

    if (input != null && input.length!=0) {
        int x[] = input;
        int even = 0, odd = 0;
        for (int i = 0; i < x.length; i++) {
            if (x[i] % 2 == 0)
                even += x[i];
            else
                odd += x[i];
        }
        int[] ans = {even, odd};
        return ans;
    }

    return def;
}

public static void main(String [ ] args){
    int[]   ar = {10,20,30,40,50,60,71,80,90,91};
    int[]   res;

    res = getSumOfEvensAndOdds(ar);
    System.out.println("Result: " + res[0] + " " + res[1]);

    int[]   ar2 = {};
    res = getSumOfEvensAndOdds(ar2);
    System.out.println("Result: " + res[0] + " " + res[1]);

    int[]   ar3 = null;
    res = getSumOfEvensAndOdds(ar3);
    System.out.println("Result: " + res[0] + " " + res[1]);
}

}

I use input!=null to check whether the array is null and input.length!=0 to check if its size is 0. Also, in the main method I give three examples.

Comments

0
public int[] getSumOfEvensAndOdds(int[] input) {

        int x[] = input;
        int even = 0, odd = 0;
        for (int i = 0; i < x.length; i++) {
        if(x[i] != null)    //in case array contains elements which aren't null
        {
            if (x[i] % 2 == 0)
                even += x[i];
            else
                odd += x[i];
        }
        else   //in case the array has null array elements
        {
           even = 0;
           odd = 0;
        }
        int[] ans={even, odd};
        return ans;
    }

2 Comments

Thank you for the response. As you have stated here while checking if the array is empty, but the question says that if any of the elements is null then treat it as an empty array.
Did you try to compile that? x[i] has type int so it is a primitive type, not a reference. It cannot be null.
0

Empty array may be return like this return new int[]{}

Comments

-1

Your question is just about arrays being empty. A quick search got me this: How can I check whether an array is null / empty?

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.