Avatar of gudii9
gudii9
Flag for United States of America asked on

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
JavaProgramming Languages-OtherProgramming

Avatar of undefined
Last Comment
awking00

8/22/2022 - Mon
gudii9

ASKER
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
gudii9

ASKER
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
ASKER CERTIFIED SOLUTION
ozo

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
SOLUTION
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
krakatoa

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

Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
awking00

You could also do it this way -
return nums.length==0?0:nums.length==1?nums[0]:nums[0]+nums[1;]
ozo

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

ASKER
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
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
ozo

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

nums[1;]
should have been
nums[1];
SOLUTION
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
gudii9

ASKER
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

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
);
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
gudii9

ASKER
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

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

ASKER
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
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
gudii9

ASKER
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?
gudii9

ASKER
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
awking00

>>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.
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
krakatoa

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

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

//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])
           )
         ;
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
gudii9

ASKER
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
awking00

>>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]
gudii9

ASKER
//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
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
ozo

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

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.