sunfresh

Array with 6

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