MLSAKIIT
Published in

MLSAKIIT

Competitive Programming Algorithms Every Beginner Must Know

Competitive Programming
Harvard University Professor David Malan using his classic phonebook example to illustrate a binary search algorithm
int binarySearch(int arr[], int l, int r, int x){if (r >= l) {int mid = l + (r - l) / 2if (arr[mid] == x)return mid;if (arr[mid] > x)return binarySearch(arr, l, mid - 1, x);return binarySearch(arr, mid + 1, r, x);}return -1;}
int binary_exponentiation(int a, int b)
{
if(b==0) return 1; long long int value=binary_exponentiation(a,b/2); if(b%2==0)
{
return value*value; } else
{
return a*value*value; }}
A truth table illustrating the results on applying the said operators
if(n%2 ! = 0) print(“ODD”);
if(n&1) print(“ODD”);
void SieveOfEratosthenes(int n){    bool prime[n + 1];    memset(prime, true, sizeof(prime));    for (int p = 2; p * p <= n; p++)    {        if (prime[p] == true)        {             for (int i = p * p; i <= n; i += p)                 prime[i] = false;        }}for (int p = 2; p <= n; p++)if (prime[p])cout << p << " ";}

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store