Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Arrays containing 11
- Given an array of ints, compute recursively the number of times that the value 11 appears in the array.
- 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,11
- 0
- Output
- 1
- Input
- 11,11
- 0
- Output
- 2
- 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 int isEleven(int[] numbers, int index){
- int count = 0;
- if(index<numbers.length){
- if(numbers[index] == 11){
- count++;
- return count + isEleven(numbers, index+1);
- }
- return count+isEleven(numbers, index+1);
- }
- return count;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment