-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patha23linearSearch.java
More file actions
184 lines (164 loc) · 5.87 KB
/
a23linearSearch.java
File metadata and controls
184 lines (164 loc) · 5.87 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
public class a23linearSearch {
public static void main(String[] args) {
int arr[] = {1,2,3,4,5,2,6,7,2,8};
linearSearch(arr,4);
// finding first and last occurence of a no if no repeats multiple times.
FLlinearSearch(arr, 2, false);
FLlinearSearch(arr, 2, true);
//find multiple occurences of a no.
multiLinearSearch(arr,2);
// <------------------------2D Array--------------------------------->
int arr2D[][] = {
{2,4,1,5,1},
{6,7,3,4,2},
{8,1,2,2,3},
{5,5,7,8,4}
}; // similar to arr2D[4][5] -> 4 1D arrays and each 1D array size is 5
linearSearch2D(arr2D, 4);
// finding first and last occurence of a no if no repeats multiple times.
FLlinearSearch2D(arr2D, 4, false);
FLlinearSearch2D(arr2D, 4, true);
linearSearchMultiple2d(arr2D,4);
findMaximumSumKiSubarray(arr2D);
}
static void linearSearch(int arr[], int target){
int ans = -1;
for(int i=0; i<arr.length; i++){
if (arr[i]==target) {
ans = i;
break; // This breaks out of the nearest loop(for loop)
}
}
if (ans == -1) {
System.out.println("Element not present.");
} else{
System.out.println("Element fount at index: "+ans);
}
}
static void FLlinearSearch(int arr[], int target, boolean islast){
int ans = -1;
for(int i=0; i<arr.length; i++){
if (arr[i]==target) {
ans = i;
if (islast == false) {
break;
}
}
}
if (ans == -1) {
System.out.println("Element not present.");
} else{
System.out.println("Element fount at index: "+ans);
}
}
public static void multiLinearSearch(int arr[], int target){
int ans[] = new int[arr.length]; // bcz in worst case all elements can be same.
int k = 0;
for(int i=0; i<arr.length; i++){
if (arr[i]==target) {
ans[k] = i;
k++;
}
}
if (k==0) {
System.out.println("Element not present in the array.");
} else {
for(int i=0; i<k; i++)
System.out.println("Element fount at index: "+ans[i]);
}
}
static void linearSearch2D(int arr[][], int target){
int outerIndex = -1;
int innerIndex = -1;
boolean found = false;
for(int i=0; i<arr.length; i++){
for(int j=0; j<arr[i].length; j++){
if (arr[i][j]==target) {
outerIndex = i;
innerIndex = j;
found = true;
break;
}
}
if (found==true) {
break;
}
}
// or if(outerIndex == -1 && innerIndex == -1) both are same bcz if value of outerindex changes then innnerindex value also changes
if(outerIndex == -1) {
System.out.println("Element is not present in the array");
} else {
System.out.println("Found element at index: "+ outerIndex + "," + innerIndex);
}
}
static void FLlinearSearch2D(int arr[][], int target, boolean islast){
int outerIndex = -1;
int innerIndex = -1;
boolean found = false;
for(int i=0; i<arr.length; i++){
for(int j=0; j<arr[i].length; j++){
if (arr[i][j]==target) {
outerIndex = i;
innerIndex = j;
if (islast == false) {
found = true;
break;
}
}
}
if (found==true) {
break;
}
}
// or if(outerIndex == -1 && innerIndex == -1) both are same bcz if value of outerindex changes then innnerindex value also changes
if(outerIndex == -1) {
System.out.println("Element is not present in the array");
} else {
System.out.println("Found element at index: "+ outerIndex + "," + innerIndex);
}
}
public static void linearSearchMultiple2d(int arr[][], int target){
// int size = arr.length * arr[0].length; only works for 2d array
int size = 0; // works for 2d as well as jagged array.
for(int i=0;i<arr.length;i++){
size = size + arr[i].length;
}
int ans[][] = new int[size][2];
int k=0;
for(int i=0;i<arr.length;i++){
boolean found = false;
for(int j=0;j<arr[i].length;j++){
if(target == arr[i][j]){
ans[k][0] = i;
ans[k][1] = j;
k++;
}
}
if(found == true){
break;
}
}
if(k == 0) {
System.out.println("Element is not present in the array");
} else {
for(int i=0;i<k;i++) {
System.out.println("Found element at pos: " + ans[i][0] + "," + ans[i][1]);
}
}
}
static void findMaximumSumKiSubarray(int arr[][]){
int max = Integer.MIN_VALUE;
int resIndex = -1;
for(int i=0; i<arr.length; i++){
int sum = 0;
for(int j=0; j<arr[i].length; j++){
sum = sum + arr[i][j];
}
if (sum>max) {
max = sum;
resIndex = i;
}
}
System.out.println("Maximum sum subarray is: "+ max +" , for the index: "+ resIndex);
}
}