sunfresh

Array values times 10

Jan 20th, 2023
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.40 KB | None | 0 0
  1. Array values times 10
  2. Given an array of ints, compute recursively if the array contains somewhere a value followed in the array by that value times 10.
  3. 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.
  4.  
  5. Input
  6. On the first line you will be given the comma separated array. On the second line you will receive the index.
  7.  
  8. Output
  9. On the only output line you should print the result.
  10.  
  11. Sample tests
  12. Input
  13. 1,2,20
  14. 0
  15. Output
  16. true
  17. Input
  18. 3,30
  19. 0
  20. Output
  21. true
  22.  
  23. import java.util.Scanner;
  24. public class ArraysContaining11 {
  25.  
  26. public static void main(String[] args) {
  27. Scanner scanner = new Scanner(System.in);
  28. String[] input = scanner.nextLine().split(",");
  29. int index = Integer.parseInt(scanner.nextLine());
  30. int[] numbers = new int[input.length];
  31.  
  32. for (int i = 0; i < input.length; i++) {
  33. numbers[i] = Integer.parseInt(input[i]);
  34. }
  35. System.out.println(isEleven(numbers, index));
  36. }
  37.  
  38. public static boolean isEleven (int[] numbers, int index){
  39. if(index<0 || index>numbers.length-2){
  40. return false;
  41. }
  42. if(numbers[index] * 10 == numbers[index + 1]){
  43. return true;
  44. }
  45. return isEleven(numbers, index+1);
  46. }
  47. }
Advertisement
Add Comment
Please, Sign In to add comment