-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrecursion.cpp
More file actions
executable file
·497 lines (429 loc) · 13.4 KB
/
Copy pathrecursion.cpp
File metadata and controls
executable file
·497 lines (429 loc) · 13.4 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
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
#include <iostream>
#include <vector>
#include <queue>
#include <cstdio>
#include <map>
#include <stdio.h>
#include <cstring>
using namespace std;
//1 2 3 4 5 7
int find_first_missing_number_helper(int *arr, int start, int end)
{
//base cases
int mid = (start + end) / 2;
int offset = mid - start;
if (start >= end) return 0;
if ((arr[mid] + 1) != arr[mid+1]) return (arr[mid] + 1);
if ((arr[start]+ offset) < arr[mid]) {
return find_first_missing_number_helper(arr, start, (mid));
} else {
return find_first_missing_number_helper(arr, mid+1, end);
}
}
int find_first_missing_number(int *arr, int size)
{
if ((size == 0) || (size == 1)) { return 0;}
return find_first_missing_number_helper(arr, 0, size-1);
}
int find_first_duplicate_number(int *arr, int start, int end)
{
//base case
int mid = (start+end)/2;
int offset = mid - start;
if (start >= end) {return -1;} //no duplication
if (arr[mid] == arr[mid+1]) {return arr[mid];}
//recursion
if ((arr[start]+offset) < arr[mid]) {
return find_first_duplicate_number(arr, start, mid);
} else {
return find_first_duplicate_number(arr, mid+1, end);
}
}
// 1 2 3 3 3 3 4
// 1 3
int find_first_target_number_in_sorted_array(int *arr, int start, int end, int target)
{
//base cases
int mid = (start + end) / 2;
if (start == end) {
if (arr[start] == target) {
return start;
} else {
return -1;
}
}
if ((arr[mid] != arr[mid+1]) && (arr[mid+1] == target)) {
return mid+1;
}
if (arr[mid] >= target) {
return find_first_target_number_in_sorted_array(arr, start, mid, target);
} else {
return find_first_target_number_in_sorted_array(arr, (mid+1), end, target);
}
}
// 1 3 3 3 3 5 6
// 1 3
// 1 1 3 3
int find_last_target_number_in_sorted_array(int *arr, int start, int end, int target)
{
//base cases
if (start == end) {
if (arr[start] == target) {
return start;
} else {
printf("last index for target %d is not found\n", target);
return -1;
}
}
int mid = (start + end) / 2;
if ((arr[mid] == target) && (arr[mid+1] != target)) {
return mid; //this is the last occurance of the target
} else {
if (arr[mid] > target) {
return find_last_target_number_in_sorted_array(arr, start, mid, target);
} else {
return find_last_target_number_in_sorted_array(arr, mid+1, end, target);
}
}
}
int find_target_number_occurance(int *arr, int start, int end, int target)
{
int first = find_first_target_number_in_sorted_array(arr, start, end, target);
if (first == -1) {
printf("target number %d is not found\n", target);
return -1;
}
int last = find_last_target_number_in_sorted_array(arr, start, end, target);
if (last == -1) {
printf("invalid last index\n");
return -1;
}
printf(" target %d occurs %d times\n", target, (last-first+1));
return (last - first + 1);
}
int climb_stairs_helper(int steps, int *buffer)
{
if (steps == 1) return 1;
if (steps == 2) return 2;
if (buffer[steps] > 0) return buffer[steps]; //return value from the cache
buffer[steps] = climb_stairs_helper((steps-1), buffer) + climb_stairs_helper((steps-2), buffer);
return buffer[steps];
}
int climb_stairs(int steps)
{
int buffer[steps+1];
memset(buffer, 0x0, (steps+1));
int result = 0;
result = climb_stairs_helper(steps, buffer);
printf(" result for %d steps is %d\n", steps, result);
return result;
}
int find_element_in_rotated_array(int *arr, int start, int end, int target)
{
// base cases
if (start == end) {
if (arr[start] != target) {
return (-1); //no element is found within the array
} else {
return start;
}
}
int mid = (start + end) / 2;
if (arr[mid] == target) return mid;
if (arr[mid] == arr[start]) {
if (arr[mid] == arr[end]) {
//Need to search both left and right
int index = find_element_in_rotated_array(arr, start, (mid-1), target);
if (index >= 0) return index;
return find_element_in_rotated_array(arr, (mid+1), end, target);
} else {
return find_element_in_rotated_array(arr, (mid+1), end, target);
}
} else if (arr[mid] < arr[end]){
if ((arr[mid] < target) && (target <= arr[end])) {
//target is on the right porition of the array (sorted)
return find_element_in_rotated_array(arr, (mid+1), end, target);
} else {
return find_element_in_rotated_array(arr, start, (mid-1), target);
}
} else if (arr[mid] > arr[start]) {
if ((arr[mid] > target) && (target >= arr[start])) {
//left side is sorted
return find_element_in_rotated_array(arr, start, (mid-1), target);
} else {
return find_element_in_rotated_array(arr, (mid+1), end, target);
}
}
return (-1);
}
int find_number_in_rotated_array(int *arr, int size, int target)
{
int pos = find_element_in_rotated_array(arr, 0, (size-1), target);
printf(" target %d is in position %d\n", target, pos);
return pos;
}
int count_rotations_helper(int *arr, int start, int end)
{
//base cases
if (start == end) return 0; //no rotation in the array
int mid = (start + end) / 2;
if (arr[mid] > arr[mid+1]) {
return (mid+1); //mid+1 contains the smallest number in the array
}
if (arr[mid] == arr[start]) {
if(arr[mid] < arr[end]) {
//the right side is sorted
return count_rotations_helper(arr, start, mid);
} else if (arr[mid] > arr[end]) {
return count_rotations_helper(arr, (mid+1), end);
} else {
// need to search both sides
int temp = count_rotations_helper(arr, start, mid);
if (temp > 0) return temp;
return count_rotations_helper(arr, (mid+1), end);
}
} else if (arr[mid] > arr[start]) {
return count_rotations_helper(arr, (mid+1), end);
} else {
return count_rotations_helper(arr, start, mid);
}
}
int count_rotations_in_sorted_array(int *arr, int size)
{
int rotate_num = count_rotations_helper(arr, 0, (size-1));
printf(" rotation is : %d\n", rotate_num);
}
void print_subset_helper(vector< int > &subset)
{
for (int i=0; i<subset.size(); i++) {
printf(" %d ", i);
}
printf("\n");
}
void print_subsets_in_array(int *array, int size, vector<vector< int > > &subsets)
{
if (size == 0) return; //return an empty subset
print_subsets_in_array(array, (size - 1), subsets);
int element = array[size-1]; //get the number at size-1
for (int i=0; i<subsets.size(); i++) {
subsets[i].push_back(element);
print_subset_helper(subsets[i]);
}
vector< int > temp;
temp.push_back(element);
subsets.push_back(temp);
printf(" %d\n", element);
}
void print_powerset(int *arr, int size)
{
vector<vector< int > > subsets;
print_subsets_in_array(arr, size, subsets);
}
void print_target_sum_pairs(int *arr, int size, int target_sum)
{
map<int, int> complement;
for (int i=0; i<size; i++) {
int difference = target_sum - arr[i];
if (complement[difference] == 0) { //no complement pair is found
complement[arr[i]]++; //increment the complement count
} else if (complement[difference] > 0) {
printf(" Pair: %d + %d = %d\n", arr[i], difference, target_sum);
complement[difference]--;
}
}
}
void merge(int *arr, int *helper, int start, int mid, int end)
{
int left1 = start;
int left2 = mid;
int right1 = mid+1;
int right2 = end;
int current = left1;
while ((left1 <= left2) && (right1 <= right2)) {
if (arr[left1] <= arr[right1]) {
helper[current] = arr[left1];
left1++;
} else {
helper[current] = arr[right1];
right1++;
}
current++;
}
//copy the remaining elements
while (left1 <= left2) {
helper[current++] = arr[left1];
left1++;
}
while (right1 <= right2) {
helper[current++] = arr[right1];
right1++;
}
//copy the sorted items back to the original array;
for (int i=start; i<=end; i++) {
arr[i] = helper[i];
}
}
void merge_sort_helper(int *arr, int *helper, int start, int end)
{
if (start < end) {
int mid = (start + end) / 2;
merge_sort_helper(arr, helper, start, mid);
merge_sort_helper(arr, helper, (mid+1), end);
merge(arr, helper, start, mid, end);
}
}
void merge_sort(int *arr, int size)
{
int helper[size];
memset(helper, 0x0, size);
merge_sort_helper(arr, helper, 0, (size-1));
printf("Array after sorting: \n");
for (int i=0; i<size; i++) {
printf(" %d", arr[i]);
}
printf("\n");
}
void test_merge_sort()
{
int arr1[2] = {2, 1};
int arr2[3] = {3, 1, 2};
int arr3[4] = {1, 2, 4, 3};
int arr4[4] = {2, 1, 3, 4};
int arr5[5] = {5, 2, 1, 3, 4};
int arr6[5] = {5, 2, 1, 4, 3};
int arr7[5] = {5, 4, 3, 2, 1};
merge_sort(arr1, 2);
merge_sort(arr2, 3);
merge_sort(arr3, 4);
merge_sort(arr4, 4);
merge_sort(arr5, 5);
merge_sort(arr6, 5);
merge_sort(arr7, 5);
}
void test_print_subset()
{
int arr1[1] = {1};
int arr2[2] = {1, 2};
int arr3[3] = {1, 2, 3};
int arr4[4] = {1, 2, 2, 3};
print_powerset(arr1, 1);
printf("\n");
print_powerset(arr2, 2);
printf("\n");
print_powerset(arr3, 3);
printf("\n");
print_powerset(arr4, 4);
printf("\n");
}
void test_print_target_sum_pairs()
{
int arr_1[1] = {1};
int arr_2[2] = {1,2};
int arr_3[3] = {1,3,5};
int arr_4[4] = {1,2,0,4};
int arr_5[5] = {1,2,0,4,1};
int arr_6[8] = {7,1,6,2,5,3,2,6};
int arr_7[7] = {7,2,2,7,3,6,9};
print_target_sum_pairs(arr_2, 1, 2);
print_target_sum_pairs(arr_2, 2, 3);
print_target_sum_pairs(arr_3, 3, 6);
print_target_sum_pairs(arr_4, 4, 4);
print_target_sum_pairs(arr_5, 5, 5);
print_target_sum_pairs(arr_6, 8, 8);
print_target_sum_pairs(arr_7, 8, 9);
}
void test_count_rotations_in_sorted_array()
{
int arr1[2] = {3, 1};
int arr2[3] = {3, 1, 2};
int arr3[3] = {2, 3, 1};
int arr4[4] = {3, 4, 1, 2};
int arr5[4] = {2, 3, 4, 1};
int arr6[5] = {3, 3, 3, 2, 3};
int arr7[5] = {3, 2, 3, 3, 3};
int arr8[5] = {3, 3, 3, 3, 3};
count_rotations_in_sorted_array(arr1, 2);
count_rotations_in_sorted_array(arr2, 3);
count_rotations_in_sorted_array(arr3, 3);
count_rotations_in_sorted_array(arr4, 4);
count_rotations_in_sorted_array(arr5, 4);
count_rotations_in_sorted_array(arr6, 5);
count_rotations_in_sorted_array(arr7, 5);
count_rotations_in_sorted_array(arr8, 5);
}
void test_find_number_in_rotated_array()
{
int arr1[2] = {3, 1};
int arr2[3] = {3, 1, 2};
int arr3[3] = {2, 3, 1};
int arr4[4] = {3, 4, 1, 2};
int arr5[4] = {2, 3, 4, 1};
int arr6[5] = {3, 3, 1, 2, 2};
int arr7[5] = {3, 3, 1, 2, 3};
find_number_in_rotated_array(arr1, 2, 3);
find_number_in_rotated_array(arr1, 2, 1);
find_number_in_rotated_array(arr2, 3, 2);
find_number_in_rotated_array(arr3, 3, 2);
find_number_in_rotated_array(arr4, 4, 4);
find_number_in_rotated_array(arr5, 4, 1);
find_number_in_rotated_array(arr6, 5, 3);
find_number_in_rotated_array(arr7, 5, 2);
}
void test_climb_stairs()
{
climb_stairs(1);
climb_stairs(2);
climb_stairs(3);
climb_stairs(4);
climb_stairs(5);
climb_stairs(6);
climb_stairs(7);
}
void test_find_target_number_occurance()
{
// test cases:
int arr1[1] = {1};
int arr2[2] = {1, 3};
int arr3[3] = {1, 3, 3};
int arr4[4] = {1, 3, 3, 3};
int arr5[5] = {1, 2, 2, 2, 3};
int arr6[6] = {2, 2, 2, 2, 2, 2};
printf("%s\n", __func__);
int rc_1 = find_target_number_occurance(arr1, 0, 0, 1);
int rc_2 = find_target_number_occurance(arr2, 0, 1, 1);
int rc_3 = find_target_number_occurance(arr3, 0, 2, 1);
int rc_4 = find_target_number_occurance(arr4, 0, 3, 3);
int rc_5 = find_target_number_occurance(arr5, 0, 4, 2);
int rc_6 = find_target_number_occurance(arr6, 0, 5, 2);
int rc_7 = find_target_number_occurance(arr6, 0, 5, 1);
}
void test_first_missing_number()
{
int a1[1] = {1};
int a2[2] = {2, 4};
int a3[3] = {2, 3, 4};
int a4[4] = {2, 3, 5, 6};
int a5[4] = {2, 4, 5, 6};
int a6[5] = {1, 2, 3, 5, 6};
int a7[5] = {1, 2, 3, 4, 6};
printf("%s\n", __func__);
int rc_1 = find_first_missing_number(a1, 1);
int rc_2 = find_first_missing_number(a2, 2);
int rc_3 = find_first_missing_number(a3, 3);
int rc_4 = find_first_missing_number(a4, 4);
int rc_5 = find_first_missing_number(a5, 4);
int rc_6 = find_first_missing_number(a6, 6);
int rc_7 = find_first_missing_number(a7, 7);
printf("Results: %d %d %d %d %d %d %d\n", rc_1, rc_2, rc_3, rc_4, rc_5, rc_6, rc_7);
}
int main()
{
test_first_missing_number();
test_find_target_number_occurance();
test_climb_stairs();
test_find_number_in_rotated_array();
test_count_rotations_in_sorted_array();
test_print_target_sum_pairs();
test_print_subset();
test_merge_sort();
}