Java > Recursion-1 > array220 (CodingBat Solution)

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);
}


10 comments :

  1. May I know if the following code has any problems?

    public 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);

    }

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

    ReplyDelete
  3. this 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

    ReplyDelete
  4. why in eclipse when i passing 3,30,50,60 it says true?

    ReplyDelete
  5. public boolean array220(int[] nums, int index) {
    if (index+1 >= nums.length)
    return false;

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

    ReplyDelete
  6. 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) {
    Arrays.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;
    }

    }

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

    if (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);

    }

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

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

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

    }

    ReplyDelete

Follow Me

If you like our content, feel free to follow me to stay updated.

Subscribe

Enter your email address:

We hate spam as much as you do.

Upload Material

Got an exam, project, tutorial video, exercise, solutions, unsolved problem, question, solution manual? We are open to any coding material. Why not upload?

Upload

Copyright © 2012 - 2014 Java Problems  --  About  --  Attribution  --  Privacy Policy  --  Terms of Use  --  Contact