array sum2 challenge

gudii9
gudii9 used Ask the Experts™
on
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
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Author

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

Author

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
Most Valuable Expert 2014
Top Expert 2015
Commented:
return (nums.length>0?nums[0]:0)+(nums.length>1?nums[1]:0);
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

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

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

awking00Information Technology Specialist

Commented:
You could also do it this way -
return nums.length==0?0:nums.length==1?nums[0]:nums[0]+nums[1;]
ozo
Most Valuable Expert 2014
Top Expert 2015

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

Author

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
ozo
Most Valuable Expert 2014
Top Expert 2015

Commented:
(nums.length>1?nums[1]:0);
is nums[1] when nums.length>1, otherwise, it is 0

nums[1;]
should have been
nums[1];
awking00Information Technology Specialist
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]; ==> 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.

Author

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.
ozo
Most Valuable Expert 2014
Top Expert 2015

Commented:
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
);

Author

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
ozo
Most Valuable Expert 2014
Top Expert 2015

Commented:
No, but it can be written as
  nums.length==0?0:nums[0]
(because we know that nums.length is never < 0)

Author

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

Author

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?

Author

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
awking00Information Technology Specialist

Commented:
>>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.
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.
ozo
Most Valuable Expert 2014
Top Expert 2015

Commented:
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
ozo
Most Valuable Expert 2014
Top Expert 2015

Commented:
//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])
           )
         ;

Author

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
awking00Information Technology Specialist

Commented:
>>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]

Author

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
ozo
Most Valuable Expert 2014
Top Expert 2015

Commented:
return nums.length==0?0:(nums.length==1?nums[0]:nums[0]+nums[1]);
awking00Information Technology Specialist

Commented:
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.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial