-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRecursion_Arrays.java
More file actions
67 lines (65 loc) · 2.17 KB
/
Recursion_Arrays.java
File metadata and controls
67 lines (65 loc) · 2.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import java.util.ArrayList;
public class Recursion_Arrays {
public static void main(String[] args) {
int[] arr={1,2,3,4,5,6,7,8};
int index=0;
System.out.println(Sort(arr,index));
System.out.println(Search(arr,8,index));
System.out.println(SearchIndex(arr,7,index));
int[] arr2={1,2,4,4,5,4,6,7};
SearchIndexes(arr2,4,index);
System.out.println(list);
System.out.println("---------------------------");
ArrayList<Integer> list=SearchIndexeslist(arr2,4,index,new ArrayList<>());
System.out.println(list);
// or
// ArrayList<Integer> example=new ArrayList<>();
// ArrayList<Integer> called=SearchIndexeslist(arr2,4,index,example);
// System.out.println(called);
// System.out.println(example);
// Both called and example will have same value after excution
}
public static boolean Sort(int[] arr,int index){
if(index==arr.length-1){
return true;
}
return arr[index]<arr[index+1] && Sort(arr,index+1);
}
public static boolean Search(int[] arr,int target,int index){
if(index == arr.length){
return false;
}
return arr[index] == target || Search(arr,target,index+1);
}
// To find index
public static int SearchIndex(int[] arr,int target,int index){
if(index==arr.length){
return -1;
}
if(arr[index]==target){
return index;
}
else{
return SearchIndex(arr,target,index+1);
}
}
static ArrayList<Integer> list=new ArrayList<>();
public static void SearchIndexes(int[] arr,int target,int index){
if(index==arr.length){
return ;
}
if(arr[index]==target){
list.add(index);
}
SearchIndexes(arr,target,index+1);
}
public static ArrayList<Integer> SearchIndexeslist(int[] arr,int target,int index,ArrayList<Integer> list){
if(index==arr.length){
return list;
}
if(arr[index]==target){
list.add(index);
}
return SearchIndexeslist(arr,target,index+1,list);
}
}