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

asked on

array220 challenge

Hi,
I am working on below challenge and not cclear on description. please advise

http://codingbat.com/prob/p173469


Recursion-1 > array220
prev  |  next  |  chance
Given an array of ints, compute recursively if the array contains somewhere a value followed in the array by that value times 10. We'll use the convention of considering only the part of the array that begins at the given index. In this way, a recursive call can pass index+1 to move down the array. The initial call will pass in index as 0.

array220([1, 2, 20], 0) → true
array220([3, 30], 0) → true
array220([3], 0) → false
Avatar of sarabande
sarabande
Flag of Luxembourg image

- recursion stops with false if the index is greater or equal to array length.
- if the index is greater than 0 and nums[index] is equal to 10 times nums[index-1] then stop with success.
- proceed recursion by calling array220 passing the array and the index incremented by 1 and return the result.

Sara
Avatar of gudii9

ASKER

array220([1, 2, 20], 0) → true
array220([3, 30], 0) → true
array220([3], 0) → false

how above is true and true then false?

array220([1, 2, 20], 0) → true

element at 0 index of above array is 1 so 1 *10 is 10 are we expected to see 10 in rest of elements and if found return true else false?
ASKER CERTIFIED SOLUTION
Avatar of sarabande
sarabande
Flag of Luxembourg 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
Avatar of gudii9

ASKER

the challenge is to find a pair of consecutive numbers where (left * 10 == right).

above is clear. let me think
Avatar of gudii9

ASKER

public boolean array220(int[] nums, int index) {
  if(index>nums.length-2){
    return false;
  }
if(nums[index+1]==nums[index]*10){
  return true;
}
else{
  return array220(nums,index+1);
}

}

Open in new window


above passed all tests. any improvements or alternate approaches?
Avatar of gudii9

ASKER

public boolean array220(int[] nums, int index) {
  if(index>nums.length-2){
    return false;
  }
if(nums[index]*10==nums[index+1]){
  return true;
}
else{
  return array220(nums,index+1);
}

}


/*- recursion stops with false if the index is greater or equal to array length.
- if the index is greater than 0 and nums[index] is equal to 10 times nums[index-1] 
then stop with success.
- proceed recursion by calling array220 passing the array and the index incremented 
by 1 and return the result.
*/

Open in new window


above bit refined i think
any improvements or alternate approaches?

the 'index>nums.length-2' looks a little bit clumsy.

normally, if a range of indices needs to be handled i prefer to skip the index 0 and then can use nums[index-1] and nums[index].

public boolean array220(int[] nums, int index) {
  if(index>=nums.length){
    return false;
  }
if(index > 0 && nums[index-1]*10==nums[index]){
  return true;
}
return array220(nums,index+1);

}

Open in new window


Sara
Avatar of gudii9

ASKER

sure. got it