Friday 6 July 2012

Kahan We Met



Geet is travelling to meet her boyfriend in Mumbai.
Aditya is travelling to Mumbai for his business meeting.
Geet starts from Delhi while Aditya starts from Bangalore. Both of them are travelling on bus passing from town to town.
They meet at a nondescript city and end up travelling together to Mumbai. By the end of the journey they fall in love and settle down.
Enough of Bollywood stories, The problem at hand is to find Kahan they met?

For all interested programmers, Represent this as a programming problem(Data Structure) and find the location where Geet and Aditya would have met.

P.S. No need to actually find any city on the map where they met.
*/
//   ===========================================================================
//      Input Formate :
//
//            12 13 14 21 22            (these are 5 city pincode(2 disit integer pincoede for each city they pass through))
//
//            31 32 33 42 20 21 22      (these are 7 city pincode )
//
//     Output : 21                      (pincode of the city where they will met)
//   ===================================================================


#include<stdio.h>


int kahan_they_met(int A[20],int G[20],int g,int a)
{
    int i_g=0,i_a=0;
    if(g>a)
          i_g+=g-a;
    else  i_a+=a-g;


    while(i_g <= g)
    {
        if(A[i_a]==G[i_g])
            return A[i_a];
        i_g++;
        i_a++;
    }
return 0;
}


int convert_input(int K[],char string[200])
{
    int j=0,i=0,temp=0;
    while(string[i]!='\0')
    {
        temp=0;
        while(string[i]!=' ' && string[i]!='\0')
            temp=temp*10 + (string[i++]-'0') ;
        if(string[i]==' ')
            i++;
        K[j++]=temp;
    }


return j-1;
}




int main()
{
    char string[200];
    int g,a,i,G[20],A[20],met;
    printf("\nEnter city beetween D and M for GEET (eg: 12 13 14 21 22 ) : ");
    gets(string);
    g=convert_input(G,string);


    printf("\n-----------------\nEnter city beetween B and M for  for Aditya visit : ");
    gets(string);
    a=convert_input(A,string);


//    for(i=0;i<=g;i++)
//        printf("\n%d>>%d",i,G[i]);


   met=kahan_they_met(A,G,g,a);


   if(met)
        printf("\n\nGeet and Aditya will met at : %d\n",met);
    else
        printf("\n\nGeet and Aditya will met at Mumbai\n");


return 0;


}

1 comment:

  1. Thanks for ur post....Could you please explain this..Bcz i am new to Data structure

    ReplyDelete