-
Notifications
You must be signed in to change notification settings - Fork 0
/
Questions_group1
587 lines (455 loc) · 13.9 KB
/
Questions_group1
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
public class InterviewQuestions1 {
// JAVA Code for Search in a row wise and
// column wise sorted matrix
/* Searches the element x in mat[][]. If the
element is found, then prints its position
and returns true, otherwise prints "not found"
and returns false */
public static void search(int[][] mat, int n, int x)
{
int i = 0, j = n - 1; // set indexes for top right
// element
while (i < n && j >= 0) {
if (mat[i][j] == x) {
System.out.print("n Found at " + i + " " + j);
return;
}
if (mat[i][j] > x)
j--;
else // if mat[i][j] < x
i++;
}
System.out.print("n Element not found");
return; // if ( i==n || j== -1 )
}
public static int maxDiffBetweenVAlues(int[] arr) {
int max_diff = arr[1] - arr[0];
int min_element = arr[0];
int i;
for (i = 1; i < arr.length; i++)
{
if (arr[i] - min_element > max_diff)
max_diff = arr[i] - min_element;
if (arr[i] < min_element)
min_element = arr[i];
}
return max_diff;
}
//חיפוש 3 איברים ממשיים שסכומם z במערך ממוין A (O(n^2))
//we serch for x+y+z = w
//for every z we will look for a pair x,y such as x+y = w-z that are not z itself
//x->l (left index) y-> r (right)
public static int[] threeSum(int[]A ,int sum) {
int l=0,r=0,z=0;
/* Sort the elements */
Arrays.sort(A); //O(nlogn)
/* Now look for the two candidates
in the sorted array*/
l = 0;
r = A.length - 1;
for(z=0; z<A.length; z++) //O(n) * O(n) of the while loop =>O(n^2)
{
while (l < r)
{
if(l==z) //skip z
l++;
else if(r==z) //skip z
r++;
else
{
if (A[l] + A[r] == sum - A[z])
return new int[]{A[l],A[r],A[z]};
else if (A[l] + A[r] < sum)
l++;
else // A[i] + A[j] > sum
r--;
}
}
}
return null;
}
//using sorting-> O(nlogn) time, O(1) space.
public static int mostFrequent(int arr[])
{
//say at least 1 element, otherwise we can return -1 or MinInteger if needed
int currentMax=0, finalMax=0, finalNumber=arr[0];
Arrays.sort(arr);
for(int i=0; i<arr.length-1; i++)
{
if(arr[i]==arr[i+1])
currentMax++;
else {
if(currentMax>finalMax)
{
finalMax=currentMax;
finalNumber = arr[i];
}
currentMax=0;
}
}
return finalNumber;
}
//using hashing -> O(n) time and space
public static int mostFrequentHashed(int arr[])
{
// Insert all elements in hash
Map<Integer, Integer> hp =
new HashMap<Integer, Integer>();
for(int i = 0; i < arr.length; i++)
{
int key = arr[i];
if(hp.containsKey(key))
{
int freq = hp.get(key);
freq++;
hp.put(key, freq);
}
else
{
hp.put(key, 1);
}
}
// find max frequency.
int max_count = 0, res = -1;
for(Entry<Integer, Integer> val : hp.entrySet())
{
if (max_count < val.getValue())
{
res = val.getKey();
max_count = val.getValue();
}
}
return res;
}
public static int[] commonElem(int[] arr1, int[] arr2) {
ArrayList<Integer> temp = new ArrayList<Integer>(0);
int index1=0, index2=0;
while(index1<arr1.length && index2<arr2.length)
{
if(arr1[index1]>arr2[index2])
index2++;
else if(arr1[index1]<arr2[index2])
index1++;
else //equals
{
temp.add(arr1[index1++]);
index2++;
}
}
int[] arrTemp = new int[temp.size()];
for(int i=0; i<arrTemp.length ; i++)
arrTemp[i] = temp.remove(0);
return arrTemp;
}
public static boolean isRotationOf(int[] arr1 , int[] arr2) {
//Check length of arrays
if(arr1.length != arr2.length)
return false;
int index1=0,index2=0, indexToReach=0, startingIndex=0;
for( ; index1<arr1.length ; index1++)
{
if(arr1[index1] == arr2[index2])
{
startingIndex = index1;
while(index1<arr1.length && index2<arr2.length && arr1[index1] == arr2[index2])
{
index1++;index2++;
}
if(index1==arr1.length)
{
// System.out.println("\n" + index1 + "\t" + index2);
indexToReach = arr1.length - index2;
index1=0;
while(index1<indexToReach && index2<arr2.length && arr1[index1] == arr2[index2])
{
index1++;index2++;
}
if(index2 == arr2.length)
return true;
}
index1=startingIndex;
index2=0;
}
}
return false;
}
/*another approache is to duplicate the second and search for the first as a sequence in the other
public static boolean isRotationOf2(int[] arr1 , int[] arr2) {
int[] temp = new int[arr2.length*2];
for(int i=0 ; i<arr2.length; i++)
temp[i] = arr2[i];
for(int i=0 ; i<arr2.length; i++)
temp[arr2.length+i] = arr2[i];
int index1=0, index2=0;
while(index1<arr1.length)
{
while(arr1[index1] == temp[index2])
index1++;
}
}
*/
// Definition for a Node.
public static class Node {
int val;
Node next;
Node random;
public Node(int val) {
this.val = val;
this.next = null;
this.random = null;
}
}
public static Node copyRandomList(Node head) {
if(head==null){
return null;
}
// Space O(1)
Node current = head;
while(current!=null){
// insert the new copied node to original linked list
Node next = current.next;
current.next = new Node(current.val);
current.next.next = next;
current = next;
}
current = head;
while(current!=null){
if(current.random!=null){
current.next.random = current.random.next;
}
current = current.next.next;
}
current = head;
Node res = head.next;
while(current!=null){
// eg: A->A'->B->B'...
// next = B
Node next = current.next.next;
if(next!=null){
// A'->B' ...
current.next.next = next.next;
}
// A->B ...
current.next = next;
current = next;
}
return res;
}
public static int mul(int a, int b) {
if(a<b)
return mulAid(b,a);
else return mulAid(a,b);
}
public static int mulAid(int a, int b) {
if(b==0)
return 0;
return a + mulAid(a,b-1);
}
public static boolean patternInsString(String s,String p) {
char[] charArrayS = new char[s.length()];
charArrayS = s.toCharArray();
char[] charArrayP = new char[p.length()];
charArrayP = p.toCharArray();
for(int sIndex=0, pIndex=0; sIndex<s.length() ; sIndex++)
{
pIndex=0;
while(
( charArrayS[sIndex] == charArrayP[pIndex] ) ||
(charArrayS[pIndex] == '.' ) ||
( charArrayS[pIndex] == '*' && charArrayS[sIndex] == '0' ) ||
( charArrayS[pIndex] == '*' && sIndex>0 && charArrayS[sIndex] == charArrayS[sIndex-1] )
)
{
sIndex++; pIndex++;
if(pIndex==charArrayP.length)
return true;
}
}
return false;
}
public static boolean isMatch(String s, String p) {
char[] charArrayS = new char[s.length()];
charArrayS = s.toCharArray();
char[] charArrayP = new char[p.length()];
charArrayP = p.toCharArray();
int sIndex=0, pIndex=0;
if(p.length()>s.length() )
{
pIndex+=p.length()-s.length();
char[] temp = new char[s.length()];
for(int i=0 ; i<s.length() ; i++)
{
temp[i] = charArrayP[i+pIndex];
}
Map<Character, Integer> stringHash = new HashMap<Character, Integer>();
for (char ch : s.toCharArray())
stringHash.put(ch, stringHash.containsKey(ch) ? (stringHash.get(ch) + 1) : 1);
Map<Character, Integer> pHash = new HashMap<Character, Integer>();
for (char ch : s.toCharArray())
pHash.put(ch, pHash.containsKey(ch) ? (pHash.get(ch) + 1) : 1);
}
while(
( sIndex<charArrayS.length && pIndex<charArrayP.length ) &&
(
( charArrayS[sIndex] == charArrayP[pIndex] ) ||
(charArrayP[pIndex] == '.' ) ||
( charArrayP[pIndex] == '*' && charArrayS[sIndex] == '0' ) ||
( charArrayP[pIndex] == '*' && sIndex>0 && charArrayS[sIndex] == charArrayP[pIndex-1] )
)
)
{
sIndex++; pIndex++;
if(pIndex==charArrayP.length && sIndex == charArrayS.length)
return true;
}
return false;
}
public static int mutants(int N , int K, int[] arr) {
if(N!=arr.length || N%K!=0)
return -1;
Arrays.sort(arr);
int numOfLists = N/K;
List<int[]> intList = new ArrayList<int[]>(0);
ArrayList<Integer>[] resultLists = new ArrayList[numOfLists];
// initializing
for (int i = 0; i < numOfLists; i++) {
resultLists[i] = new ArrayList<Integer>(0);
}
intList.add(new int[2]);
intList.get(0)[0]=arr[0];
intList.get(0)[1]=1;
System.out.println();
for (int i=1; i<arr.length ; i++)
{
System.out.print(arr[i] + "\t");
if(intList.get(intList.size()-1)[0] != arr[i])
{
int[] temp = new int[2];
temp[0]=arr[i];
temp[1] =1;
intList.add(temp);
}else {
intList.get(intList.size()-1)[1]++;
}
}
System.out.println("\n\n");
Collections.sort(intList, (i1, i2) -> Integer.compare(i1[0], i2[0]));
Collections.sort(intList, (i1, i2) -> Integer.compare(i1[1], i2[1]));
Collections.reverse(intList);
for(int[] mutant : intList)
if(mutant[1]>numOfLists)
return -1;
int listIndex=0;
int side=0;
while(!intList.isEmpty()) {
{
if(side%2==0)
{
int mutant=0;
System.out.println("\nZUGI: \n" + intList.get(mutant)[0] );
while(resultLists[listIndex].size()<K)
{
if(intList.get(mutant)[1]==0)
intList.remove(mutant);
else {
System.out.print(intList.get(mutant)[0] + "\t");
resultLists[listIndex].add(intList.get(mutant)[0]);
intList.get(mutant)[1]--;
if(intList.get(mutant)[1]==0)
intList.remove(mutant);
else {
mutant++;
}
}
}
}else {
int mutant=intList.size()-1;
System.out.println("\nE-ZUGI: \n" + intList.get(mutant)[0]);
while(resultLists[listIndex].size()<K)
{
if(intList.get(mutant)[1]==0)
intList.remove(mutant);
else {
System.out.print(intList.get(mutant)[0] + "\t");
resultLists[listIndex].add(intList.get(mutant)[0]);
intList.get(mutant)[1]--;
if(intList.get(mutant)[1]==0)
intList.remove(mutant);
}
mutant--;
}
}
side++;
listIndex++;
}
}
int res=0;
for(int i=0; i<resultLists.length ; i++)
{
Collections.sort(resultLists[i]);
System.out.println();
for(int item : resultLists[i])
System.out.print(item + "\t");
int tail = resultLists[i].get(0);
int lead = resultLists[i].get(resultLists[i].size()-1);
System.out.println("\n" + tail + "\t" + lead + "\t" + res + "\n");
res+= Math.abs(lead-tail);
}
return res;
}
public static int findMinPositiveOrg(int[] A) {
// write your code in Java SE 8
//naive approcahe
int min=1;
Arrays.sort(A); //O(nlogn)
for(int i=0 ; i<A.length ; i++){ //O(n)
if(A[i] > min)
return min;
else if(A[i]==min)
min++;
if(i==A.length)
return A[A.length-1]+1;
}
return min;
}
public static int findMinPositive(int[] A) {
// write your code in Java SE 8
//naive approcahe
int N=100000;
int SIZE = 1000000;
int[] hashTable = new int[SIZE*2+1]; //O(1)?
for(int i=0 ; i<A.length ; i++) //O(n)
hashTable[A[i]+SIZE] = 1;
for(int i=SIZE+1 ; i<hashTable.length ; i++) //O(n)
{
if( hashTable[i]!=1)
return i-SIZE;
if(i==hashTable.length)
return A[hashTable.length] - SIZE +1;
}
return 1;
}
public static double findSquareRoot(int n) {
int i=1;
double mid=1;
while(i*i<n)
i++;
if(i*i==n)
return i;
mid=i-0.5;
for(double k=1; k<10000; k++)
{
if(mid*mid>n)
mid=mid-(1/(k*2));
else mid=mid+(1/(k*2));
}
return mid;
}
}//end of class