Wednesday, December 25, 2013

Q. N persons are standing in a circle and play a game which starts from 1. In this game we are passing each other baton and after count of D which ever person holds that baton is out from the game and then the game begins from the next person in the circle. Print the order in which the people gets out.

INPUT  N=5,  D=2;

OUTPUT:  2,4,1,5,3

First  2  gets  out  then  4  so  we  left  with  1  3  5
Game  starts  with  5  and  hence  1  gets  out  and  game  goes  like  that.........



Solution:-  We  are  initializing  an  array  with  size  N  with  a[i]=i;

Now  we  are  counting  (prevIndex+D)%length;  length--;  is  the  next  element  to  be  removed

length  is  updated  dynamically

And  the  element  removed  is  printed...

#include<stdio.h>

void remove(int*,int,int);

void remove(int *arr,int k,int n)
{
for(int i=k+1;i<n;i++)
    arr[i-1]=arr[i];
}


int main()
{
        int n=12;
        int d=2;
        int arr[n];
        int length=n;
        int v=0;
     
        for(int k=0;k<n;k++)
        arr[k]=k+1;  
     
     
        while(1)
        {
                if(length==1)
                break;
         
             if(v!=0)
                v=v+d-1;
                else
                v=v+d;
             
                if(v%length!=0 || v==0)
                v=v%length;
                else
                v=length;
     
     
        printf("%d\n",arr[v-1]);
     
        if(v!=0)
        remove(arr,v-1,length);
        else
        remove(arr,v,length);
     
        length--;
     
        if(v>length)
        v=0;
     
        }
        printf("%d",arr[0]);  
        return 0;
}

If  you  fell  asking  anything  please  tell  me  anytime....


Q. Basic fibonaci program using DP

This  is  the  first  program  for  understanding  DP

You  must  be  knowing  Fib(n)=Fib(n-1)+Fib(n-2)

Lets  see  the  function  calls  of  Fib(6)







Here  we  can  see  Fib(4)  is  called  two  times  and  same  goes  with  Fib(3)  and  other  function  calls.  So  we  can  reduce  our  complexity  by  using  arrays  for  storage....

Just  where  there  is  returning  condition i.e Fib(0)=0,,  we  make  Fib[0]=0

and  make  Fib[n]=Fib[n-1]+Fib[n-2]  we  can  avoid  unecessary  calls  to  functions  again  and  again.....

#include<stdio.h>
int main()
{
int n=12;
int fib[12];
fib[0]=0;
fib[1]=1;
for(int k=2;k<n;k++)
{
fib[k]=fib[k-1]+fib[k-2];
}
printf("%d",fib[n-1]);

Please  tell  me  for  any  other  updates in  this  article

Tuesday, December 10, 2013

Q. Find the maximum sum subarray such that no 2 elements are contiguous

INPUT  {5,4,10,40,50,35}
OUTPUT  5+40+35=80

This  thing  is  used  when  basically  we  are  getting  a  recursive  function  and  DP  helps  us  in  reducing  complexity.

We  can  see  here  F(j)={arr[j]+F(j-2)}  as  we  cant  have  consecutive  elements  so  we  take  sum  till   j-2

where  F(i)=  max  subarray  formed  till   the  ith  index  of  array

#include<stdio.h>

int main()
{
 int n=6;
 int arr[6] = {5,4, 10, 40, 50, 35};
 //int arr[5]={3,2,7,10};
 int dp[5];
 dp[0]=arr[0];
 dp[1]=arr[0]>=arr[1]?arr[0]:arr[1];

 for(int k=2;k<n;k++)
 {
 int sum=arr[k]+dp[k-2];
 dp[k]= sum>dp[k-1]?sum:dp[k-1];
 }
 printf("%d",dp[n-1]);
}

I  know  I  may  be  bad  at  explaining  things  so  please  tell  me  if  you  are  unable  to  understand  I  will  try  to  make  content  mare  explainable..

Also  please  tell  if  my  code  is  failing  at  some  point 

Saturday, December 7, 2013

Q. Connect nodes of tree at same level with next pointer


Here  we  have  a  single  difference  in  structure  of  a  link  list  and  that  is  we  have  next  node  in  this  and  we  have  to  connect  the


Like  in  the  above  tree  we  need  to  connect

2->3
4->5->6
7->8

Here  is  the  working  code  below  for  this.  It  is  basically  a  level  order  traversal  only

#include<iostream.h>
#include<queue>
#include<vector>
using namespace std;
struct node
{
    int info;
    struct node* left;
    struct node* right;
    struct node* next;
};
node* newNode(int data)
{
    struct node* temp = new node;
    temp->info = data;
    temp->left = NULL;
    temp->right = NULL;
    temp->next=NULL;
  return (temp);
}
int breakCondition(vector<node*> temp)
{
for(int k=0;k<temp.size();k++)
{
node* temp1=temp.at(k);
if(temp1->left || temp1->right)
return 1;
}
return 0; }

int main()
{
struct node *n1 = newNode(1);
    n1->left        = newNode(2);
    n1->right       = newNode(3);
    n1->left->left  = newNode(4);
    n1->left->right = newNode(5);
    n1->right->left  = newNode(6);
    n1->left->right->left = newNode(7);
    n1->left->right->right = newNode(8);
 
    std::queue<node*> Q1;
    vector<node*> v;
    v.push_back(n1);
    Q1.push(n1);
 
 
while(1)
{
if(breakCondition(v)==0)
break;
v.clear();
    while(!Q1.empty())
    {
     node* temp=Q1.front();
   
     if(temp->left!=NULL)
     v.push_back(temp->left);
if(temp->right!=NULL)
     v.push_back(temp->right);
   
     Q1.pop();
   }
 
 
   for(int k=0;k<v.size();k++)
  {
node* temp2=v.at(k);
Q1.push(temp2);
cout<<temp2->info;   }
  cout<<endl;
    for(int k=0;k<v.size()-1;k++)
  {
node* temp=v.at(k);
node*temp1=v.at(k+1);
temp->next=temp1;    }
 
}
node*temp=n1->left->right->left;
temp=temp->next;
cout<<"Next  pointer  to  temp  is"<<"  "<<temp->info<<endl;
return 0;
}

Friday, November 1, 2013

Q. Reverse the group of k nodes in a link list

INPUT:  1->2>3->4->5->6->7->8     K=2

Then

OUTPUT:  1->0->3->2->5->4->7->6->8

METHOD  1

In  this  method we  are  maintaining  an  array  of  size  k  and  then  putting  first  value  at  index  k  and  second  at  k-1  and  so  on........

Then  we  are  putting  value  back  at  link  list..

Here  is  the  working  code  of  it  using  array.


Time  Complexity=  O(n)
Space  Complexity=O(k)



METHOD  2

 It  is  a  really  tricky  method  for  coding.  It  is  asked  to  code  many  times  in  interviews.  But  neglecting  this  question  is  not  a  good  option .One  must  practice  this  one  before.

Inplace  method  of  doing  this  question  and  link - link  swap  for  reversing..


Let  us  see  the  main  code  of  this.....

node* reverse(node* start,int k)
{

node *curr=start,*prev,*next,*end;
int i=0;
prev=end;
end=start;
if(curr==NULL)
return NULL;

while(i<k &&  curr!=NULL)

{
next=curr->next;
curr->next=prev;
prev=curr;
curr=next;
i++;
}
end->next=reverse(curr,k);
return prev;
}



Forming sum tree of a givn tree (nodes are to be replaced with the sum of values of their children)


INPUT:-

OUTPUT:-


Good  question  to  pratice  recursion  and  asked  in  many  interviews  to  check  the  concepts  of  recursion  and  trees.  



Here  is  a  C++  code  for  this

#include<iostream.h>
using namespace std;

struct node
{
    int info;
    struct node* left;
    struct node* right;
};

node* newNode(int data)
{
    struct node* temp = new node;
    temp->info = data;
    temp->left = NULL;
    temp->right = NULL;
  return (temp);
}

int sumTree(node* root)
{
if(root==NULL)
return 0;

int sum=root->info;
root->info=sumTree(root->left) + sumTree(root->right);
return root->info+sum;
}


void preorder(node* root)
{
if(root==NULL)
return;
cout<<root->info<<"        ";
preorder(root->left);
preorder(root->right);
}


int main()
{
struct node *n1 = newNode(1);
    n1->left        = newNode(2);
    n1->right       = newNode(3);
    n1->left->left  = newNode(4);
    n1->left->right = newNode(5);
    n1->right->left  = newNode(6);
    n1->left->right->left = newNode(7);
    n1->left->right->right = newNode(8);
    preorder(n1);
    cout<<endl;
    sumTree(n1);
    preorder(n1);
    
return 0;
}




Thursday, October 31, 2013

Diameter of a tree (longest leaf to leaf path)

I  do  not  know  what  was  GeeksforGeeks  algorithm  for  this  question.  I  did  not  get  a  bit  of  that.
But  this  is  my  code  which  has  passed  all  the  cases  from  interviewstreet.com.  You  can  yourself check   second  question  in   the  link  below

https://www.interviewstreet.com/recruit/test/start/sample


Here  is  my  code..  Hope  it  is  easily  understood

In  this  we  are  taking  that  both  the  leaves  have  common  source (which  may  be  root  or  any  other)
which  is  passed  into  the  function diameterOfTree()

Leaf  to  Leaf  path  from  that  node will  be  1+lh+rh

And  maximum  of  all  that  is  calculated

int maximum(int a,int b)
{
    if(a>=b)
        return a;
    else
        return b;
}  


int max=0;

int height(node* root)
{
    if(root!=NULL)
    {  
    int lh=height(root->left);
    int rh=height(root->right);
 
    return 1+maximum(lh,rh);
    }
    return 0;
}




int diameterOfTree(node * root)
{
    if(root!=NULL)
    {
    int temp=1+height(root->left)+height(root->right);
        if(temp>max)
            max=temp;
    diameterOfTree(root->left);
    diameterOfTree(root->right);
    }
    return max;
}