-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
EX0401_CountOccurrencees - VSCode version
- Loading branch information
1 parent
d6ce91c
commit eb67abd
Showing
1 changed file
with
81 additions
and
0 deletions.
There are no files selected for viewing
81 changes: 81 additions & 0 deletions
81
VSCode/Ex0401_CountOccurrences/Ex0401_CountOccurrences.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
#include <iostream> | ||
#include <vector> | ||
#include <random> | ||
#include <numeric> | ||
#include <iostream> | ||
|
||
using namespace std; | ||
|
||
void Print(vector<int>& arr) | ||
{ | ||
for (int i = 0; i < arr.size(); i++) | ||
cout << arr[i] << " "; | ||
cout << endl; | ||
} | ||
|
||
int Count1(const vector<int>& arr, int x) | ||
{ | ||
//TODO: O(n) | ||
return 0; | ||
} | ||
|
||
int Count2(const vector<int>& arr, int x) | ||
{ | ||
//TODO: O(log(n) + count) | ||
return 0; | ||
} | ||
|
||
int Count3(const vector<int>& arr, int x) | ||
{ | ||
//TODO: O(log(n)) | ||
return 0; | ||
} | ||
|
||
int main() | ||
{ | ||
random_device rd; | ||
mt19937 gen(rd()); | ||
|
||
const int n = 20; | ||
vector<int> my_vector(n); | ||
|
||
int x = 6; // target to find | ||
|
||
for (int r = 0; r < 100; r++) | ||
{ | ||
uniform_int_distribution<int> value_distribution(1, 10); | ||
generate(my_vector.begin(), my_vector.end(), [&]() { return value_distribution(gen); }); | ||
sort(my_vector.begin(), my_vector.end()); | ||
|
||
Print(my_vector); | ||
|
||
const int expected_count = std::count(my_vector.begin(), my_vector.end(), x); | ||
|
||
cout << "Expected count = " << expected_count << endl; | ||
|
||
// 1. O(n) brute force | ||
if (Count1(my_vector, x) != expected_count) | ||
{ | ||
cout << "Wrong count1: " << Count1(my_vector, x) << endl; | ||
exit(-1); | ||
} | ||
|
||
// 2. O(log(n) + count) | ||
if (Count2(my_vector, x) != expected_count) | ||
{ | ||
cout << "Wrong count2: " << Count2(my_vector, x) << endl; | ||
exit(-1); | ||
} | ||
|
||
// 3. O(log(n)) | ||
if (Count3(my_vector, x) != expected_count) | ||
{ | ||
cout << "Wrong count3: " << Count3(my_vector, x) << endl; | ||
exit(-1); | ||
} | ||
} | ||
|
||
cout << "Good!" << endl; | ||
|
||
return 0; | ||
} |