Skip to content

Instantly share code, notes, and snippets.

View lnikon's full-sized avatar

Vahag Bejanyan lnikon

View GitHub Profile
#include <iostream>
#include <vector>
template <class T>
size_t partition(std::vector<T>& vec, size_t p, size_t q);
template <class T>
void quicksort_util(std::vector<T>& vec, size_t p, size_t r);
template <class T>
@lnikon
lnikon / binary_search_without_iterators.cpp
Created June 27, 2018 11:48
Binary Search Implementation
#include <vector>
#include <iostream>
#include <utility>
template <class T>
auto binarySearch(std::vector<T>& vec, const T& key)
{
size_t left = 0;
size_t right = vec.size() + 1;
size_t middle = (left + right) / 2;
@lnikon
lnikon / bsearch.c
Created July 23, 2018 08:28
Binary Search from Linux Kernel /lib/bsearch.c
/*
* A generic implementation of binary search for the Linux kernel
*
* Copyright (C) 2008-2009 Ksplice, Inc.
* Author: Tim Abbott <[email protected]>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; version 2.
*/
@lnikon
lnikon / revwrd.cpp
Created July 29, 2018 13:53
Reverse words in a given string
#include <iostream>
#include <string>
#include <algorithm>
void solution(std::string str) {
str += " ";
auto size = str.size();
size_t start = 0;
for(size_t i = 0; i < size; i++) {
size_t end = str.find(' ', start);
@lnikon
lnikon / fast_power.cpp
Created August 8, 2018 07:35
Binary Power
#include <vector>
#include <iostream>
using ull = unsigned long long;
ull fast_power(ull num, int power)
{
if(num < 0 || power < 0) {
std::cout << "invalid arguments specified\n";
}
@lnikon
lnikon / from2tonprimes.cpp
Created August 9, 2018 07:05
Prime numbers from 2 to N(O(N^2) worst case complexity)
#include <iostream>
#include <cmath>
#include <vector>
using ull = unsigned long long;
bool isPrime(int n)
{
for(int i = 2; i < n / 2; i++)
{
@lnikon
lnikon / counting_sort.cpp
Created August 9, 2018 20:17
Counting sort.
template <class T>
auto countingSort(const std::vector<T>& vec)
{
if(vec.size() <= 1) std::move(vec);
std::vector<T> counts(vec.size(), 0);
const auto size = vec.size();
for(size_t i = 0; i < size - 1; ++i)
{
1) Find the last element of a list.
xs = [1, 2, 3, 4]
last xs
2) Find the last but one element of a list.
xs = [1, 2, 3, 4]
last (init xs)
3) Find the K'th element of a list. The first element in the list is number 1.
xs = [1, 2, 4, 3, 5, 6, 8, 7]
#include <iostream>
#include <unordered_map>
#include <vector>
#include <string>
#include <algorithm>
template <class T>
auto&& minusKFromAll(std::vector<T> v, T k) {
std::transform(std::begin(v), std::end(v), std::begin(v), [](T i) { return i - 1; });
@lnikon
lnikon / lis.cpp
Last active March 22, 2019 13:54
Longest Increasing subsequence
#include <vector>
#include <iostream>
#include <algorithm>
#include <iterator>
template <class ForwardIterator, class Comp = std::less<>>
auto lis(ForwardIterator begin,
ForwardIterator end,
Comp comp = Comp()) {