-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinSearch.java
More file actions
86 lines (77 loc) · 2.48 KB
/
LinSearch.java
File metadata and controls
86 lines (77 loc) · 2.48 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
import java.util.*;
public class LinSearch {
public static void main(String[] args) {
int[] arr = { 6, 9, 2, 5, 4, 15, 1, 7 };
int[][] arr2 = { { 0, 1, 2 }, { 2, 3, 4 }, { 5, 8, 9 } };
int t = 8;
int n = 0;
boolean found = false;
for (int i = 0; i < arr.length; i++) {
if (arr[i] == n) {
System.out.println(n + " is found at " + i);
found = true;
break;
}
}
if (!found) {
System.out.println("The element does not exist in the given array");
}
SearchString("Sana", 'n');
System.out.println(MinEl(arr));
System.out.println(MaxEl(arr));
System.out.println(Arrays.toString(twoDim(arr2, t)));
System.out.println(evenElements(arr2));
}
static void SearchString(String str, char j) {
boolean found = false;
for (int i = 0; i < str.length(); i++) {
if (str.charAt(i) == j) {
System.out.println("Character " + j + " found at index " + i);
found = true;
break;
}
}
if (!found) {
System.out.println("Character not found in the string");
}
}
static int MinEl(int[] arr) {
int min = Integer.MAX_VALUE;
for (int i = 0; i < arr.length; i++) {
if (arr[i] < min) {
min = Math.min(arr[i], min);
}
}
return min;
}
static int MaxEl(int[] arr) {
int max = Integer.MIN_VALUE;
for (int i = 0; i < arr.length; i++) {
if (arr[i] > max) {
max = Math.max(max, arr[i]);
}
}
return max;
}
static int[] twoDim(int[][] arr2, int t) {
for (int i = 0; i < arr2.length; i++) {
for (int j = 0; j < arr2[0].length; j++) {
if (arr2[i][j] == t) {
return new int[] { i, j };
}
}
}
return new int[] { -1, -1 };
}
static ArrayList<Integer> evenElements(int[][] arr2) {
ArrayList<Integer> list = new ArrayList<>();
for (int i = 0; i < arr2.length; i++) {
for (int j = 0; j < arr2[0].length; j++) {
if (arr2[i][j] % 2 == 0) {
list.add(arr2[i][j]);
}
}
}
return list;
}
}