array sum2 challenge

Hi,

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;
}

Open in new window



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	    

Open in new window



i failed few tests. How to fix and improve my code. please advise
LVL 7
gudii9Asked:
Who is Participating?
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.

gudii9Author Commented:
i wonder when i write like 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];
 
 
 
}

why i got below error

Compile problems:


Error:      else (nums.length==1)
                          ^
Syntax error, insert "AssignmentOperator ArrayInitializer" to complete ArrayInitializerAssignement



what it means?
please advise
0
gudii9Author Commented:
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;
 
  
}

Open in new window


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
0
ozoCommented:
return (nums.length>0?nums[0]:0)+(nums.length>1?nums[1]:0);
0

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
Ultimate Tool Kit for Technology Solution Provider

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.

krakatoaCommented:
public int sum2(int[] nums) {


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


}

Open in new window

0
krakatoaCommented:
This is how I would do the ternary option :

public int sum2(int[] nums) {

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


}

Open in new window

0
awking00Commented:
You could also do it this way -
return nums.length==0?0:nums.length==1?nums[0]:nums[0]+nums[1;]
0
ozoCommented:
return nums.length<1?0:nums[0]+(nums.length<2?0:nums[1]);
0
gudii9Author Commented:
return (nums.length>0?nums[0]:0)+(nums.length>1?nums[1]:0);






return nums.length>1?nums[0]+nums[1]:nums.length>0?nums[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]+(nums.length<2?0:nums[1]);(//if length less than 1 (true say 0 ) then return nums[0] which is fine if it false what is happening in nums[0]+(nums.length<2?0:nums[1]??
please advise
0
ozoCommented:
(nums.length>1?nums[1]:0);
is nums[1] when nums.length>1, otherwise, it is 0

nums[1;]
should have been
nums[1];
0
awking00Commented:
>>return (nums.length>0?nums[0]:0)+(nums.length>1?nums[1]:0);

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

return nums.length==0?0:nums.length==1?nums[0]:nums[0]+nums[1]; ==> put ";" outside the brackets

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

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.
0
gudii9Author Commented:
return (nums.length>0?nums[0]:0)+(nums.length>1?nums[1]:0);//// >0 should cover >1 case already in the front right???

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

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.
0
ozoCommented:
return (nums.length>0?nums[0]:0)+(nums.length>1?nums[1]:0);//// >0 should cover >1 case already in the front right???
>0 covers the nums[0] part of the >1 case result


return nums.length>1?nums[0]+nums[1]:nums.length>0?nums[0]:0;
return nums.length>1?(
    nums[0]+nums[1] // this is the >1 case
):(
    nums.length>0?nums[0]:0   // this is the not >1 case
);
0
gudii9Author Commented:
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
0
ozoCommented:
No, but it can be written as
  nums.length==0?0:nums[0]
(because we know that nums.length is never < 0)
0
gudii9Author Commented:
public int sum2(int[] nums) {

return (nums.length==0?0:nums[0])+(nums.length>1?nums[1]:0);
 
  
}

Open in new window


above worked fine
0
gudii9Author Commented:
public int sum2(int[] nums) {

return (nums.length==0?0:nums[0])+(nums.length>=1?nums[1]:0);
 
  
}

Open in new window


why above code failing when i change from
(nums.length>1
to
(nums.length>=1

please advise

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

in above working solution where we are taking care when nums.length is 1 case?
0
gudii9Author Commented:
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]);

Open in new window


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
0
awking00Commented:
>>why above code failing when i change from
 (nums.length>1
 to
 (nums.length>=1<<
Take a look at what this says
return (nums.length==0?0:nums[0])+(nums.length>1?nums[1]: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])+(nums.length>=1?nums[1]: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.
0
krakatoaCommented:
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.
0
ozoCommented:
return (nums.length==0?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 first term becomes nums[0]
When nums.length == 1,  nums.length>0 is false, so the second term becomes 0
nums[0]+0 is the required result
0
ozoCommented:
//return nums.length<1?0:nums[0]+(nums.length<2?0:nums[1]);
return
          nums.length<1
          ?
              0
          :
              (
                    nums[0]
                +
                    (nums.length<2?0:nums[1])
             )
          ;

//return nums.length==0?0:nums.length==1?nums[0]:nums[0]+nums[1];
return
        nums.length==0
        ?
           0
        :
           (
             nums.length==1
             ?
               nums[0]
             :
               (nums[0]+nums[1])
           )
         ;
0
gudii9Author Commented:
public int sum2(int[] nums) {


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


}
how this solution works when nums.length is 1. In this case it should return

return nums[0]+nums[1];

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

please advise
0
awking00Commented:
>>how this solution works when nums.length is 1. In this case it should return

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

No, it shouldn't, it should return just nums[0]
0
gudii9Author Commented:
//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]
         ;

Open in new window


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:(nums.length==1?nums[0]:nums[0]+nums)[1]

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

public int sum2(int[] nums) {


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


Compile problems:


Error:      return nums.length==0?0:(nums.length==1?nums[0]:nums[0]+nums)[1];
                                                      ^^^^^^^^^^^^
The operator + is undefined for the argument type(s) int, int[]


see Example Code to help with compile problems

please advise
0
ozoCommented:
return nums.length==0?0:(nums.length==1?nums[0]:nums[0]+nums[1]);
0
awking00Commented:
gudii9, Just in case you didn't notice what ozo pointed out, the parenthesis needs to be outside the nums[1]) and not in between nums and the opening bracket.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
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.