Skip to content

Instantly share code, notes, and snippets.

// manually transpose a 3x3 matrix
/*
a b c a d g
d e f ~ b e h
g h i c f i
*/
#include <iostream>
#include <vector>
// how to swap two numbers without using a temporary variable
// think about it this way
// x <-- a
// y <-- b
// x' <-- x - y = a - b
// y' <-- x' + y = ( a - b ) + b = a
// x''<-- y' - x' = a - ( a - b ) = b
#include <iostream>
// How do you check if a string contains only digits?
#include <iostream>
#include <string>
#include <algorithm>
std::string your_string = "012345678774829506432X";
std::string my_set = "0123456789";
std::string::iterator it = my_set.begin();
// How can a given string be reversed using recursion?
#include <iostream>
#include <string>
std::string reverse( const std::string& str )
{
std::string _str = str;
if ( _str.size() > 1 )
return reverse( _str.substr( 1, _str.size() - 1 ) ).append( _str.substr( 0, 1 ) );
// How do you print the first non-repeated character from a string?
#include <iostream>
#include <string>
std::string my_str = "aaaaaaabccccccc";
int main ()
{
int index;
// How do you check if two strings are anagrams of each other?
#include <iostream>
#include <string>
#include <algorithm>
std::string ana = "cinema";
std::string gram = "iceman";
int main ()
// How do you print duplicate characters from a string?
#include <iostream>
#include <string>
#include <set>
std::string my_string = "hello world";
std::string your_string = "bye world";
std::set<char> my_set;
// How do you find duplicate numbers in an array if it contains multiple duplicates?
#include <iostream>
#include <array>
#include <map>
std::array<int, 10> arr = { 1, 2, 9, 4, 1, 6, 7, 3, 9, 1 };
std::map<int, int> my_hash;
// How do you find all pairs of an integer array whose sum is equal to a given number?
#include <iostream>
#include <array>
int desired_sum = 10;
std::array<int, 10> arr = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
int main ()
{
// How do you find the largest and smallest number in an unsorted integer array?
#include <iostream>
#include <vector>
auto main() -> int
{
std::vector<int> unsorted_arr = { 9, 2, 3, 4, 5, 10, 7, 8, 1, 6 };
int* max_index = &unsorted_arr[0];