Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a recursive method public static int findTimes10(int[] nums) that analyzes

ID: 3777619 • Letter: W

Question

Write a recursive method public static int findTimes10(int[] nums) that analyzes an array of integers and returns the index of the first value whose successor is 10 times the original. If no such value exists, the method returns -1.

Method Call Return Value

findTimes10(new int[]{1,5,3,5,50,2,4,6,60}) 3

findTimes10(new int[]{1,5,3,5,15,2,4,6,60}) 7

findTimes10(new int[]{1,5,3,5,15,2,4,6,5}) -1

Note: You are not permitted to use any loops whatsoever in writing this method! You may write helper methods if you like, but those methods must not contain any loops either.

Explanation / Answer

FindTimesTest.java


public class FindTimesTest {

  
   public static void main(String[] args) {
       System.out.println(findTimes10(new int[]{1,5,3,5,50,2,4,6,60}));
       System.out.println(findTimes10(new int[]{1,5,3,5,15,2,4,6,60}) );
       System.out.println(findTimes10(new int[]{1,5,3,5,15,2,4,6,5}) );
   }
   public static int findTimes10(int[] nums){
       return findTimes(nums, 0, nums.length);
   }
   public static int findTimes(int[] nums,int start, int n){
       if (n-1 == start){
           return -1;
       }
       else{
           if(nums[start] * 10 == nums[start+1]){
               return start;
           }
           else{
               return findTimes(nums, start+1, n);
           }
       }
   }
}

Output:

3
7
-1

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote