Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Array with 6
- Given an array of ints, compute recursively if the array contains a 6.
- 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,6,4
- 0
- Output
- true
- Input
- 1,4
- 0
- Output
- false
- import java.util.Scanner;
- public class ArrayWith6 {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- String[] input = scanner.nextLine().split(",");
- int index = Integer.parseInt(scanner.nextLine());
- System.out.println(containsSix(input, index));
- }
- static boolean containsSix(String[] input, int index){
- if( index < 0 || index > input.length-1){ // index is out of bounds or not found
- return false;
- } else if(input[index].equals("6")){
- return true; // If we see it, return true.
- }
- return containsSix(input ,index + 1);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment