Link to home
Start Free TrialLog in
Avatar of gudii9
gudii9Flag for United States of America

asked on

fix23 challenge

Hi,

i am trying below challenge

http://codingbat.com/prob/p120347

i wrote as below

public int[] fix23(int[] nums) {

int numsNew[]=new int[3];
numsNew=nums;
numsNew[1]=0;
  if(nums[1]==nums[2])
  return numsNew;
  else 
  return nums;
}

Open in new window


i am failing below
xpected      Run            
fix23({1, 2, 3}) → {1, 2, 0}      {1, 0, 3}      X         
fix23({2, 3, 5}) → {2, 0, 5}      {2, 0, 5}      OK         
fix23({1, 2, 1}) → {1, 2, 1}      {1, 0, 1}      X         
fix23({3, 2, 1}) → {3, 2, 1}      {3, 0, 1}      X         
fix23({2, 2, 3}) → {2, 2, 0}      {2, 0, 3}      X         
fix23({2, 3, 3}) → {2, 0, 3}      {2, 0, 3}      OK         
other tests

how to improve and fix my code. please advise
Avatar of ozo
ozo
Flag of United States of America image

for( int i=1;i<3;++i ){
    if( nums[i-1]==2&&nums[ i ]==3 ){ nums[ i ]=0; }
  }
  return nums;
ASKER CERTIFIED SOLUTION
Avatar of ozo
ozo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of gudii9

ASKER

public int[] fix23(int[] nums) {

int numsNew[]=new int[3];
int len =nums.length;
numsNew=nums;
for(int i=0;i<=len-2;i++)
{
if((nums[i]==2)&(nums[i+1]==3))
{
   numsNew[i+1]=0;
  }
  }
  return numsNew;
}

Open in new window


i wrote as below and passed all tests.

Expected      Run            
fix23({1, 2, 3}) → {1, 2, 0}      {1, 2, 0}      OK         
fix23({2, 3, 5}) → {2, 0, 5}      {2, 0, 5}      OK         
fix23({1, 2, 1}) → {1, 2, 1}      {1, 2, 1}      OK         
fix23({3, 2, 1}) → {3, 2, 1}      {3, 2, 1}      OK         
fix23({2, 2, 3}) → {2, 2, 0}      {2, 2, 0}      OK         
fix23({2, 3, 3}) → {2, 0, 3}      {2, 0, 3}      OK         
other tests
OK         


Does below supposed to pass?
fix23({3, 2, 4}) → {0, 2, 4}      {0,2, 4}      OK
Does below supposed to pass?

No.
Avatar of gudii9

ASKER

No.
Given an int array length 3, if there is a 2 in the array immediately followed by a 3, set the 3 element to 0. Return the changed array.

is it because 2 followed 3 means  always 3 should come afgter 2 not otherway(3 should come before 2 still that is following right??sorry english is not my first language??)
Avatar of gudii9

ASKER

1 a: to go or come after or behind (someone or something)

i saw the link it says after or behind both??
please advise
In this context:
{2    ,     3}
^           ^       
before      after
^           ^
front       back
mean the same thing

Open in new window

Avatar of gudii9

ASKER

got it. So 2 should come before then 3 should come after (that is like saying 2 in the array immediately followed by a 3
that is why below came like this without replacing 3 with 0
fix23({3, 2, 1}) → {3, 2, 1}      {3, 2, 1}

)

now i take follow means after to make it easy