Skip to content

Commit b88f136

Browse files
recursion arrays
1 parent d348324 commit b88f136

4 files changed

Lines changed: 145 additions & 0 deletions

File tree

1.54 MB
Binary file not shown.
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
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+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.kunal.arrays;
2+
3+
public class RBS {
4+
public static void main(String[] args) {
5+
int[] arr = {5, 6, 7, 8, 9, 1, 2, 3};
6+
System.out.println(search(arr, 4, 0, arr.length - 1));
7+
}
8+
9+
static int search(int[] arr, int target, int s, int e) {
10+
if (s > e) {
11+
return -1;
12+
}
13+
14+
int m = s + (e-s) / 2;
15+
if (arr[m] == target) {
16+
return m;
17+
}
18+
19+
if (arr[s] <= arr[m]) {
20+
if (target >= arr[s] && target <= arr[m]) {
21+
return search(arr, target, s, m-1);
22+
} else {
23+
return search(arr, target, m+1, e);
24+
}
25+
}
26+
27+
if (target >= arr[m] && target <= arr[e]) {
28+
return search(arr, target, m+1, e);
29+
}
30+
31+
return search(arr, target, s, m-1);
32+
}
33+
34+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.kunal.arrays;
2+
3+
public class Sorted {
4+
public static void main(String[] args) {
5+
int[] arr = {1, 2, 3, 5, 16, 8};
6+
System.out.println(sorted(arr, 0));
7+
}
8+
9+
static boolean sorted(int[] arr, int index) {
10+
// base condition
11+
if (index == arr.length - 1) {
12+
return true;
13+
}
14+
15+
return arr[index] < arr[index + 1] && sorted(arr, index + 1);
16+
}
17+
}

0 commit comments

Comments
 (0)