Skip to content

Instantly share code, notes, and snippets.

View rohith2506's full-sized avatar

Rohith Uppala rohith2506

View GitHub Profile
@rohith2506
rohith2506 / 10.cpp
Created March 3, 2015 05:40
Maximum sliding window
// Using deque
void max_slid_window(vector<int> v){
deque<int> q;
for(int i=0; i<k; i++){
while(!q.empty() && v[i] >= v[q.back()])
q.pop_back();
q.push_back(i);
}
for(int i=k+1; i<n; i++){
@rohith2506
rohith2506 / 9.cpp
Created March 3, 2015 05:30
Rearrange +ve and -ve elements in an array
// Use parititon method of quick sort
void quick_sort(vector<int> v){
int i=-1;
for(int j=0; j<n; j++){
if(a[j] < 0){
i++;
swap(a[i],a[j]);
}
}
@rohith2506
rohith2506 / 8.cpp
Created March 2, 2015 09:57
diameter of a binary tree
// diameter of binary tree
// consider a root --> diameter of a tree = max(if path goes through that root, if path does not go through that root)
// so diamter = max(lh+rh+1, max(ld,rd));
int diameter(node *t){
if(t != NULL){
int lh = height(t -> left);
int rh = height(t -> right);
int ld = diameter(t -> left);
@rohith2506
rohith2506 / 7.cpp
Created March 2, 2015 09:44
BST to sorted DLL
// Converting bst to sorted DLL
// go in in order and and keep track of previous node and make it previous to current node and current node next to this node.
void bsttodll(node *root, node **head){
if(root == NULL) return ;
static node *prev = NULL;
bsttodll(root->left, head);
if(prev == NULL) *head = root;
else {
root -> previous = prev;
prev -> next = previous;
@rohith2506
rohith2506 / 6.cpp
Created March 2, 2015 09:27
NGE of an array
// http://www.geeksforgeeks.org/next-greater-element/
// simple stack logic.
int nge(int a[]){
vector<int> nge;
stack<int> stk;
stk.push(a[0]);
for(int i=1; i<n; i++){
int next = a[i];
while( !stk.empty()){
@rohith2506
rohith2506 / 5.cpp
Created March 2, 2015 09:15
count number of inversions in an array (a variation of merge sort)
/*
It's a modification of merge sort
see for explanation:- http://www.geeksforgeeks.org/counting-inversions/
*/
int merge_sort(int a[], int low, int high){
if(high > low){
int mid = (low + high)/2;
inv_count = merge_sort(a, low, mid);
inv_count += merge_sort(a, mid+1, high);
return merge(a, temp, low, mid, high);
@rohith2506
rohith2506 / 4.cpp
Created March 2, 2015 08:25
Count the number of possible triangles
// As though it looks like O(n^3) but it actally is O(n^2). because of k initialized outisde and does not depenv on j value.
int possible_triangles(vector<int> a){
int count = 0;
for(int i=0; i<n-2; i++){
int k = i+2;
for(int j=i+1; j<n; j++){
while(k < n && a[i] + a[j] > a[k]) ++k;
count = count + (k-j-1);
}
@rohith2506
rohith2506 / 3.cpp
Created February 26, 2015 12:29
Trie Implementation in c++
http://ideone.com/eF7Nih
@rohith2506
rohith2506 / 3.cpp
Created February 26, 2015 12:05
Pancake sorting
//http://www.geeksforgeeks.org/pancake-sorting/
void flip(vector<int> v, int i){
int temp, start = 0;
while(start < i){
int temp = v[start];
v[start] = v[i];
v[i] = temp;
start++;
i--;
@rohith2506
rohith2506 / 2.cpp
Created February 26, 2015 11:29
Smallest substring with given string ( Optimized approach )
// Very good problem. But difficult to understand.
// http://leetcode.com/2010/11/finding-minimum-window-in-s-which.html
int search(vector<string> txt, vector<string> pat, string s1, string s2){
for(int i=0; i<s2.length(); i++) pat[(int)(s2[i])]++;
txt[256] = {0};
int begin = 0, end = 0;
while(end < s1.length()){
if(pat[s1[end]] == 0) continue;
txt[s1[end]]++l;