-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCodilityAlgorithms.cpp
1694 lines (1452 loc) · 56.1 KB
/
CodilityAlgorithms.cpp
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 "CodilityAlgorithms.h"
#include "gtest/gtest.h"
#include <algorithm>
// Lesson 1 (Iterations) - BinaryGap tests.
// -------------------------------------------------
// Test case to check if the function returns the correct binary gap for a positive integer.
TEST(BinaryGapTest, PositiveInteger)
{
EXPECT_EQ(binaryGap(9), 2); // Binary representation of 9 is 1001, so the gap is 2.
EXPECT_EQ(binaryGap(529), 4); // Binary representation of 529 is 1000010001, so the gap is 4.
EXPECT_EQ(binaryGap(1041), 5); // Binary representation of 1041 is 10000010001, so the gap is 5.
}
// Test case to check if the function handles a case where there is no binary gap.
TEST(BinaryGapTest, NoBinaryGap)
{
EXPECT_EQ(binaryGap(15), 0); // Binary representation of 15 is 1111, so there's no binary gap.
EXPECT_EQ(binaryGap(1), 0); // Binary representation of 1 is 1, so there's no binary gap.
EXPECT_EQ(binaryGap(7), 0); // Binary representation of 7 is 111, so there's no binary gap.
}
// Test case to check if the function handles the lower bound of the input range.
TEST(BinaryGapTest, LowerBound)
{
EXPECT_EQ(binaryGap(1), 0); // Binary representation of 1 is 1, so there's no binary gap.
}
// Test case to check if the function handles the upper bound of the input range.
TEST(BinaryGapTest, UpperBound) {
EXPECT_EQ(binaryGap(2147483647), 0); // Binary representation of the upper bound has no binary gap.
}
// Lesson 2 (Arrays) - CyclicRotation tests.
// -------------------------------------------------
// Test case to check if the function correctly rotates the array to the right by K positions.
TEST(CyclicRotationTest, BasicRotation)
{
std::vector<int> input1 = { 3, 8, 9, 7, 6 };
std::vector<int> expected1 = { 6, 3, 8, 9, 7 };
EXPECT_EQ(cyclicRotation(input1, 1), expected1);
std::vector<int> input2 = { 1, 2, 3, 4, 5 };
std::vector<int> expected2 = { 2, 3, 4, 5, 1 };
EXPECT_EQ(cyclicRotation(input2, 4), expected2);
}
// Test case to check if the function correctly handles an empty input array.
TEST(CyclicRotationTest, EmptyArray)
{
std::vector<int> empty;
EXPECT_EQ(cyclicRotation(empty, 3), empty);
}
// Test case to check if the function correctly handles a rotation of zero times.
TEST(CyclicRotationTest, ZeroRotation)
{
std::vector<int> input = { 1, 2, 3, 4, 5 };
std::vector<int> expected = { 1, 2, 3, 4, 5 };
EXPECT_EQ(cyclicRotation(input, 0), expected);
}
// Test case to check if the function correctly handles a rotation greater than the array size.
TEST(CyclicRotationTest, RotationGreaterThanSize)
{
std::vector<int> input = { 1, 2, 3, 4, 5 };
std::vector<int> expected = { 3, 4, 5, 1, 2 }; // Equivalent to rotating by 8 positions.
EXPECT_EQ(cyclicRotation(input, 8), expected);
}
// Test case to check if the function correctly handles rotations when K is equal to the array size.
TEST(CyclicRotationTest, RotationEqualToSize)
{
std::vector<int> input = { 1, 2, 3, 4, 5 };
std::vector<int> expected = { 1, 2, 3, 4, 5 }; // No change, as K is equal to the size.
EXPECT_EQ(cyclicRotation(input, 5), expected);
}
// Lesson 2 (Arrays) - OddOccurrencesInArray tests.
// -------------------------------------------------
// Test case to check if the function correctly finds the unpaired element in the array.
TEST(OddOccurrencesInArrayTest, BasicTest)
{
std::vector<int> input1 = { 9, 3, 9, 3, 9, 7, 9 };
EXPECT_EQ(oddOccurrencesInArray(input1), 7);
std::vector<int> input2 = { 1, 2, 3, 2, 1 };
EXPECT_EQ(oddOccurrencesInArray(input2), 3);
}
// Test case to check if the function correctly handles an array with a single element.
TEST(OddOccurrencesInArrayTest, SingleElementArray)
{
std::vector<int> input = { 42 };
EXPECT_EQ(oddOccurrencesInArray(input), 42);
}
// Test case to check if the function correctly handles large input arrays.
TEST(OddOccurrencesInArrayTest, LargeInput)
{
std::vector<int> input;
for (int i = 1; i <= 1000000; ++i) {
input.push_back(i);
input.push_back(i);
}
input.push_back(999999); // Add the unpaired element at the end.
EXPECT_EQ(oddOccurrencesInArray(input), 999999);
}
// Lesson 3 (Time Complexity) - FrogJmp tests.
// -------------------------------------------------
// Test case to check if the function returns the correct minimal number of jumps.
TEST(FrogJmpTest, BasicTest)
{
EXPECT_EQ(frogJmp(10, 85, 30), 3); // X = 10, Y = 85, D = 30, minimal jumps = ceil((85 - 10) / 30) = 3.
EXPECT_EQ(frogJmp(1, 1000000000, 1), 999999999); // X = 1, Y = 1000000000, D = 1, minimal jumps = ceil((1000000000 - 1) / 1) = 999999999.
}
// Test case to check if the function correctly handles a case where X and Y are equal.
TEST(FrogJmpTest, SamePositions)
{
EXPECT_EQ(frogJmp(7, 7, 3), 0); // X = 7, Y = 7, D = 3, no jumps needed (already at the target).
}
// Test case to check if the function correctly handles a case where D is greater than or equal to (Y - X).
TEST(FrogJmpTest, LargeDistance)
{
EXPECT_EQ(frogJmp(1, 10, 20), 1); // X = 1, Y = 10, D = 20, minimal jumps = ceil((10 - 1) / 20) = 1.
EXPECT_EQ(frogJmp(1, 100, 100), 1); // X = 1, Y = 100, D = 100, minimal jumps = ceil((100 - 1) / 100) = 1.
}
// Lesson 3 (Time Complexity) - PermMissingElem tests.
// -------------------------------------------------
// Test case to check if the function returns the correct missing element.
TEST(PermMissingElemTest, BasicTest)
{
std::vector<int> input1 = { 2, 3, 1, 5 };
EXPECT_EQ(permMissingElem(input1), 4); // The missing element is 4.
std::vector<int> input2 = { 1, 2, 4, 5 };
EXPECT_EQ(permMissingElem(input2), 3); // The missing element is 3.
}
// Test case to check if the function correctly handles an empty input array.
TEST(PermMissingElemTest, EmptyArray)
{
std::vector<int> empty;
EXPECT_EQ(permMissingElem(empty), 1); // The missing element is 1 (smallest possible).
}
// Test case to check if the function correctly handles an array with a single element.
TEST(PermMissingElemTest, SingleElementArray)
{
std::vector<int> input = { 1 };
EXPECT_EQ(permMissingElem(input), 2); // The missing element is 2.
}
// Test case to check if the function correctly handles large input arrays.
TEST(PermMissingElemTest, LargeInput)
{
std::vector<int> input;
for (int i = 1; i <= 100000; ++i) {
input.push_back(i);
}
EXPECT_EQ(permMissingElem(input), 100001); // The missing element is 100001.
}
// Lesson 3 (Time Complexity) - TapeEquilibrium
// -------------------------------------------------
// Test case to check if the function returns the correct minimal difference.
TEST(TapeEquilibriumTest, BasicTest)
{
std::vector<int> input1 = { 3, 1, 2, 4, 3 };
EXPECT_EQ(tapeEquilibrium(input1), 1); // P = 3, |(3 + 1 + 2) - (4 + 3)| = 1.
std::vector<int> input2 = { 1, 1, 1, 1, 1 };
EXPECT_EQ(tapeEquilibrium(input2), 1); // P = 1, |(1 + 1) - (1 + 1 + 1)| = 1.
}
// Test case to check if the function handles negative numbers correctly.
TEST(TapeEquilibriumTest, NegativeNumbers)
{
std::vector<int> input = { -10, 5, -3, 4, 2 };
EXPECT_EQ(tapeEquilibrium(input), 6); // P = 6, |(-10 + 5 - 3 + 4) - 2)| = 6.
}
// Test case to check if the function handles large input arrays.
TEST(TapeEquilibriumTest, LargeInput)
{
std::vector<int> input;
for (int i = 1; i <= 100000; ++i) {
input.push_back(i);
}
EXPECT_EQ(tapeEquilibrium(input), 5658); // P = 5658.
}
// Test case to check if the function returns the correct minimal difference when N is 2.
TEST(TapeEquilibriumTest, NIs2)
{
std::vector<int> input1 = { 1, 2 };
EXPECT_EQ(tapeEquilibrium(input1), 1); // P = 1, |(1) - (2)| = 1.
std::vector<int> input2 = { 5, 5 };
EXPECT_EQ(tapeEquilibrium(input2), 0); // P = 1, |(5) - (5)| = 0.
}
// Test case to check if the function returns the correct minimal difference when all elements are the same.
TEST(TapeEquilibriumTest, AllElementsSame)
{
std::vector<int> input1 = { 2, 2, 2, 2, 2 };
EXPECT_EQ(tapeEquilibrium(input1), 2); // P = 2, |(2 + 2 + 2) - (2 + 2)| = 2.
std::vector<int> input2 = { -5, -5, -5, -5, -5 };
EXPECT_EQ(tapeEquilibrium(input2), 5); // P = 5, |(-5 -5) - (-5 - 5 - 5)| = 5.
}
// Test case to check if the function handles alternating positive and negative numbers.
TEST(TapeEquilibriumTest, AlternatingNumbers)
{
std::vector<int> input = { -1, 2, -3, 4, -5 };
EXPECT_EQ(tapeEquilibrium(input), 1); // P = 2, |(-1 + 2) - (-3 + 4 - 5)| = 1.
}
// Lesson 4 (Counting Elements) - FrogRiverOne
// -------------------------------------------------
// Test when the frog can jump to the other side of the river.
TEST(FrogRiverOneTest, FrogCanJump)
{
std::vector<int> leaves = { 1, 3, 1, 4, 2, 3, 5, 4 };
int X = 5;
int result = frogRiverOne(X, leaves);
EXPECT_EQ(result, 6); // The frog can jump at time 6.
}
// Test when the frog can never jump to the other side.
TEST(FrogRiverOneTest, FrogCantJump)
{
std::vector<int> leaves = { 1, 3, 1, 4, 2, 3, 5, 4 };
int X = 6;
int result = frogRiverOne(X, leaves);
EXPECT_EQ(result, -1); // The frog can't jump to the other side.
}
// Test when the frog can jump immediately.
TEST(FrogRiverOneTest, FrogCanJumpImmediately)
{
std::vector<int> leaves = { 1 };
int X = 1;
int result = frogRiverOne(X, leaves);
EXPECT_EQ(result, 0); // The frog can jump at time 0.
}
// Test when X is 1, and there's only one leaf at position 1.
TEST(FrogRiverOneTest, FrogCanJump_X1)
{
std::vector<int> leaves = { 1 };
int X = 1;
int result = frogRiverOne(X, leaves);
EXPECT_EQ(result, 0); // The frog can jump at time 0.
}
// Test when X is 1, but there's no leaf at position 1.
TEST(FrogRiverOneTest, FrogCantJump_X1_NoLeaf)
{
std::vector<int> leaves = { 2, 3, 4 };
int X = 1;
int result = frogRiverOne(X, leaves);
EXPECT_EQ(result, -1); // The frog can't jump to the other side.
}
// Test when X is equal to the length of the array
TEST(FrogRiverOneTest, FrogCanJump_XEqualsLength)
{
std::vector<int> leaves = { 1, 2, 3, 4, 5 };
int X = 5;
int result = frogRiverOne(X, leaves);
EXPECT_EQ(result, 4); // The frog can jump at time 4.
}
// Test when X is very large (maximum value) and there are leaves at every position.
TEST(FrogRiverOneTest, FrogCanJump_XMaxValue)
{
int X = 100000;
std::vector<int> leaves(X);
for (int i = 0; i < X; ++i) {
leaves[i] = i + 1; // Leaves at positions 1 to X.
}
int result = frogRiverOne(X, leaves);
EXPECT_EQ(result, X - 1); // The frog can jump at time X - 1.
}
// Test when X is very large (maximum value) but there are missing leaves.
TEST(FrogRiverOneTest, FrogCantJump_XMaxValue_MissingLeaves)
{
int X = 100000;
std::vector<int> leaves(X - 1); // Missing one leaf at position X.
for (int i = 0; i < X - 1; ++i) {
leaves[i] = i + 1; // Leaves at positions 1 to X-1.
}
int result = frogRiverOne(X, leaves);
EXPECT_EQ(result, -1); // The frog can't jump to the other side.
}
// Lesson 4 (Counting Elements) - PermCheck
// -------------------------------------------------
// Test when array A is a permutation.
TEST(PermCheckTest, ArrayIsPermutation)
{
std::vector<int> A = { 4, 1, 3, 2 };
EXPECT_EQ(permCheck(A), 1); // Array A is a permutation.
}
// Test when array A is not a permutation (missing values).
TEST(PermCheckTest, ArrayNotPermutation_MissingValues)
{
std::vector<int> A = { 4, 1, 3 };
EXPECT_EQ(permCheck(A), 0); // Array A is not a permutation (missing value 2).
}
// Test when array A is not a permutation (duplicate values).
TEST(PermCheckTest, ArrayNotPermutation_DuplicateValues)
{
std::vector<int> A = { 4, 1, 3, 2, 2 };
EXPECT_EQ(permCheck(A), 0); // Array A is not a permutation (duplicate value 2).
}
// Test when array A contains only one element (minimum edge case).
TEST(PermCheckTest, ArrayWithOneElement) {
std::vector<int> A = { 1 };
EXPECT_EQ(permCheck(A), 1); // Array A is a permutation with a single element.
}
// Test when array A is not a permutation (out of range values).
TEST(PermCheckTest, ArrayNotPermutation_OutOfRangeValues)
{
std::vector<int> A = { 4, 1, 3, 2, 6 };
EXPECT_EQ(permCheck(A), 0); // Array A is not a permutation (value 6 is out of range).
}
// Test when array A is a permutation with values from 1 to N (in ascending order).
TEST(PermCheckTest, ArrayIsPermutation_AscendingOrder)
{
std::vector<int> A = { 1, 2, 3, 4, 5 };
EXPECT_EQ(permCheck(A), 1); // Array A is a permutation.
}
// Test when array A is a permutation with values from 1 to N (in descending order).
TEST(PermCheckTest, ArrayIsPermutation_DescendingOrder)
{
std::vector<int> A = { 5, 4, 3, 2, 1 };
EXPECT_EQ(permCheck(A), 1); // Array A is a permutation.
}
// Test when array A is a permutation with a large number of elements (N = 100,000).
TEST(PermCheckTest, LargeArrayIsPermutation)
{
std::vector<int> A(100000);
for (int i = 0; i < 100000; ++i) {
A[i] = i + 1; // A is a permutation with values from 1 to 100,000.
}
EXPECT_EQ(permCheck(A), 1); // Array A is a permutation.
}
// Lesson 4 (Counting Elements) - MaxCounters
// -------------------------------------------------
// Test when N is 1, and A contains a single max counter operation.
TEST(MaxCountersTest, N1_SingleMaxCounter)
{
int N = 1;
std::vector<int> A = { 2 };
std::vector<int> result = maxCounters(N, A);
EXPECT_EQ(result, std::vector<int>({ 0 }));
}
// Test when N is 5, and A contains a sequence of operations.
TEST(MaxCountersTest, N5_SequenceOfOperations) {
int N = 5;
std::vector<int> A = { 3, 4, 4, 6, 1, 4, 4 };
std::vector<int> result = maxCounters(N, A);
EXPECT_EQ(result, std::vector<int>({ 3, 2, 2, 4, 2 }));
}
// Test when N is 3, and A contains only max counter operations.
TEST(MaxCountersTest, N3_MaxCounterOperationsOnly) {
int N = 3;
std::vector<int> A = { 4, 4, 4, 4, 4 };
std::vector<int> result = maxCounters(N, A);
EXPECT_EQ(result, std::vector<int>({ 0, 0, 0 }));
}
TEST(MaxCountersTest, N1_SingleIncreaseOperation) {
int N = 1;
std::vector<int> A = { 1 };
std::vector<int> result = maxCounters(N, A);
EXPECT_EQ(result, std::vector<int>({ 1 }));
}
// Test when N is 2, and A contains a sequence of operations.
TEST(MaxCountersTest, N2_SequenceOfOperations)
{
int N = 2;
std::vector<int> A = { 1, 2, 1, 2, 1 };
std::vector<int> result = maxCounters(N, A);
EXPECT_EQ(result, std::vector<int>({ 3, 2 }));
}
// Test when N is 4, and A contains a sequence of operations with multiple max counter operations.
TEST(MaxCountersTest, N4_SequenceWithMultipleMaxCounterOperations)
{
int N = 4;
std::vector<int> A = { 3, 4, 4, 6, 1, 4, 4, 5, 5 };
std::vector<int> result = maxCounters(N, A);
EXPECT_EQ(result, std::vector<int>({ 4, 4, 4, 4 }));
}
// Test when N is 3, and A contains alternating increase and max counter operations.
TEST(MaxCountersTest, N3_AlternatingOperations)
{
int N = 3;
std::vector<int> A = { 1, 4, 2, 4, 3, 4 };
std::vector<int> result = maxCounters(N, A);
EXPECT_EQ(result, std::vector<int>({ 3, 3, 3 }));
}
// Test when N is 100,000, and A contains a large sequence of operations.
TEST(MaxCountersTest, N100000_LargeSequenceOfOperations)
{
int N = 100000;
std::vector<int> A(1000000, N + 1);
std::vector<int> result = maxCounters(N, A);
EXPECT_TRUE(std::all_of(result.begin(), result.end(), [](int val) { return val == 0; }));
}
// Lesson 4 (Counting Elements) - MissingInteger
// -------------------------------------------------
// Test when A contains positive integers in ascending order.
TEST(MissingIntegerTest, AscendingPositiveIntegers)
{
std::vector<int> A = { 1, 2, 3, 4, 5 };
int result = missingInteger(A);
EXPECT_EQ(result, 6); // The smallest missing positive integer is 6.
}
// Test when A contains positive integers in descending order.
TEST(MissingIntegerTest, DescendingPositiveIntegers)
{
std::vector<int> A = { 5, 4, 3, 2, 1 };
int result = missingInteger(A);
EXPECT_EQ(result, 6); // The smallest missing positive integer is 6.
}
// Test when A contains a mix of positive and negative integers.
TEST(MissingIntegerTest, MixOfPositiveAndNegativeIntegers)
{
std::vector<int> A = { -1, -3, 6, 4, 1, 2 };
int result = missingInteger(A);
EXPECT_EQ(result, 3); // The smallest missing positive integer is 3.
}
// Test when A contains only negative integers.
TEST(MissingIntegerTest, OnlyNegativeIntegers)
{
std::vector<int> A = { -5, -4, -3, -2, -1 };
int result = missingInteger(A);
EXPECT_EQ(result, 1); // The smallest missing positive integer is 1.
}
// Test when A contains a single positive integer.
TEST(MissingIntegerTest, SinglePositiveInteger)
{
std::vector<int> A = { 42 };
int result = missingInteger(A);
EXPECT_EQ(result, 1); // The smallest missing positive integer is 1.
}
// Test when A contains a single negative integer.
TEST(MissingIntegerTest, SingleNegativeInteger)
{
std::vector<int> A = { -42 };
int result = missingInteger(A);
EXPECT_EQ(result, 1); // The smallest missing positive integer is 1.
}
// Test when A contains all positive integers from 1 to N, where N is large (N = 100,000).
TEST(MissingIntegerTest, AllPositiveIntegersUpToLargeN)
{
int N = 100000;
std::vector<int> A(N);
for (int i = 1; i <= N; ++i) {
A[i - 1] = i;
}
int result = missingInteger(A);
EXPECT_EQ(result, N + 1); // The smallest missing positive integer is N + 1.
}
// Test when A contains all negative integers and zero, where N is large (N = 100,000).
TEST(MissingIntegerTest, AllNegativeIntegersAndZero)
{
int N = 100000;
std::vector<int> A(N);
for (int i = 0; i < N; ++i) {
A[i] = -i;
}
int result = missingInteger(A);
EXPECT_EQ(result, 1); // The smallest missing positive integer is 1.
}
// Test when A contains a large number of random positive and negative integers.
TEST(MissingIntegerTest, LargeNumberOfRandomIntegers)
{
int N = 100000;
std::vector<int> A(N);
for (int i = 0; i < N; ++i) {
A[i] = rand() % 2000000 - 1000000; // Random integers between -1,000,000 and 1,000,000.
}
int result = missingInteger(A);
// Calculate the expected result manually based on the generated random integers.
std::vector<int> sortedA = A;
std::sort(sortedA.begin(), sortedA.end());
int expected = 1;
for (int i : sortedA) {
if (i == expected) {
++expected;
}
}
EXPECT_EQ(result, expected); // The smallest missing positive integer.
}
// Lesson 5 (Prefix Sums) - PassingCars
// -------------------------------------------------
// Test when A contains no passing cars.
TEST(PassingCarsTest, NoPassingCars)
{
std::vector<int> A = { 0, 0, 0, 0, 0 };
EXPECT_EQ(passingCars(A), 0); // No passing cars, so the result should be 0.
}
// Test when A contains only one car traveling east.
TEST(PassingCarsTest, SingleCarTravelingEast)
{
std::vector<int> A = { 0 };
EXPECT_EQ(passingCars(A), 0); // Only one car, so there are no passing cars.
}
// Test when A contains only one car traveling west.
TEST(PassingCarsTest, SingleCarTravelingWest)
{
std::vector<int> A = { 1 };
EXPECT_EQ(passingCars(A), 0); // Only one car, so there are no passing cars.
}
// Test when A contains multiple passing cars.
TEST(PassingCarsTest, MultiplePassingCars)
{
std::vector<int> A = { 0, 1, 0, 1, 1 };
EXPECT_EQ(passingCars(A), 5); // There are five pairs of passing cars.
}
// Test when A contains a large number of cars (maximum limit).
TEST(PassingCarsTest, LargeNumberOfCars)
{
int N = 100000;
std::vector<int> A(N, 0);
EXPECT_EQ(passingCars(A), 0); // No passing cars in a large array of eastbound cars.
}
// Test when A contains a large number of cars, all traveling west (maximum limit).
TEST(PassingCarsTest, LargeNumberOfCarsTravelingWest)
{
int N = 100000;
std::vector<int> A(N, 1);
EXPECT_EQ(passingCars(A), 0); // No passing cars in a large array of westbound cars.
}
// Test when A contains a large number of cars with one car heading westward.
TEST(PassingCarsTest, LargeNumberOfCarsOneCarHeadingWestwardInTheMiddle)
{
int N = 100000;
std::vector<int> A(N, 0);
A[N / 2] = 1; // A single car is heading westward, positioned in the middle.
EXPECT_EQ(passingCars(A), 50000);
}
// Test when carPasses exceeds 1,000,000,000.
TEST(PassingCarsTest, CarPassesExceedLimit)
{
int N = 100000;
std::vector<int> A(N, 0);
for (int i = 0; i < N; ++i) {
if (!(i % 2)) {
A[i] = 1;
}
}
EXPECT_EQ(passingCars(A), -1); // carPasses exceeds the limit, so the result should be -1.
}
// Lesson 5 (Prefix Sums) - CountDiv
// -------------------------------------------------
// Test when A = B and K = 1 (smallest possible inputs with K = 1).
TEST(CountDivTest, AEqualsBAndKOne)
{
int A = 5, B = 5, K = 1;
EXPECT_EQ(countDiv(A, B, K), 1); // Only 5 is divisible by 1 within the range [5..5].
}
// Test when A = 6, B = 11, and K = 2.
TEST(CountDivTest, BasicTest)
{
int A = 6, B = 11, K = 2;
EXPECT_EQ(countDiv(A, B, K), 3); // There are three numbers divisible by 2 within the range [6..11]: 6, 8, and 10.
}
// Test when A = 0, B = 100, and K = 10.
TEST(CountDivTest, LargeRange)
{
int A = 0, B = 100, K = 10;
EXPECT_EQ(countDiv(A, B, K), 11); // There are eleven numbers divisible by 10 within the range [0..100].
}
// Test when A = 0, B = 1,000,000,000, and K = 1,000,000,000 (maximum possible values).
TEST(CountDivTest, LargeValues)
{
int A = 0, B = 1000000000, K = 1000000000;
EXPECT_EQ(countDiv(A, B, K), 2); // Only 0 and 1000000000 are divisible by 1000000000 within the range [0..1000000000].
}
// Test when A, B, and K are at their maximum values.
TEST(CountDivTest, MaximumValues)
{
int A = 2000000000, B = 2000000000, K = 2000000000;
EXPECT_EQ(countDiv(A, B, K), 1); // Only 2000000000 is divisible by 2000000000 within the range [2000000000..2000000000].
}
// Test with a combination of minimum, maximum, and in-between values.
TEST(CountDivTest, MixedValues)
{
int A = 1, B = 2000000000, K = 1000000;
EXPECT_EQ(countDiv(A, B, K), 2000); // There are 2000 numbers divisible by 1000000 within the range [1..2000000000].
}
// Lesson 6 (Sorting) - Distinct
// -------------------------------------------------
// Test when the array is empty (N = 0).
TEST(DistinctTest, EmptyArray)
{
std::vector<int> A;
EXPECT_EQ(distinct(A), 0); // The array is empty, so there are no distinct values.
}
// Test when all elements in the array are the same.
TEST(DistinctTest, AllSameElements)
{
std::vector<int> A = { 2, 2, 2, 2, 2 };
EXPECT_EQ(distinct(A), 1); // All elements are the same (2), so there is only one distinct value.
}
// Test when the array contains distinct values.
TEST(DistinctTest, DistinctValues)
{
std::vector<int> A = { 2, 1, 3, 4, 5 };
EXPECT_EQ(distinct(A), 5); // All elements are distinct, so there are five distinct values.
}
// Test when the array contains negative values.
TEST(DistinctTest, NegativeValues)
{
std::vector<int> A = { -2, -1, -3, -2, -1 };
EXPECT_EQ(distinct(A), 3); // There are three distinct values: -2, -1, and -3.
}
// Test when the array contains the maximum number of elements (N = 100,000) with distinct values.
TEST(DistinctTest, LargeNumberOfElementsDistinct)
{
const int N = 100000;
std::vector<int> A(N);
for (int i = 0; i < N; ++i) {
A[i] = i;
}
EXPECT_EQ(distinct(A), N); // All elements are distinct, so there should be N distinct values.
}
// Test when the array contains the maximum number of elements (N = 100,000) with the same value.
TEST(DistinctTest, LargeNumberOfElementsSameValue)
{
const int N = 100000;
std::vector<int> A(N, 42); // Initialize with the same value (42).
EXPECT_EQ(distinct(A), 1); // All elements are the same, so there should be only one distinct value.
}
// Test when the array contains a large number of elements (N = 100,000) with a mix of values.
TEST(DistinctTest, LargeNumberOfElementsMixedValues)
{
const int N = 100000;
std::vector<int> A(N);
for (int i = 0; i < N; ++i) {
A[i] = i % 1000; // Repeat values every 1000 elements.
}
EXPECT_EQ(distinct(A), 1000); // There are 1000 distinct values repeated in the array.
}
// Lesson 6 (Sorting) - MaxProductOfThree
// -------------------------------------------------
// Test when the array contains only three elements with positive values.
TEST(MaxProductOfThreeTest, ThreePositiveElements)
{
std::vector<int> A = { 3, 5, 7 };
EXPECT_EQ(maxProductOfThree(A), 3 * 5 * 7); // The maximum product should be 3 * 5 * 7 = 105.
}
// Test when the array contains a mix of positive and negative values.
TEST(MaxProductOfThreeTest, MixedValues)
{
std::vector<int> A = { -3, 5, -7, 2, 4 };
EXPECT_EQ(maxProductOfThree(A), -7 * -3 * 5); // The maximum product should be -7 * -3 * 5 = 105.
}
// Test when the array contains three elements with negative values.
TEST(MaxProductOfThreeTest, ThreeNegativeElements)
{
std::vector<int> A = { -3, -5, -7 };
EXPECT_EQ(maxProductOfThree(A), -3 * -5 * -7); // The maximum product should be -3 * -5 * -7 = -105.
}
// Test when the array contains three elements with mixed values.
TEST(MaxProductOfThreeTest, ThreeMixedElements)
{
std::vector<int> A = { -3, 5, -7 };
EXPECT_EQ(maxProductOfThree(A), -3 * 5 * -7); // The maximum product should be -3 * 5 * -7 = 105.
}
// Test when the array contains many elements with both positive and negative values.
TEST(MaxProductOfThreeTest, ManyElementsMixedValues)
{
std::vector<int> A = { -10, -2, 0, 5, 8, -9, -7, 12 };
EXPECT_EQ(maxProductOfThree(A), -10 * -9 * 12); // The maximum product should be -10 * -9 * 12 = 1080.
}
// Test when N = 3 (minimum value for N) and all elements are positive.
TEST(MaxProductOfThreeTest, ThreePositiveElementsMinN)
{
std::vector<int> A = { 2, 3, 5 };
EXPECT_EQ(maxProductOfThree(A), 2 * 3 * 5); // The maximum product should be 2 * 3 * 5 = 30.
}
// Test when N = 3 (minimum value for N) and all elements are negative.
TEST(MaxProductOfThreeTest, ThreeNegativeElementsMinN)
{
std::vector<int> A = { -2, -3, -5 };
EXPECT_EQ(maxProductOfThree(A), -2 * -3 * -5); // The maximum product should be -2 * -3 * -5 = -30.
}
// Test when N = 3 (minimum value for N) and elements are mixed.
TEST(MaxProductOfThreeTest, ThreeMixedElementsMinN)
{
std::vector<int> A = { -2, 3, -5 };
EXPECT_EQ(maxProductOfThree(A), -2 * 3 * -5); // The maximum product should be -2 * 3 * -5 = 30.
}
// Test when N = 100000 (maximum value for N) and elements are all zeros.
TEST(MaxProductOfThreeTest, AllZeroElementsMaxN)
{
std::vector<int> A(100000, 0);
EXPECT_EQ(maxProductOfThree(A), 0); // All elements are zeros, so the maximum product should be 0.
}
// Test when N = 100000 (maximum value for N) and elements are all positive.
TEST(MaxProductOfThreeTest, AllPositiveElementsMaxN)
{
std::vector<int> A(100000, 5);
EXPECT_EQ(maxProductOfThree(A), 5 * 5 * 5); // All elements are the same (5), so the maximum product should be 5 * 5 * 5 = 125.
}
// Test when N = 100000 (maximum value for N) and elements are all negative.
TEST(MaxProductOfThreeTest, AllNegativeElementsMaxN)
{
std::vector<int> A(100000, -5);
EXPECT_EQ(maxProductOfThree(A), -5 * -5 * -5); // All elements are the same (-5), so the maximum product should be -5 * -5 * -5 = -125.
}
// Lesson 6 (Sorting) - Triangle
// -------------------------------------------------
// Test when the array is empty (N = 0).
TEST(TriangleTest, EmptyArray)
{
std::vector<int> A;
EXPECT_EQ(triangle(A), 0); // An empty array cannot form a triangular triplet.
}
// Test when the array contains only two elements (N = 2).
TEST(TriangleTest, TwoElements)
{
std::vector<int> A = { 5, 10 };
EXPECT_EQ(triangle(A), 0); // With only two elements, it's impossible to form a triangular triplet.
}
// Test when the array contains three elements that do not form a triangular triplet.
TEST(TriangleTest, NonTriangularTriplet)
{
std::vector<int> A = { 10, 2, 20 };
EXPECT_EQ(triangle(A), 0); // The elements 10, 2, and 20 do not form a triangular triplet.
}
// Test when the array contains many elements with a large range of values.
TEST(TriangleTest, LargeRangeValues)
{
std::vector<int> A = { -2147483648, 0, 2147483647 };
EXPECT_EQ(triangle(A), 0); // The large range of values doesn't form a triangular triplet.
}
// Test when N = 100000 (maximum value for N) and all elements are zeros.
TEST(TriangleTest, LargeArrayAllZeros)
{
std::vector<int> A(100000, 0);
EXPECT_EQ(triangle(A), 0); // With all elements being zeros, it's impossible to form a triangular triplet.
}
// Test when N = 100000 (maximum value for N) and all elements are positive.
TEST(TriangleTest, LargeArrayAllPositive)
{
std::vector<int> A(100000, 5);
EXPECT_EQ(triangle(A), 1); // All elements are the same (5), so a triangular triplet exists.
}
// Test when N = 100000 (maximum value for N) and all elements are negative.
TEST(TriangleTest, LargeArrayAllNegative)
{
std::vector<int> A(100000, -5);
EXPECT_EQ(triangle(A), 0); // All elements are the same (-5), but it's still impossible to form a triangular triplet.
}
// Test when N = 100000 (maximum value for N) and elements are random.
TEST(TriangleTest, LargeArrayRandomElements)
{
std::vector<int> A;
for (int i = 0; i < 100000; ++i) {
A.push_back(rand() % 1000000 - 500000); // Generate random elements between -500000 and 499999.
}
EXPECT_TRUE(triangle(A) == 0 || triangle(A) == 1); // A random array may or may not form a triangular triplet.
}
// Lesson 7 (Stacks and queues) - Brackets
// -------------------------------------------------
// Test when the string is empty.
TEST(BracketsTest, EmptyString)
{
std::string S = "";
EXPECT_EQ(brackets(S), 1); // An empty string is considered properly nested.
}
// Test when the string contains a single pair of brackets.
TEST(BracketsTest, SinglePairOfBrackets)
{
std::string S1 = "{}";
std::string S2 = "[]";
std::string S3 = "()";
EXPECT_EQ(brackets(S1), 1); // Properly nested.
EXPECT_EQ(brackets(S2), 1); // Properly nested.
EXPECT_EQ(brackets(S3), 1); // Properly nested.
}
// Test when the string contains multiple pairs of brackets.
TEST(BracketsTest, MultiplePairsOfBrackets)
{
std::string S1 = "{[()]()}";
std::string S2 = "[({})]";
std::string S3 = "{[()]";
std::string S4 = "{[()]}}";
EXPECT_EQ(brackets(S1), 1); // Properly nested.
EXPECT_EQ(brackets(S2), 1); // Properly nested.
EXPECT_EQ(brackets(S3), 0); // Not properly nested.
EXPECT_EQ(brackets(S4), 0); // Not properly nested.
}
// Test when the string contains unbalanced brackets.
TEST(BracketsTest, UnbalancedBrackets)
{
std::string S1 = "{[()]())}";
std::string S2 = "[(])";
EXPECT_EQ(brackets(S1), 0); // Not properly nested.
EXPECT_EQ(brackets(S2), 0); // Not properly nested.
}
// Test when the string contains 200,000 opening and closing brackets.
TEST(BracketsTest, LargeNumberOfBrackets)
{
std::string S(200000, '('); // Create a string with 200,000 opening brackets.
S += std::string(200000, ')'); // Append 200,000 closing brackets.
// The string consists of pairs of brackets, so it's properly nested.
EXPECT_EQ(brackets(S), 1);
}
// Test when the string contains 200,000 brackets in a non nested pattern.
TEST(BracketsTest, LargeNotNestedBrackets)
{
std::string S;
for (int i = 0; i < 100000; ++i) {
S += "({[";
}
for (int i = 0; i < 100000; ++i) {
S += ")}]";
}
EXPECT_EQ(brackets(S), 0);
}
// Test when the string contains 200,000 brackets in a nested pattern.
TEST(BracketsTest, LargeNestedBrackets)
{
std::string S;
for (int i = 0; i < 100000; ++i) {
S += "({[";
}
for (int i = 0; i < 100000; ++i) {
S += "]})";
}
EXPECT_EQ(brackets(S), 1);
}
// Lesson 7 (Stacks and queues) - Fish
// -------------------------------------------------
// Test case for the provided example.
TEST(FishTest, ProvidedExample)
{
std::vector<int> A = { 4, 3, 2, 1, 5 };
std::vector<int> B = { 0, 1, 0, 0, 0 };
EXPECT_EQ(fish(A, B), 2); // Expected result is 2 (fish 0 and fish 4 stay alive).
}
// Test case for all fish moving in the same direction (upstream).
TEST(FishTest, AllUpstream)
{
std::vector<int> A = { 4, 3, 2, 1, 5 };
std::vector<int> B = { 0, 0, 0, 0, 0 };
EXPECT_EQ(fish(A, B), 5); // Expected result is 5 (all fish stay alive).
}
// Test case for all fish moving in the same direction (downstream).
TEST(FishTest, AllDownstream)
{
std::vector<int> A = { 4, 3, 2, 1, 5 };
std::vector<int> B = { 1, 1, 1, 1, 1 };
EXPECT_EQ(fish(A, B), 5); // Expected result is 5 (all fish stay alive).
}
// Test case when all fish will stay alive.
TEST(FishTest, AllStayAlive)
{
std::vector<int> A = { 1, 2, 3, 4, 5 };
std::vector<int> B = { 0, 1, 1, 1, 1 };
EXPECT_EQ(fish(A, B), 5);
}
// Test case for large numbers.
TEST(FishTest, LargeNumbers)
{
std::vector<int> A = { 1000000000, 999999999, 500000000, 1, 1000000000 };
std::vector<int> B = { 1, 1, 0, 0, 1 };
EXPECT_EQ(fish(A, B), 3); // Expected result is 3.
}
// Test case for all fish moving in the same direction (upstream) with large numbers.
TEST(FishTest, LargeNumbersAllUpstream)
{
std::vector<int> A = { 1000000000, 999999999, 500000000, 1, 1000000000 };
std::vector<int> B = { 0, 0, 0, 0, 0 };
EXPECT_EQ(fish(A, B), 5); // Expected result is 5 (all fish stay alive).
}
// Test case for all fish moving in the same direction (downstream) with large numbers.
TEST(FishTest, LargeNumbersAllDownstream)
{
std::vector<int> A = { 1000000000, 999999999, 500000000, 1, 1000000000 };
std::vector<int> B = { 1, 1, 1, 1, 1 };
EXPECT_EQ(fish(A, B), 5); // Expected result is 5 (all fish stay alive).
}
// Test case for a large number of elements with fish moving in the same direction (upstream).
TEST(FishTest, LargeCountAllUpstream)
{
const int N = 100000;