|
| 1 | +package com.kunal.arrays; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | + |
| 5 | +public class Find { |
| 6 | + public static void main(String[] args) { |
| 7 | + int[] arr = {2, 3, 1, 4, 4, 5}; |
| 8 | +// System.out.println(find(arr, 4, 0)); |
| 9 | +// System.out.println(findIndex(arr, 4, 0)); |
| 10 | +// System.out.println(findIndexLast(arr, 4, arr.length-1)); |
| 11 | +// findAllIndex(arr, 4, 0); |
| 12 | +// System.out.println(list); |
| 13 | + |
| 14 | +// ArrayList<Integer> list = new ArrayList<>(); |
| 15 | +// ArrayList<Integer> ans = findAllIndex(arr, 4, 0, list); |
| 16 | +// System.out.println(ans); |
| 17 | +// System.out.println(list); |
| 18 | + |
| 19 | + System.out.println(findAllIndex2(arr, 4, 0)); |
| 20 | + |
| 21 | + |
| 22 | + } |
| 23 | + |
| 24 | + static boolean find(int[] arr, int target, int index) { |
| 25 | + if (index == arr.length) { |
| 26 | + return false; |
| 27 | + } |
| 28 | + return arr[index] == target || find(arr, target, index + 1); |
| 29 | + } |
| 30 | + |
| 31 | + static int findIndex(int[] arr, int target, int index) { |
| 32 | + if (index == arr.length) { |
| 33 | + return -1; |
| 34 | + } |
| 35 | + if (arr[index] == target) { |
| 36 | + return index; |
| 37 | + } else { |
| 38 | + return findIndex(arr, target, index + 1); |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + static int findIndexLast(int[] arr, int target, int index) { |
| 43 | + if (index == -1) { |
| 44 | + return -1; |
| 45 | + } |
| 46 | + if (arr[index] == target) { |
| 47 | + return index; |
| 48 | + } else { |
| 49 | + return findIndexLast(arr, target, index - 1); |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + static ArrayList<Integer> list = new ArrayList<>(); |
| 54 | + static void findAllIndex(int[] arr, int target, int index) { |
| 55 | + if (index == arr.length) { |
| 56 | + return; |
| 57 | + } |
| 58 | + if (arr[index] == target) { |
| 59 | + list.add(index); |
| 60 | + } |
| 61 | + findAllIndex(arr, target, index + 1); |
| 62 | + } |
| 63 | + |
| 64 | + static ArrayList<Integer> findAllIndex(int[] arr, int target, int index, ArrayList<Integer> list) { |
| 65 | + if (index == arr.length) { |
| 66 | + return list; |
| 67 | + } |
| 68 | + if (arr[index] == target) { |
| 69 | + list.add(index); |
| 70 | + } |
| 71 | + return findAllIndex(arr, target, index + 1, list); |
| 72 | + } |
| 73 | + |
| 74 | + static ArrayList<Integer> findAllIndex2(int[] arr, int target, int index) { |
| 75 | + |
| 76 | + ArrayList<Integer> list = new ArrayList<>(); |
| 77 | + |
| 78 | + if (index == arr.length) { |
| 79 | + return list; |
| 80 | + } |
| 81 | + |
| 82 | + // this will contain answer for that function call only |
| 83 | + if (arr[index] == target) { |
| 84 | + list.add(index); |
| 85 | + } |
| 86 | + ArrayList<Integer> ansFromBelowCalls = findAllIndex2(arr, target, index + 1); |
| 87 | + |
| 88 | + list.addAll(ansFromBelowCalls); |
| 89 | + |
| 90 | + return list; |
| 91 | + } |
| 92 | + |
| 93 | + |
| 94 | +} |
0 commit comments