-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path07.Arrays cpp.cpp
More file actions
1411 lines (1220 loc) · 33.9 KB
/
Copy path07.Arrays cpp.cpp
File metadata and controls
1411 lines (1220 loc) · 33.9 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
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
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
//! --------------------------------> Array problems in cpp <-----------------------------------------
void print_array(int arr[], int n)
{
for (int i = 0; i < n; i++)
{
cout << arr[i] << " ";
}
}
//^ Easy quation of array
//! Q finding the largest elemnt in tha Array
void large_elemenet_arry(int arr[], int n)
{
// Brute force solution for this is to sort the arry
// return the last element of the array
// sort(arr);
// cout<<arr[n-1]; where the n is the size of the array
// optimal solution
// consider 1st element of the array as largest
int i = 0;
int max = arr[i];
for (int i = 0; i < n; i++)
{
if (arr[i] > max)
{
max = arr[i];
}
}
cout << "largest element is the array is " << max;
}
//! Q finding the second largest element in the array
void second_largest(int arr[], int n)
{
// int large = 0;
// for (int i = 0; i < n; i++)
// {
// if (arr[i]>large)
// {
// large = arr[i];
// }
// }
// cout<<"largest element is the array is "<<large<<endl;
// int second_largest = -1;
// for (int i = 0; i < n; i++)
// {
// if (arr[i]>second_largest && arr[i]!=large)
// {
// second_largest = arr[i];
// }
// }
// cout<<"second largest ele in the array is "<<second_largest;
int largest = arr[0];
int second_largest = INT_MIN;
for (int i = 0; i < n; i++)
{
if (arr[i] > largest)
{
second_largest = largest;
largest = arr[i];
}
else if (arr[i] < largest && arr[i] > second_largest)
{
second_largest = arr[i];
}
}
cout << "second largest element in arry is " << second_largest << endl;
}
//! Q finding the smallest element in the array
void smallest_ele(int arr[], int n)
{
int small = arr[0];
for (int i = 1; i < n; i++)
{
if (arr[i] < small)
{
small = arr[i];
}
}
cout << "the smallest element is arry is " << small;
}
//! Q finding the second smallest element in the array
void second_small_ele(int arr[], int n)
{
int small = arr[0];
int second_smallest = INT_MAX;
for (int i = 0; i < n; i++)
{
if (arr[i] < small)
{
second_smallest = small;
small = arr[i];
}
else if (arr[i] != small && arr[i] < second_smallest)
{
second_smallest = arr[i];
}
}
cout << "second smallest in the arry is " << second_smallest;
}
//! check if the array is sorted or not
int check_sorted_array(int arr[], int n)
{
// checking if the array is sorted or not
for (int i = 0; i < n; i++)
{
if (arr[i] >= arr[i - 1])
{
}
else
{
return false;
}
}
return true;
// for (int i = 0; i < n - 1; i++) {
// if (arr[i] > arr[i + 1]) {
// return false;
// }
// }
// return true;
}
//! remove deplicate element from the array
int remove_duplicate(int arr[], int n)
{
// brute force solution
// set<int> st;
// for(int i = 0; i<n; i++)
// {
// st.insert(arr[i]);
// }
// cout<< st.size();
// optimal solution
int i = 0;
for (int j = 1; j < n; j++)
{
if (arr[j] != arr[i])
{
arr[i + 1] = arr[j];
i++;
}
}
return i + 1;
}
//! left rotate the array by one place
void left_rotate_array(int arr[], int n)
{
// shifting by one place to left in the array
// brute force solution 0 1 2 3 4
// using another array {1,2,3,4,5}
// int temp[n];
// for (int i = 1; i < n; i++)
// {
// temp[i - 1] = arr[i];
// }
// // insert the 1st element into last
// temp[n - 1] = arr[0];
// // calling the function for printing the array
// print_array(temp,n); // TC = O(n) SP= O(n)
// optimal solution using pointers
// storing the first element of arr in variable
// shifting elements in the array by i-1
// 0 1 2 3 4
// {1,2,3,4,5}
int temp = arr[0];
for (int i = 0; i < n; i++)
{
arr[i - 1] = arr[i];
}
arr[n - 1] = temp;
print_array(arr, n); // Tc=O(n) SC=O(1)
}
//! left rotate the array by k places
void left_rotate_by_k_places(int arr[], int n, int k)
{
// brute force
// making the temp arr for storing the k elements
// int temp[k];
// //now insert elements in the temp arry
// for (int i = 0; i < k; i++)
// {
// temp[i] = arr[i];
// }
// // now shift the k+1 elements the same array
// for (int i = k; i < n; i++)
// {
// arr[i-k] = arr[i];
// }
// // insert the temp to back in the array
// int j = 0;
// for (int i = n-k; i < n; i++)
// {
// arr[i] = temp[j];
// j++;
// }
// print_array(arr,n);
// optimize the solution for space complaxity
// reversing from 0 to k
reverse(arr, arr + k);
// reversing the array from k to n
reverse(arr + k, arr + n);
// reversing the entire array
reverse(arr, arr + n);
// printing the array
print_array(arr, n);
/*
void Reverse(int arr[], int start, int end)
{
while (start <= end)
{
int temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
start++;
end--;
}
}
*/
}
//! right rotate the array by k places
void right_rotate_by_k_places(int arr[], int n, int k)
{
// brute force solution
// int temp[k];
// for (int i = 0; i < k; i++)
// {
// temp[i] = arr[n - k + i];
// }
// for (int i = n - k - 1; i >= 0; i--)
// {
// arr[i + k] = arr[i];
// }
// for (int i = 0; i < k; i++)
// {
// arr[i] = temp[i];
// }
// print_array(arr, n);
// optimal solution
// reverse the array from 0 to n-k
reverse(arr, arr + n - k);
// reverse the array from n-k to n
reverse(arr + n - k, arr + n);
// reverse the entire array
reverse(arr, arr + n);
print_array(arr, n);
}
//! move all zeros to end
void move_zeros_to_end(int arr[], int n)
{
// brute force solution
// find non zero elements and place them into a temp array
// vector<int> temp;
// for (int i = 0; i < n; i++)
// {
// if (arr[i]!=0)
// {
// temp.push_back(arr[i]);
// }
// }
// // after removing the all non zero elements from the array
// // the array become empty now insert the all elements back
// // from the temp array to main array
// int non_zero_elements = temp.size();
// for (int i = 0; i < non_zero_elements; i++)
// {
// arr[i] = temp[i];
// }
// //mark all remaining elements as 0 from the all non zero
// // elements to n or end of the array
// for (int i = non_zero_elements; i <n ; i++)
// {
// arr[i] = 0;
// }
// print_array(arr,n); // TC O(n)+ O(x) + O(n-x) = O(2n)
// SC O(x)
//! optimal aproach
// we use two pointer aproach i and j j present at -1 and i=0
// at first we finding 0th element in the array and update
// that elements index to j
// at next step we just swap the 0 element with next non zero-element
int j = -1;
for (int i = 0; i < n; i++)
{
if (arr[i] == 0)
{ // TC = O(x)
j = i;
break;
}
}
// again run the loop for swaping the 0 element with non-zero element
for (int i = j + 1; i < n; i++)
{
if (arr[i] != 0)
{
swap(arr[i], arr[j]); // TC = O(n-x)
j++;
}
}
print_array(arr, n); // TC = O(x) + O(n-x) xx get cansel
// TC = O(n)
// SC = O(1)
/*
int left = 0;
for(int right = 0; right<n; right++)
{
if(arr[i]!= 0)
{
swap(arr[right], arr[left]);
left++;
}
}
0 1 2 3
arr = {0,0,0,1} left = 0; right=0;
condition if arr[i] means 0th element is not zero then swap
if right index element with left index element
since the at 0th index we have the 0 so we skip swap
right++ right moves to 1th index
skip swap right++ right moves to 2nd index
her also present 0 skip swap right++ 3rd index
her we have the 1 is not equal to 0 and left is at the 0th
so we swap the right index 3 element 1 with left index 0 element 0
the resul array look like
arr[] = (1,0,0,0)
*/
}
//! union of two sorted arrays
vector<int> Union_of_two_sorted_Arrays(vector<int> a, vector<int> b)
{
// brute force solution for finding union on two sets
// {1,2,4,5}
// {2,3,4,5,6}
// un {1,2,3,4,5,6}
// set<int> st;
// int n = a.size();
// int m = b.size();
// for (int i = 0; i < n; i++){
// st.insert(a[i]);
// }
// for (int i = 0; i < m; i++){
// st.insert(b[i]);
// }
// // converting the set into vector
// vector<int> result(st.begin(), st.end());
// return result;
// optimal approach using two pointer approach
int n = a.size();
int m = b.size();
int i = 0;
int j = 0;
// a={1,2,4,5}
// b={2,3,4,5,6}
// un {1,2,3,4,5,6}
vector<int> ans;
while (i < n && j < m)
{
if (a[i] <= b[j])
{
if (ans.size() == 0 || ans.back() != a[i])
{
ans.push_back(a[i]);
}
i++;
}
else
{
if (ans.size() == 0 || ans.back() != b[j])
{
ans.push_back(b[j]);
}
j++;
}
}
while (j < m)
{
if (ans.size() == 0 || ans.back() != b[j])
{
ans.push_back(b[j]);
}
j++;
}
while (i < n)
{
if (ans.size() == 0 || ans.back() != a[i])
{
ans.push_back(a[i]);
}
i++;
}
return ans;
}
//! intersection of two sorted arrays
void intersection_of_arrays(int arr1[], int arr2[], int n, int m)
{
int i = 0;
int j = 0;
vector<int> ans;
while (i < n && j < m)
{
if (arr1[i] == arr2[j])
{
ans.push_back(arr1[i]);
}
else if (arr1[i] < arr2[j])
{
i++;
}
else
{
j++;
}
}
for (auto it : ans)
{
cout << it;
}
}
//! missing number in the array
void missing_no_in_array(int arr[], int n)
{
//! brute force solution for this is
// for (int i = 1; i < n; i++){
// // its basicaly the counter
// int counter = 0;
// // seariching the element like linerar search
// for (int j = 0; j < n; j++){
// if (arr[j] == i){
// counter = 1;
// break;
// }
// //Tc = n*n O(n^2)
// //SC = O(1)
// }
// // if counter returns 0 that means the element is
// // not present in the array in range 1 to n
// if(counter == 0){ cout<< i; }
//! }
// better solution using hasharray
// we have to declare the hash array size of n+1
// since array follows 0 based indexing if we suppose
// the n=5 and we declare hash[n] so it will create
// the array up to only that why we have to use n+1
// declaring the hashArray with all 0 elements
// int hash_arr[n+1]={0};
// //now update the frequency of array elements in hashmap
// // arr[] = (1 2 4 5) n = 5
// // 0 1 2 3 4 5
// // hashmap ={0,1,1,0,1,1}
// for (int i = 0; i < n; i++)
// {
// // if the element present it update it to 1
// hash_arr[arr[i]]=1; //TC O(n)
// }
// for (int i =1; i < n; i++)
// {
// if (hash_arr[i] == 0 ) //TC O(n)
// {
// cout<<i;
// }
// } //TC = O(N) + O(n) = O(2n)
// Sc = O(n)
// optimal solution
// there are 2 optimal solution available for this problem
// her we use formula of sum = n * (n+1)/2;
// and we get summetion of the array elements
// int sum = n*(n+1)/2; // n=5 5*(5+1)/2 = 30/2 =15
// int ans = 0;
// for (int i = 0; i <n-1; i++)
// {
// ans = ans + arr[i];
// }
// // we minus the sum with summetion of array element for getting
// // the missing number return sum-ans;
// cout<<sum-ans;
// another optimal solution using the XOR
// if we do any tow same number xor we get in return 0
// 2^2 = 0
}
//! maximum consecutive number of 1's
void Max_Consecutive_number_of_one(int arr[], int n)
{
// arr[] = {0,1,1,0,1,1,1,0,1,1,0} max = 3
int maxi = 0;
int count = 0;
for (int i = 0; i < n - 1; i++)
{
if (arr[i] == 1)
{
count++;
if (count > maxi)
{
maxi = count;
}
// max ( maxi , count) did the same thing if we declare
// maxi = max (maxi, count
}
else
{
count = 0;
}
}
cout << maxi;
}
//! find element that apprears once
void Find_element_that_appears_once(int arr[], int n)
{
// brute force solution
// for (int i = 0; i <n; i++)
// {
// int num = arr[i];
// int count = 0;
// for (int j = 0; j < n; j++)
// {
// if (arr[j] == num)
// {
// count++;
// }
// }
// if (count == 1)
// {
// cout<< num;
// }
// } TC = O(n) * O(n) = O(n^2) SC = O(1)
// better solution is to use hashing
// declare the hash array size of max element present in the array
int maxi = 0;
for (int i = 0; i < n; i++)
{
if (arr[i] > maxi)
{
maxi = arr[i];
}
}
int hashmap[maxi + 1] = {0};
for (int i = 0; i < n; i++)
{
hashmap[arr[i]]++;
}
for (int i = 0; i < n; i++)
{
if (hashmap[i] == 1)
{
cout << i;
}
}
// TC = O(n) + O(n) + (n) = O(3n)
// SC = O(x)
// int xorr =0;
// for (int i = 0; i < n; i++)
// {
// xorr = xorr ^ arr[i];
// }
// cout<<xorr;
// TC = O(n)
// SC = O(1)
}
//! --------------------------------> Medium quation of array <-----------------------------------------
//! Longest subarray with sum K
int Longest_Subarray_with_sum_K(int A[], int N, int K)
{
// Input: ‘n’ = 7 ‘k’ = 3
// ‘a’ = [1, 2, 3, 1, 1, 1, 1]
// int lenght = 0;
// for (int i = 0; i < n; i++)
// {
// int sum = 0; // 1 3
// for (int j = i; j < n; j++)
// {
// sum = sum + arr[j];
// if (sum == k)
// {
// lenght = max(lenght, j - i + 1);
// }
// }
// }
// cout << lenght; // TC = O(n^2) SC = O(1)
// better solution
map<int, int> preSumMap;
int sum = 0;
int max_length = 0;
for (int i = 0; i < N; i++)
{
// calculating the sum of sunArray
sum = sum + A[i];
// updating the sub array size if its equal to k
if (sum == K)
{
// suppose we have the array
// 0 1 2 3 4 5
// {10, 5, 3, 7, 1, 9}
// if the sub array found at 3 if we return only
// but and size become 2 but what if we are
// calculating from 1 becuse then it given 3 as
// answer thats why i+1
max_length = max(max_length, i + 1);
}
int remains = sum - K;
// checking the remaining is available in map using find and also
// check its not equal to the map last element
if (preSumMap.find(remains) != preSumMap.end())
{
// Calculate the length of the subarray
int len = i - preSumMap[remains];
// Update the maximum length
max_length = max(max_length, len);
}
// Store the current cumulative sum if it's not already in the map
if (preSumMap.find(sum) == preSumMap.end())
{
preSumMap[sum] = i;
}
}
return max_length;
}
//! two sum problem
std::pair<int, int> two_sum_using_pair(int arr[], int n, int target)
{
for (int i = 0; i < n; i++)
{
for (int j = i + 1; j < n; j++)
{
if (arr[i] + arr[j] == target)
{
return {i, j};
}
}
}
return {-1, -1}; // Return -1, -1 if no such pair exists
}
int two_sum(int arr[], int n, int target)
{
// for (int i = 0; i < n; i++)
// {
// for (int j = i+1; j < n; j++)
// {
// if (arr[i] + arr[j] == target)
// {
// cout<<i<<" "<<j;
// }
// }
// } // Tc = O(n) * O(n) = O(n^2) SC = O(1)
//! better solution using maps
// unordered_map<int, int> numMap;
// // Build the hash table
// for (int i = 0; i < n; i++)
// {
// numMap[arr[i]] = i;
// }
// // Find the complement
// for (int i = 0; i < n; i++)
// {
// int complement = target - arr[i];
// if (numMap.count(complement) && numMap[complement] != i)
// {
// // return {i, numMap[complement]};
// cout << i << numMap[complement];
// break;
// }
// }
// optimal
// two pointer approach where the two pointer are
// left and right left point 1st index and last points to last index
// left right
// [4, 1, 2, 3, 1]
// we just check left + right = target is yes then return left and right index
// if not then we increment left++ and decrement right-- for next iterantion
int left = 0;
int right = n - 1;
sort(arr, arr + n);
while (left < right)
{
int sum = arr[left] + arr[right];
if (sum == target)
{
return left, right;
}
else if (sum < target)
{
left++;
}
else
{
right++;
}
}
}
//! sort an array of 0s 1s and 2s without using extra space or sorting algo
void Sort_an_array_of_zeros_once_and_two(int arr[], int n)
{
// brute force solution just sort and return the array
// sort(arr,arr+n); // TC = O(n log n) SC = O(1)
// print_array(arr,n);
// using counters
// int count_0 = 0;
// int count_1 = 0;
// int count_2 = 0;
// // get the count of element of 0s 1s and 2s in the array
// for (int i = 0; i < n; i++)
// {
// if (arr[i] == 0)
// {
// count_0++;
// }
// else if (arr[i] == 1)
// {
// count_1++;
// }
// else
// {
// count_2++;
// }
// }
// // rewrite the element in the array from counts of each element
// int i = 0, j = 0, k = 0;
// // Rewrite 0s
// for (i = 0; i < count_0; i++)
// {
// arr[i] = 0;
// }
// // Rewrite 1s
// for (j = i; j < i + count_1; j++)
// {
// arr[j] = 1;
// }
// // Rewrite 2s
// for (k = j; k < j + count_2; k++)
// {
// arr[k] = 2;
// }
// l m h l m h lm h
// nums[] = {2, 0, 2, 1, 1, 0} {0,0,2,1,1,2} {0,0,2,1,1,2}
// lmh loop end her
// {0,0,1,1,2,2}
//! optimal using Dutch National flag algorithm.
int low = 0, mid = 0, high = n - 1;
while (mid <= high)
{
if (arr[mid] == 0)
{
swap(arr[low], arr[mid]);
low++;
mid++;
}
else if (arr[mid] == 1)
{
mid++;
}
else
{
swap(arr[mid], arr[high]);
high--;
}
}
// TC = O(n) SC = O(1)
print_array(arr, n);
}
//! find the majority element in the array
int Majority_Element(int nums[], int n)
{
// nums = [2,2,1,1,1,2,2]
//! brute force
// for (int i = 0; i < n; i++)
// {
// int count = 0;
// for(int i = 0; i<n; i++)
// {
// for(int j = 0; j<n; j++)
// {
// if(nums[j] == nums[i] )
// {
// count++;
// }
// }
// if(count > n/2) {return nums[i];}
// else{count = 0;}
// }
// return{};
// } // TC = O(n^2) Sc = O(1)
//! better solution using hashing
// map<int, int> mpp;
// // for the frequency of element in the hashmap
// for (int i = 0; i < nums.size(); i++) {
// mpp[nums[i]]++;
// }
// // since in the map elements store like key pair<int,int> where
// // 1st int is element and second one its frequency so it.second is
// // used for access the second pair of map same of vector and set
// for (auto it : mpp) {
// if (it.second > (nums.size() / 2)) {
// return it.first;
// }
// }
// return {};
//!---> optimal Moore's Voting Algorithm
int count = 0;
int element;
// moores voting algo
for (int i = 0; i < n; i++)
{
if (count == 0)
{ // 7 5 7 5 1 1 5 5 7 5 5 7 7 5 5 5 5
count = 1; // 2 1 2 1 0 1 0 1 0 1 2 1 0 1 2 3 4
element = nums[i];
}
else if (nums[i] == element)
{
count++;
}
else
{
count--;
}
}
// if we do not found the element or there is not mejority
// element in the array
int count1 = 0;
for (int i = 0; i < n; i++)
{
if (nums[i] == element)
{
count1++;
}
}
if (count1 > (n / 2))
{
return element;
}
return -1;
}
//! find the maximum subarray sum
int Maximum_Subarray_Sum(int arr[], int n)
{
// brute solution
// int lenght = 0;
// for (int i = 0; i < n; i++)
// {
// int sum = 0; // 1 3
// for (int j = i; j < n; j++)
// {
// sum = sum + arr[j];
// if (sum == k)
// {
// lenght = max(lenght, j - i + 1);
// }
// }
// }
// cout << lenght;
//! optimal Kadane's Algorithm
// nums = [-2,1,-3,4,-1,2,1,-5,4] Output: 6
int sum = 0;
int maxi = INT_MIN;
for (int i = 0; i < n; i++)
{
sum = sum + arr[i];
maxi = max(maxi, sum);
if (sum < 0)
{
sum = 0;
}
}
return maxi;
// TC = O(n) SC= O(1)
}
//! Best time to buy and sell stock (only one transaction)
void Best_Time_to_Buy_and_Sell_Stock(int arr[], int n)
{
// [7,1,5,3,6,4]
int mini = arr[0]; //
int profit = 0; //
for (int i = 1; i < n; i++)
{
int cost = arr[i] - mini;
if (cost > profit)
{
profit = cost;