-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 07b81d1
Showing
10 changed files
with
320 additions
and
0 deletions.
There are no files selected for viewing
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,15 @@ | ||
cmake_minimum_required(VERSION 2.8) | ||
|
||
project(codility) | ||
|
||
set(CMAKE_BUILD_TYPE Debug) | ||
set(CMAKE_CXX_STANDARD 14) | ||
|
||
add_executable(binary_gap binary_gap.cpp) | ||
add_executable(arrays_cyclic_rotation arrays_cyclic_rotation.cpp) | ||
add_executable(odd_occurrences_in_array odd_occurrences_in_array.cpp) | ||
add_executable(perm_missing_elem perm_missing_elem.cpp) | ||
add_executable(frogjmp frogjmp.cpp) | ||
add_executable(tape_equilibrium tape_equilibrium.cpp) | ||
add_executable(counting_perm_check counting_perm_check.cpp) | ||
add_executable(frog_river_one frog_river_one.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,5 @@ | ||
Solution for Codility's (https://www.codility.com/) programming exercises, in c++. | ||
I'll be adding more solutions over time. | ||
|
||
Author: Diego dos Santos Fronza | ||
[email protected] |
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,38 @@ | ||
#include <iterator> | ||
#include <iostream> | ||
#include <vector> | ||
#include <cstddef> | ||
using namespace std; | ||
|
||
vector<int> solution(vector<int> &A, int K) | ||
{ | ||
if (K == 0 || K == A.size()) { | ||
return A; | ||
} | ||
|
||
vector<int> rotated(A.size()); | ||
|
||
for (std::size_t i = 0; i < A.size(); ++i) { | ||
rotated[(i + K) % A.size()] = A[i]; | ||
} | ||
|
||
return rotated; | ||
} | ||
|
||
int main() | ||
{ | ||
std::vector<int> vec = {1, 2, 3, 4, 5}; | ||
|
||
for (int i = 0; i <= (vec.size() + 2); i++) { | ||
vector<int> test(vec); | ||
cout << "Rotating vector by " << i << endl; | ||
std::copy(begin(test), end(test), ostream_iterator<int>(cout, " ")); | ||
cout << endl; | ||
|
||
test = solution(test, i); | ||
std::copy(begin(test), end(test), ostream_iterator<int>(cout, " ")); | ||
cout << endl << endl; | ||
} | ||
|
||
return 0; | ||
} |
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,39 @@ | ||
#include <iostream> | ||
|
||
using namespace std; | ||
|
||
int binary_gap(int n) | ||
{ | ||
unsigned mask = (1u << 30); | ||
bool one = false; | ||
int bin_gap = 0; | ||
int max_gap = 0; | ||
|
||
while (mask) { | ||
if (one) { | ||
if ((n & mask) == 0) { | ||
++bin_gap; | ||
} else { | ||
if (max_gap < bin_gap) { | ||
max_gap = bin_gap; | ||
} | ||
bin_gap = 0; | ||
} | ||
} else { | ||
one = (n & mask) != 0 ; | ||
} | ||
mask >>= 1; | ||
} | ||
|
||
return max_gap; | ||
} | ||
|
||
int main() | ||
{ | ||
cout << binary_gap(9) << endl; | ||
cout << binary_gap(529) << endl; | ||
cout << binary_gap(20) << endl; | ||
cout << binary_gap(15) << endl; | ||
cout << binary_gap(32) << endl; | ||
return 0; | ||
} |
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,41 @@ | ||
#include <vector> | ||
#include <iostream> | ||
#include <algorithm> | ||
using namespace std; | ||
|
||
int solution(vector<int> &A) | ||
{ | ||
if (A.empty()) { | ||
return 0; | ||
} | ||
|
||
std::sort(begin(A), end(A)); | ||
|
||
if (A[0] != 1 || A.back() != A.size()) { | ||
return 0; | ||
} | ||
|
||
for (std::size_t i = 0, limit = A.size() - 1; i < limit; ++i) { | ||
if ((A[i] + 1) != A[i + 1]) { | ||
return 0; | ||
} | ||
} | ||
|
||
return 1; | ||
} | ||
|
||
int main() | ||
{ | ||
std::vector<vector<int>> inputs = { | ||
{4, 1, 3, 2}, | ||
{4, 1, 3}, | ||
{2, 3, 4, 5}, | ||
{1, 2, 3, 4, 5, 7} | ||
}; | ||
|
||
for (std::size_t i = 0; i < inputs.size(); ++i) { | ||
cout << solution(inputs[i]) << endl; | ||
} | ||
|
||
return 0; | ||
} |
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,45 @@ | ||
#include <vector> | ||
#include <iostream> | ||
#include <algorithm> | ||
#include <set> | ||
using namespace std; | ||
|
||
int solution(int X, vector<int> &A) | ||
{ | ||
// If no leaves falling or less leaves then X, can't cross the river | ||
if (A.empty() || A.size() < X) { | ||
return -1; | ||
} | ||
|
||
std::set<int> jumps; // set with all required jumps, when we find X and this set is empty, frog can cross the river | ||
for (int i = 1; i < X; ++i) { | ||
jumps.insert(i); | ||
} | ||
|
||
for (std::size_t i = 0; i < A.size(); ++i) { | ||
int leave = A[i]; | ||
if (leave == X) { | ||
if (jumps.empty()) { | ||
return i; | ||
} | ||
} else { | ||
auto element = jumps.find(leave); | ||
if (element != jumps.end()) { | ||
jumps.erase(element); | ||
} | ||
} | ||
} | ||
|
||
return -1; | ||
} | ||
|
||
int main() | ||
{ | ||
std::vector<int> jumps = {1, 3, 1, 4, 2, 3, 5, 4}; | ||
|
||
std::cout << solution(5, jumps); | ||
|
||
cout << endl; | ||
|
||
return 0; | ||
} |
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,20 @@ | ||
#include <iostream> | ||
|
||
int solution(int X, int Y, int D) | ||
{ | ||
int distance = Y - X; | ||
int jumps = (distance) / D; | ||
if ((distance % D) != 0) { | ||
++jumps; | ||
} | ||
|
||
return jumps; | ||
} | ||
|
||
int main() | ||
{ | ||
using namespace std; | ||
cout << solution(30, 70, 7) << endl; | ||
|
||
return 0; | ||
} |
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,31 @@ | ||
#include <iostream> | ||
#include <vector> | ||
#include <algorithm> | ||
using namespace std; | ||
|
||
int solution(vector<int> &A) | ||
{ | ||
std::sort(begin(A), end(A)); | ||
|
||
for (std::size_t i = 0; i < (A.size() - 1); i += 2) { | ||
if (A[i] != A[i+1]) { | ||
return A[i]; | ||
} | ||
} | ||
|
||
return A[A.size() - 1]; | ||
} | ||
|
||
int main() | ||
{ | ||
std::vector<std::vector<int>> test { | ||
{1, 2, 1, 2, 3}, | ||
{9, 9, 5, 3, 5, 3, 18, 18, 2, 1, 2, 8, 7, 8, 7} | ||
}; | ||
|
||
for (int i = 0; i < test.size(); i++) { | ||
cout << solution(test[i]) << endl; | ||
} | ||
|
||
return 0; | ||
} |
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,41 @@ | ||
#include <iostream> | ||
#include <vector> | ||
#include <algorithm> | ||
using namespace std; | ||
|
||
int solution(vector<int> &A) | ||
{ | ||
if (A.empty()) { | ||
return 1; | ||
} | ||
|
||
std::sort(begin(A), end(A)); | ||
|
||
if (A[0] > 1) { | ||
return A[0]-1; | ||
} | ||
|
||
for (std::size_t i = 0, limit = (A.size() - 1); i < limit; ++i) { | ||
if ((A[i] + 1) < (A[i+1])) { | ||
return (A[i] + 1); | ||
} | ||
} | ||
|
||
return A.back()+1; | ||
} | ||
|
||
int main() | ||
{ | ||
std::vector<vector<int>> inputs = { | ||
{2, 3, 1, 5}, | ||
{7, 6, 5, 8, 3, 2, 1, 4, 9} | ||
}; | ||
|
||
for (std::size_t i = 0; i < inputs.size(); ++i) { | ||
cout << solution(inputs[i]) << endl; | ||
} | ||
|
||
return 0; | ||
} | ||
|
||
|
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,45 @@ | ||
#include <vector> | ||
#include <iostream> | ||
#include <limits> | ||
#include <cmath> | ||
#include <algorithm> | ||
#include <numeric> | ||
using namespace std; | ||
|
||
int solution(vector<int> &A) | ||
{ | ||
auto limit = A.size() - 1; | ||
auto left_sum = A[0]; | ||
auto right_sum = std::accumulate(next(begin(A)), end(A), 0); | ||
|
||
int min_diff = std::abs(left_sum - right_sum); | ||
|
||
for(std::size_t i = 1; i < limit; ++i){ | ||
left_sum += A[i]; | ||
right_sum -= A[i]; | ||
|
||
if (std::abs(left_sum - right_sum) < min_diff) { | ||
min_diff = std::abs(left_sum - right_sum); | ||
} | ||
} | ||
|
||
return min_diff; | ||
} | ||
|
||
|
||
int main() | ||
{ | ||
std::vector<std::vector<int>> inputs { | ||
{3, 1, 2, 4, 3}, | ||
{1, 9, 30, 1, 100}, | ||
{1, 2, 3, 4, 5}, | ||
{-1, -2, -3} | ||
}; | ||
|
||
for (std::size_t i = 0; i < inputs.size(); ++i) { | ||
cout << solution(inputs[i]) << endl; | ||
} | ||
|
||
return 0; | ||
} | ||
|