This file contains hidden or 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
#include <algorithm> | |
#include <chrono> | |
#include <iostream> | |
#include <iterator> | |
#include <memory> | |
#include <random> | |
#include <unordered_map> | |
// Efficiency with Algorithms, Performance with Data Structures | |
// CppCon 2014: Chandler Carruth |
This file contains hidden or 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
// You are given a sorted array of numbers where each number represents a | |
// candidate in an election. For example, in the list below candidate 1 received | |
// 2 votes, candidate 2 received 1 vote, candidate 3 received 6 votes, etc. | |
// [1, 1, 2, 3, 3, 3, 3, 3, 3, 4, 5] | |
// A candidate wins an election if and only if they have strictly greater than | |
// 50% of the votes. | |
// Write a function to determine the winning candidate, if any. | |
#include <optional> | |
#include <vector> |
This file contains hidden or 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
#include <iostream> | |
bool PairityA(int n) { | |
bool res = 0; | |
while(n) { | |
res ^= n & 1; | |
n >>= 1; | |
} | |
return res; | |
} |
This file contains hidden or 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
#include <string> | |
#include <map> | |
bool IsPermutation(std::string &a, std::string &b) { | |
std::map<char, std::pair<int, int>> char_map; | |
for (auto c : a) ++char_map[c].first; | |
for (auto c : b) ++char_map[c].second; | |
for (auto it : char_map) | |
if (it.second.first != it.second.second) return false; | |
return true; |
This file contains hidden or 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
#include <iostream> | |
#include <vector> | |
struct Node { | |
char value; | |
Node *next; | |
}; | |
class LinkedList { | |
public: |
This file contains hidden or 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
#include <iostream> | |
#include <vector> | |
namespace String { | |
std::string Reverse(std::string str) { | |
char temp; | |
for (unsigned long i = 0; 2*i < str.length(); ++i) { | |
temp = str[i]; | |
str[i] = str[str.length()-i-1]; | |
str[str.length()-i-1] = temp; |
This file contains hidden or 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
int oddOccurrencesInArray(int A[], int N) { | |
int value = 0; | |
for (int i = 0; i < N; ++i) value ^= A[i]; | |
return value; | |
} | |
// Source: https://codility.com/programmers/task/odd_occurrences_in_array/ | |
// Score: 100% | |
// That's all folks! |
This file contains hidden or 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
/* | |
Task description | |
A zero-indexed array A consisting of N integers is given. Rotation of the array | |
means that each element is shifted right by one index, and the last element of | |
the array is also moved to the first place. | |
For example, the rotation of array A = [3, 8, 9, 7, 6] is [6, 3, 8, 9, 7]. The | |
goal is to rotate array A K times; that is, each element of A will be shifted to | |
the right by K indexes. |
This file contains hidden or 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
/* | |
fizzBuzz prints integers from 1 to max. For multiples of three, “Fizz” is | |
printed instead of the number. For multiples of five, “Buzz” is printed instead | |
of the number. For numbers which are multiples of both three and five, | |
“FizzBuzz” is printed instead of the number. | |
*/ | |
#include <stdio.h> | |
void fizzBuzz(int max) { |