BinaryGap
// Link to test: https://codility.com/demo/take-sample-test/binary_gap/
// "Find longest sequence of zeros in binary representation of an integer."
std::string dec2bin(int dec){
std::string bin;
while(dec != 0){
bin += (dec & 1) ? '1' : '0';
dec >>= 1;
}
return bin;
}
int binary_gap(int N){
std::string bin_num = dec2bin(N);
int gap = 0;
int highest = 0;
int mark = 0;
for(int i = 0; i < bin_num.size(); i++){
if(bin_num[i] == '1'){
gap = i - mark;
mark = i;
if(gap > highest) highest = gap - 1;
}
}
return highest;
}
CountDiv
// Link to test: https://codility.com/demo/take-sample-test/count_div/
// "Compute number of integers divisible by K in the range A to B."
int count_div(int A, int B, int K){
return (B / K) - (A / K) + !(A % K);
}
FrogJmp
// Link to test: https://codility.com/demo/take-sample-test/frog_jmp/
// "Count minimal number of jumps from position X to Y."
int frog_jmp(int X, int Y, int D){
int jumps = (Y - X) / D;
if((Y - X) % D) jumps++;
return jumps;
}
MinAvgTwoSlice
// Link to test: https://codility.com/demo/take-sample-test/min_avg_two_slice/
// "Find the minimal average of any slice containing at least two elements."
// "100/100" solution. Only checks for slices of 2 and 3 elements (which is not noted in the description of problem).
int min_avg_two_slice(vector<int> &A){
int size = A.size(), low_index = 0;
float low = (A[0] + A[1]) / 2, temp = 0;
for(int i = 0; i < size - 2; i++){
temp = float(A[i] + A[i + 1]) / 2;
if(temp < low){
low = temp;
low_index = i;
}
temp = float(A[i] + A[i + 1] + A[i + 2]) / 3;
if(temp < low){
low = temp;
low_index = i;
}
}
temp = float(A[size - 1] + A[size - 2]) / 2;
if(temp < low){
low = temp;
low_index = size - 2;
}
return low_index;
}
PassingCars
// Link to test: https://codility.com/demo/take-sample-test/passing_cars/
// "Count the number of passing cars on the road."
// "100/100" solution. Always assumes the first car is headed east (which is not noted in the description of the problem).
int passing_cars(vector<int> &A){
int east = 0, passing = 0;
for(int i = 0; i < A.size(); i++){
if(A[i] == 0) east++;
if(A[i] != 0) passing += east;
if(passing > 1000000000) return -1;
}
return passing;
}
// Correct solution based on description of problem given. Checks whether or not the first car is headed east or west (scores 90/100).
int passing_cars(vector<int> &A){
int key_cars = 0, passing = 0;
for(int i = 0; i < A.size(); i++){
if(A[i] == A[0]) key_cars++;
if(A[i] != A[0]) passing += key_cars;
if(passing > 1000000000) return -1;
}
return passing;
}
PermCheck
// Link to test: https://codility.com/demo/take-sample-test/perm_check/
// "Check whether array A is a permutation."
// Relies heavily on <algorithm>, but scores 100/100. The for loop is range-based.
#include <algorithm>
int perm_check(vector<int> &A){
int t_num = (A.size() * (A.size() + 1)) / 2;
std::sort(A.begin(), A.end());
A.erase(std::unique(A.begin(), A.end()), A.end());
int sum = 0;
for(int n : A) sum += n;
if(t_num - sum == 0) return 1;
return 0;
}
PermMissingElem
// Link to test: https://codility.com/demo/take-sample-test/perm_missing_elem/
// "Find the missing element in a given permutation."
int perm_missing_elem(vector<int> &A){
int total = 0;
int max = 0;
for(int i = 0; i < A.size(); i++) total = total + A[i];
for(int i = 1; i <= A.size() + 1; i++) max = max + i;
return max - total;
}
TapeEquilibrium
// Link to test: https://codility.com/demo/take-sample-test/tape_equilibrium/
// "Minimize the value |(A[0] + ... + A[P-1]) - (A[P] + ... + A[N-1])|."
int tape_equilibrium(vector<int> &A) {
int left = A[0];
int right = 0;
for(int i = 1; i < A.size(); i++) right += A[i];
int minimum = abs(left - right);
int difference = minimum;
for(int i = 1; i < A.size() - 1; i++){
left += A[i];
right -= A[i];
difference = abs(left - right);
if(difference < minimum) minimum = difference;
}
return minimum;
}