Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Array values times 10
- 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.
- Input
- On the first line you will be given the comma separated array. On the second line you will receive the index.
- Output
- On the only output line you should print the result.
- Sample tests
- Input
- 1,2,20
- 0
- Output
- true
- Input
- 3,30
- 0
- Output
- true
- import java.util.Scanner;
- public class ArraysContaining11 {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- String[] input = scanner.nextLine().split(",");
- int index = Integer.parseInt(scanner.nextLine());
- int[] numbers = new int[input.length];
- for (int i = 0; i < input.length; i++) {
- numbers[i] = Integer.parseInt(input[i]);
- }
- System.out.println(isEleven(numbers, index));
- }
- public static boolean isEleven (int[] numbers, int index){
- if(index<0 || index>numbers.length-2){
- return false;
- }
- if(numbers[index] * 10 == numbers[index + 1]){
- return true;
- }
- return isEleven(numbers, index+1);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment