Skip to content

Instantly share code, notes, and snippets.

View sumanth232's full-sized avatar

Sumanth Bandi sumanth232

View GitHub Profile
@sumanth232
sumanth232 / mytemplate.cpp
Last active May 2, 2020 04:03
All the #defines, typedefs, written for C++ (works in C++ 11 only)
#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
@sumanth232
sumanth232 / map.cpp
Last active December 19, 2015 01:59
Given an array, find all the pairs of elements whose sum is k . Soln :: By Anton Lunyov (using STL map)
// http://discuss.codechef.com/questions/3292/algorithm-given-an-array-find-all-the-pairs-of-elements-whose-sum-is-k
/*For the original problem here is very simple O(N * log N) solution using STL map.
Put the array in map: */
map < int, int > cnt;
for (int i = 0; i < n; ++i) {
cnt[a[i]]++;
}
@sumanth232
sumanth232 / Interview Written Test Papers
Created July 3, 2013 20:05
Interview Written Test Papers
http://sameerbsws.blogspot.in/
http://learn.hackerearth.com/tests/9/
@sumanth232
sumanth232 / compare double.cpp
Created July 8, 2013 11:03
Comparing double variables considering the the very important precision
//When I need to compare double values, I just make a function:
int CompareDouble(double a, double b)
{
if(a < b - EPS)
return -1;
if(a > b + EPS)
return 1;
return 0;
}
@sumanth232
sumanth232 / Precision in Float
Created July 8, 2013 18:39
Taking care of Precision in calculations involving floating point numbers
When I need to compare double values, I just make a function:
int CompareDouble(double a, double b)
{
if(a < b - EPS)
return -1;
if(a > b + EPS)
return 1;
return 0;
}
@sumanth232
sumanth232 / Suffix_Array.cpp
Created July 10, 2013 12:56
1) Suffix Array construction using - Manber Myers algorithm in O(n log n) time ( O(n) - Karkkainan Sander's algorithm also there ) 2) Linear-Time Longest-Common-Prefix Computation in Suffix
/* By the author :
I'd recommend reading the paper "Linear-Time Longest-Common-Prefix Computation in Suffix Arrays and Its Applications" by Toru Kasai, Gunho Lee, Hiroki Arimura, Setsuo Arikawa, and Kunsoo Park.
It shows how to find the length of the longest common prefixes between consecutive suffixes in lineal time, in around 10 lines of code (really, see code in edit).
I also wanted to share my implementation of Manber & Myers algorithm which is also on edit. I got Accepted on SUBST1 with it. */
// Begins Suffix Arrays implementation
// O(n log n) - Manber and Myers algorithm
// Refer to "Suffix arrays: A new method for on-line string searches",
@sumanth232
sumanth232 / split_string.c
Last active December 20, 2015 14:39
split function in C ( written by myself )
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define len 200
typedef struct _string
{
char tokens[30][200]; // tokens can be treated as an array of strings
int no_of_tokens;
@sumanth232
sumanth232 / max_subarray_sum.c
Created August 24, 2013 12:22
Finds the maximum contiguous sum in an array
#include <stdio.h>
#include <stdlib.h>
int max_subarray_sum(int a[],int size)
{
int i;
int cumulative_sum = a[0];
int maxsofar = a[0];
int cumulative_min = a[0];
@sumanth232
sumanth232 / measurement_of_time.c
Created September 29, 2013 08:35
Measure the time of a function in seconds
#include<stdio.h>
#include<conio.h>
#include<time.h>
void fun()
{
printf("fun() starts \n");
printf("Press enter to stop fun \n");
while(1)
{
@sumanth232
sumanth232 / modpower.c
Last active December 25, 2015 03:59
a^b % mod
#define mod 1000000007LL
typedef long long ll;
ll power(ll a, ll b)
{
ll result = 1;
while (b){
if (b&1){ // if(b%2 == 1)
result = ((result % mod) * (a % mod)) % mod;