-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayQuestions.java
More file actions
226 lines (202 loc) · 4.46 KB
/
Copy pathArrayQuestions.java
File metadata and controls
226 lines (202 loc) · 4.46 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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
public class ArrayQuestions {
public long maxProduct(int a[],int n)
{
long max=a[0],min=a[0],max_value=a[0];
for(int i=1;i<n;i++)
{
if(a[i]<0)
{
long temp=min;
min=max;
max=temp;
}
max=Math.max(a[i], max*a[i]);
min=Math.min(a[i], min*a[i]);
max_value=Math.max(max, max_value);
}
return max_value;
}
public int[][] merge(int[][] intervals) {
int j=0;
for(int i=0;i<intervals.length-1;i++)
{
if(intervals[i][1]>=intervals[i+1][0])
{
intervals[j][0]=intervals[i][0];
intervals[j][1]=intervals[i+1][1];
}
j++;
}
return intervals;
}
public int[] sortArrayWithZeroOne(int[] arr,int n )
{
int count0=0,count1=0,count2=0;
for(int i=0;i<n;i++)
{
if(arr[i]==0)
{
count0++;
}
else if(arr[i]==1)
{
count1++;
}
else
{
count2++;
}
}
int i=0;
while(count0>0)
{
arr[i]=0;
i++;
count0--;
}
while(count1>0)
{
arr[i]=1;
i++;
count1--;
}
while(count2>0)
{
arr[i]=2;
i++;
count2--;
}
return arr;
}
public static int kthSmallest(int[] arr, int l, int r, int k)
{
int temp =0;
for(int i=0;i<l;i++)
{
for(int j=0;j<l;j++)
{
if(arr[i]<arr[j])
{
temp = arr[j];
arr[j]=arr[i];
arr[i]=temp;
}
}
}
return arr[k-1];
}
public int[] oneSideNegative(int[] arr,int n)
{
int low=0,hi=n-1;
int a[]=new int[n];
for(int i=0;i<n;i++)
{
if(arr[i]<0)
{
a[low++]=arr[i];
}
else
{
a[hi--]=arr[i];
}
}
return a;
}
public void printArray(int arr[],int n)
{
for(int value:arr)
{
System.out.print(value+" ");
}
}
int maxSubarraySum(int arr[], int n){
int max=-1000000;
for(int i=0;i<n;i++)
{
int sum=0;
for(int k=0;k<=i;k++)
{
sum=arr[k]+sum;
}
if(sum>max){
max=sum;
System.out.println(max);
}
}
return max;
}
public static int doUnion(int a[], int n, int b[], int m)
{
int z=a[n-1],x=b[m-1],ans=0,count=0;
if(z>x)
{
ans=z;
}
else
{
ans=x;
}
int a1[]=new int[100*ans];
++a1[a[0]];
for(int i=1;i<n;i++)
{
if(a[i]!=a[i-1])
{
++a1[a[i]];
}
}
++a1[b[0]];
for(int i=1;i<m;i++)
{
if(b[i]!=b[i-1])
{
++a1[b[i]];
}
}
for(int i=0;i<100*ans;i++)
{
if(a1[i]>0)
{
count++;
}
}
return count;
}
static int minJumps(int[] arr){
if (arr.length <= 1)
return 0;
// Return -1 if not possible to jump
if (arr[0] == 0)
return -1;
// initialization
int maxReach = arr[0];
int step = arr[0];
int jump = 1;
// Start traversing array
for (int i = 1; i < arr.length; i++) {
// Check if we have reached
//the end of the array
if (i == arr.length - 1)
return jump;
// updating maxReach
maxReach = Math.max(maxReach, i + arr[i]);
// we use a step to get to the current index
step--;
// If no further steps left
if (step == 0) {
// we must have used a jump
jump++;
// Check if the current
//index/position or lesser index
// is the maximum reach point
//from the previous indexes
if (i >= maxReach)
return -1;
// re-initialize the steps to the amount
// of steps to reach maxReach from position i.
step = maxReach - i;
}
}
return -1;
}
}