##
**Problem:**

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

##
**Solution:**

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

May I know if the following code has any problems?

ReplyDeletepublic static boolean array220(int[] nums, int index) {

if(index==nums.length)

return false;

for(int x=0; x<nums.length; x++) {

if(nums[x]==nums[index]*10)

return true;

}

return array220(nums, index+1);

}

hi, this solution is incorrect. try it on the following array - {2,5,20}. you will get false back

ReplyDeletethis only checks to see if the following number is a 10 times a multiple of the number not any number in the rest of the the array is a multiple

ReplyDeletewhy in eclipse when i passing 3,30,50,60 it says true?

ReplyDeletepublic boolean array220(int[] nums, int index) {

ReplyDeleteif (index+1 >= nums.length)

return false;

return (nums[index]*10 == nums[index+1] || array220(nums, index+1));

}

this question is wrong. It's not asking you that if the numbers in the array arithmetically nums[x]*10==nums[x+1]. It's saying that, "somewhere a value followed in the array by that value times 10". The correct solution should be this. public boolean array220(int[] nums, int index) {

ReplyDeleteArrays.sort(nums);

return helper(nums,index,nums.length-1);

}

public boolean helper(int[] nums,int l,int r){

if (l>=r){

return false;

}

if(nums[l]*10nums[r]){

return helper(nums,l,r-1);

} else{

return true;

}

}

static boolean array220(int[] nums, int index) {

ReplyDeleteif (index >= nums.length - 1)

return false;

for (int i = 0; i < nums.length; i++) {

for (int j = i + 1; j < nums.length; j++) {

if (nums[i] * 10 == nums[j]) {

return true;

}

}

}

return array220(nums, index + 1);

}

public boolean array220(int[] nums, int index) {

ReplyDeleteif (index >= nums.length - 1) return false;

return nums[index] * 10 == nums[index + 1] || array220(nums, index + 1);

}

So do we assume that num multiplies by 10 is right next to it

ReplyDelete