-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrays_Practice.java
More file actions
147 lines (122 loc) · 3.27 KB
/
Arrays_Practice.java
File metadata and controls
147 lines (122 loc) · 3.27 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
// find whether element x is present in the given array. Return the index of the first occurrence of x in the array, or -1 if it doesn't exist.
class Solution {
static int search(int arr[], int x) {
int res=0;
if(arr.length<1 ) res=-1;
for(int i=0; i<arr.length; i++){
if(arr[i]==x) {
res=i;
break;
}else res=-1;
}
return res;
}
}
// find the minimum and maximum elements in the array. Returning a Pair that contains two elements the first one will be a minimum element and the second will be a maximum.
class Pair<K, V> {
private final K key;
private final V value;
public Pair(K key, V value) {
this.key = key;
this.value = value;
}
public K getKey() { return key; }
public V getValue() { return value; }
}
class Solution {
public Pair<Integer, Integer> getMinMax(int[] arr) {
int min=arr[0];
int max=arr[0];
for(int i : arr){
if(i<min) min=i;
if(i>max) max=i;
}
return new Pair<>(min, max);
}
}
// Fetching Second largest element in an array.
public static int secMax(int[] arr) {
if (arr.length < 2)
return -1;
int max = 0;
int secMax = 0;
for (int i = 0; i < arr.length; i++) {
int n1 = arr[i];
if (n1 > max) {
secMax = max;
max = n1;
} else if (max == secMax || i > secMax) {
if (n1 != max)
secMax = n1;
}
}
return secMax;
}
// Given an array of integers, return indices of the two numbers such that they add up to a specific target.
public static void checkTarget(int arr[], int target) {
int sum = 0;
for (int i = 0; i < arr.length; i++) {
for (int j = 1; j < arr.length; j++) {
sum = arr[i] + arr[j];
if (sum == target) {
System.out.println(i + " + " + j + " = " + sum);
}
}
}
}
// Moveing all zeros to the end of an array while maintaining the ascending order of non-zero elements.
public static int[] reArrange(int arr[]) {
int index = 0;
for (int i = 0; i < arr.length; i++) {
if (arr[i] != 0) {
arr[index++] = arr[i];
}
}
for (int i=0; i<index-1; i++) {
for(int j=0; j<index-i-1; j++) {
if(arr[j]>arr[j+1]) {
int temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
while (index < arr.length) {
arr[index++] = 0;
}
return arr;
}
// Left Rotation an array by k positions.
public static int[] leftRotation(int[] arr, int k) {
int res[] = new int[arr.length];
int m = 0;
for (int i = k; i < arr.length; i++) {
res[m++] = arr[i];
}
for (int i = 0; i < k; i++) {
res[m++] = arr[i];
}
return res;
}
// Right Rotation an array by k positions.
public static int[] rightRotation(int[] arr, int k) {
int res[] = new int[arr.length];
int m = 0;
for (int i = arr.length - k; i < arr.length; i++) {
res[m++] = arr[i];
}
for (int i = 0; i < arr.length - k; i++) {
res[m++] = arr[i];
}
return res;
}
// Finding the missing number in an array of size n containing numbers from 1 to n.
public static int missingLetter(int arr[]) {
int n = arr[arr.length - 1];
int expectedSum = n * (n + 1) / 2;
int sum = 0;
for (int i : arr) {
sum = sum + i;
}
return expectedSum - sum;
}