x
• Status: Solved
• Priority: Medium
• Security: Public
• Views: 134

# 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
0
gudii9
• 5
• 3
1 Solution

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

Author Commented:
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?
0

Commented:
these are 3 different tests and not a sequence of recursive calls

array220([1, 2, 20], 0) → true  because 2 * 10 == 20  (3rd recursive call)

array220([3, 30], 0) → true      because3 * 10 == 30  (2nd recursive call)

array220([3], 0) → false           because the array only has 1 element.

so 1 *10 is 10 are we expected to see 10 in rest of elements

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

so you have to check whether the 1 was followed by 10 and if that is not the case whether you have more luck with the next pair.

Sara
0

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

above is clear. let me think
0

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

}
``````

above passed all tests. any improvements or alternate approaches?
0

Author Commented:
``````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.
*/
``````

above bit refined i think
0

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

}
``````

Sara
0

Author Commented:
sure. got it
0
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.

## Featured Post

• 5
• 3
Tackle projects and never again get stuck behind a technical roadblock.