Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Hi,

i am trying below challenge

http://codingbat.com/prob/p190968

i tried as below

i failed few tests. How to fix and improve my code. please advise

i am trying below challenge

http://codingbat.com/prob/p190968

i tried as below

```
public int sum2(int[] nums) {
int numsSum[]=new int[4];
if(nums.length>=2)
return nums[0]+nums[1];
// else (nums.length==1)
// return nums[0];
return 0;
}
```

```
Expected Run
sum2({1, 2, 3}) → 3 3 OK
sum2({1, 1}) → 2 2 OK
sum2({1, 1, 1, 1}) → 2 2 OK
sum2({1, 2}) → 3 3 OK
sum2({1}) → 1 0 X
sum2({}) → 0 0 OK
sum2({4, 5, 6}) → 9 9 OK
sum2({4}) → 4 0 X
other tests
OK
```

i failed few tests. How to fix and improve my code. please advise

Experts Exchange Solution brought to you by

Enjoy your complimentary solution view.

Get every solution instantly with premium.
Start your 7-day free trial.

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

```
public int sum2(int[] nums) {
int numsSum[]=new int[4];
if(nums.length>=2)
return nums[0]+nums[1];
if(nums.length==1)
return nums[0];
return 0;
}
```

if i put both if and then return 0 at end i passed all test though. not sure if that is correct way of doing

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial```
public int sum2(int[] nums) {
if (nums.length ==0){return 0;}
if(nums.length==1){return nums[0];}
return nums[0]+nums[1];
}
```

```
public int sum2(int[] nums) {
return nums.length>1?nums[0]+nums[1]:nums.length>0?nums[0]:0;
}
```

return (nums.length>0?nums[0]:0)+(nums.leng th>1?nums[ 1]:0);

return nums.length>1?nums[0]+nums[1]:nums.l ength>0?nu ms[0]:0;

return nums.length==0?0:nums.length==1?nums [0]:nums[0 ]+nums[1;]

return nums.length<1?0:nums[0]+(nums.length <2?0:nums[ 1]);

i have not clearly understood meaning of above 4 lines.

what it means by

nums[1]:0);

also what it mean by

nums[1;]

also as below

return nums.length<1?0:nums[0]+(n

please advise

is nums[1] when nums.length>1, otherwise, it is 0

nums[1;]

should have been

nums[1];

return nums.length>1?nums[0]+nums

return nums.length==0?0:nums.leng

return nums.length<1?0:nums[0]+(n

i have not clearly understood meaning of above 4 lines.<<

They are all nested ternary (if-then-else) statements satisfying the requirements;

The first is saying, if the length is greater than 0 return the first number, if not return 0,

then, if the length is greater than 1, plus the second number, else plus 0.

The second is saying, if the length is greater than 1, return the first number plus the second number, then, if the length is greater than 0, return the first number, else return 0.

The third is saying, if the length is less than 1, return 0, else, if the length equals 1, return the first number, else return the first number plus the second number.

The fourth is saying, if the length is less than 1, return 0, else return the first number added to, if the length is less than 2, plus 0, else plus the second number.

return (nums.length>0?nums[0]:0)+// >1 should cover >0 case already in the front right???(nums.leng th>1?nums[ 1]:0);//// >0 should cover >1 case already in the front right???

return nums.length>1?nums[0]+nums[1]:nums.l ength>0?nu ms[0]:0;

The first is saying, if the length is greater than 0 return the first number, if not return 0,

then, if the length is greater than 1, plus the second number, else plus 0.

The second is saying, if the length is greater than 1, return the first number plus the second number, then, if the length is greater than 0, return the first number, else return 0.

return (nums.length>0?nums[0]:0)+>0 covers the nums[0] part of the >1 case result(nums.leng th>1?nums[ 1]:0);//// >0 should cover >1 case already in the front right???

return nums.length>1?nums[0]+numsreturn nums.length>1?([1]:nums.l ength>0?nu ms[0]:0;

nums[0]+nums[1] // this is the >1 case

):(

nums.length>0?nums[0]:0 // this is the not >1 case

);

above can be writen as below??

nums.length==0?nums[0]:0 // this is the not >1 case

nums.length==0?0:nums[0]

(because we know that nums.length is never < 0)

```
public int sum2(int[] nums) {
return (nums.length==0?0:nums[0])+(nums.length>1?nums[1]:0);
}
```

above worked fine

```
public int sum2(int[] nums) {
return (nums.length==0?0:nums[0])+(nums.length>=1?nums[1]:0);
}
```

why above code failing when i change from

(nums.length>1

to

(nums.length>=1

please advise

return (nums.length==0?0:nums[0])

in above working solution where we are taking care when nums.length is 1 case?

```
return nums.length==0?0:nums.length==1?nums[0]:nums[0]+nums[1;]
return nums.length<1?0:nums[0]+(nums.length<2?0:nums[1]);
```

i am bit confused about above 2 lines.How to break down put some brackets around to clearly understand the flow of solution. please advise.

where can i see more nested ternary related examples to understand above lines of codes

(nums.length>1

to

(nums.length>=1<<

Take a look at what this says

return (nums.length==0?0:nums[0])

If the length is 0, return 0, else return the first number plus, if the length is 2 or more, the second number, else plus nothing.

and this says

return (nums.length==0?0:nums[0])

If the length is 0, return 0, else return the first number plus, if the length is 1 or more, the second number, else plus nothing. The problem is that if the length is 1, there is no second number and you will get an out of bounds index error.

BTW, you need to stop piling questions on top of each other too quickly so, when the experts respond, everyone knows which question they're referring to.

where can i see more nested ternary related examples to understand above lines of codes

In just about all the Codingbat questions you've asked.

return (nums.length==0?0:When nums.length == 1, nums.length==0 is false, so the first term becomes nums[0]nums[0])+(nums.length>1?nums[1]:0);

in above working solution where we are taking care when nums.length is 1 case?

When nums.length == 1, nums.length>0 is false, so the second term becomes 0

nums[0]+0 is the required result

return

nums.length<1

?

0

:

(

nums[0]

+

(nums.length<2?0:nums[1])

)

;

//return nums.length==0?0:nums.leng

return

nums.length==0

?

0

:

(

nums.length==1

?

nums[0]

:

(nums[0]+nums[1])

)

;

if (nums.length ==0){return 0;}

if(nums.length==1){return nums[0];}

return nums[0]+nums[1];

}

return nums[0]+nums[1];

but we do not have nums[1] when nums.length is 1?

please advise

return nums[0]+nums[1];<<

No, it shouldn't, it should return just nums[0]

```
//return nums.length==0?0:nums.length==1?nums[0]:nums[0]+nums[1];
return
nums.length==0
?
0
:
[b] ([/b]
nums.length==1
?
nums[0]
:
(nums[0]+nums[1])
[b] )[/b]
;
```

above highlighted brackets are crucial for my understanding. Now get picture with those brackets without them i lost originally.

How to put those brackets in below single line

return nums.length==0?0:

can i write as above? But i am getting below error

public int sum2(int[] nums) {

return nums.length==0?0:(nums.len

Compile problems:

Error: return nums.length==0?0:(nums.len

^^^^^^^^^^^^

The operator + is undefined for the argument type(s) int, int[]

see Example Code to help with compile problems

please advise

Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Experts Exchange Solution brought to you by

Enjoy your complimentary solution view.

Get every solution instantly with premium.
Start your 7-day free trial.

public int sum2(int[] nums) {

int numsSum[]=new int[4];

if(nums.length>=2)

return nums[0]+nums[1];

else (nums.length==1)

return nums[0];

}

why i got below error

Compile problems:

Error: else (nums.length==1)

^

Syntax error, insert "AssignmentOperator ArrayInitializer" to complete ArrayInitializerAssignemen

what it means?

please advise