Skip to content

Commit

Permalink
EX0401_CountOccurrencees - VSCode version
Browse files Browse the repository at this point in the history
  • Loading branch information
HongLabInc committed Mar 12, 2024
1 parent d6ce91c commit eb67abd
Showing 1 changed file with 81 additions and 0 deletions.
81 changes: 81 additions & 0 deletions VSCode/Ex0401_CountOccurrences/Ex0401_CountOccurrences.cpp
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;
}

0 comments on commit eb67abd

Please sign in to comment.