Showing posts with label Array. Show all posts
Showing posts with label Array. Show all posts

Tuesday, 2 October 2012

Re-arrange the odd/even to odd/even places





#include<stdio.h>

void Swap (int A[], int i, int j)
{
    int temp = A[i];
    A[i]=A[j];
    A[j]=temp;
}

int main()
{
    int A[] = {3,1,4,5,7,6,10,8};
    int i=0,j=1,n;
    n=sizeof(A)/sizeof(int);
    while(i<n)
    {
        while(i<n && A[i]%2 == 0)
            i+=2;
        while(j<n && A[j]%2 == 1)
            j+=2;
        if(i<n && j<n)
            Swap(A,i,j);
    }// end of while

    for(i=0;i<n;i++)
        printf(" %d",A[i]);

return 0;
}

Let me know your thoughts

Friday, 14 September 2012

Sort an Array of 0 1& 2 in one pass






#include<stdio.h>

int main()
{
    int A[] = {2,2,2,1,1,1,0,0,0,1,2,0};
    int j,i,x,y,n,t=A[0];
    n=sizeof(A)/sizeof(A[0]);
printf("Size of array : %d\n",n);
    for(x=i=0,y=j=n-1;i<=j;)
    {
        if(t==0)
        {
            A[x]=0;
            x++;
            i++;
            t=A[i];
        }
        else if(t==2)
        {
            t=A[y];
            A[y]=2;
            y--;
            j--;
        }
        else
        {
            i++;
            t=A[i];
        }
    }

    while(x<=y)
    {
        A[x++]=1;
    }

    for(i=0;i<n;i++)
    {
        printf(" %d",A[i]);
    }
return 0;
}

Let me know your thoughts.

Saturday, 7 July 2012

To copy the contents of an array A to contents of Array B without using loops and any standard string copy functions.


#include<stdio.h>
int Copy_Array(int A[],int B[], int n,int i)
{
    if(i==n)
        return 0;
    B[i]=A[i];
    Copy_Array(A,B,n,i+1);
return 0;
}

int main()
{
    int A[10],B[10],i,n;
    printf("Enter size : ");
    scanf("%d",&n);
    printf("Enter Array : ");
    for(i=0;i<n;i++)
        scanf("%d",&A[i]);
    printf("\nArray A : ");
    for(i=0;i<n;i++)
        printf("%d ",A[i]);
    Copy_Array(A,B,n,0);
    printf("\nArray B : ");
    for(i=0;i<n;i++)
        printf("%d ",B[i]);
return 0;
}


Let me know your thoughts.

Friday, 6 July 2012

Find duplicates in O(n) time and O(1) extra space


Given an array of n elements which contains elements from 0 to n-1, with any of these numbers appearing any number of times.Find these repeating numbers in O(n) and using only constant memory space.

For example, let n be 7 and array be {1, 2, 3, 1, 3, 0, 6}, the answer should be 1 & 3.



#include<stdio.h>
#include<math.h>
int main()
{
    int n,A[15],i,flage,cycle=0,j,temp;
    printf("\nEnter size of array : ");
    scanf("%d",&n);
    printf("\nEnter elements of array( %d space saperated  integer) : \n",n );
    for(i=0;i<n;i++)
      scanf("%d",&A[i]);
    flage=0;j=0;
    printf("\n OUTPUT : ");

    for(i=0;i<n;i++)
    {
        if( A[abs(A[i])] >= 0)
           A[abs(A[i])]=-A[abs(A[i])];
        else
            printf(" %d",abs(A[i]) );
    }
return 0;
}

Let me know your thoughts.

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;


}

Given an image represented by an NxN matrix, where each pixel in the image is 4 bytes, write a method to rotate the image by 90 degrees. Can you do this in place?




INPUT  >   OUTPUT        INPUT    >  >    OUTPUT  
4                                        4                                         
1 2 3 4        1 1 1 1           11 12 13 14      41 31 21 11
1 2 3 4        2 2 2 2           21 22 23 24      42 32 22 12
1 2 3 4        3 3 3 3           31 32 33 34      43 33 23 13
1 2 3 4        4 4 4 4           41 42 43 44      44 34 24 14



#include<stdio.h>
void rotate(int I[10][10],int O[10][10],int j,int i)
{
    int k,l,temp;
    for(k=0;k<j;k++)
    {
        temp=I[k+i][i];              // copy of Left in temp
        O[k+i][i]=I[i+j-1][i+k];     // copy bottom to left
        O[i+j-1][i+k]=I[i+j-k-1][i+j-1];   // copy right to bottom
        O[i+j-k-1][i+j-1]=I[i][i+j-k-1];   // copy top to right
        O[i][i+j-k-1]=temp;            // copy temp(left) to top
    }
}// end of rotate

int main()
{
    int I[10][10],O[10][10],i,j,N;
    scanf("%d",&N);
    for(i=0;i<N;i++)
    {
        for(j=0;j<N;j++)
            scanf("%d",&I[i][j]);
    }
    i=0;
    j=N;
    while(j>0)
    {
        rotate(I,O,j,i);  // i is base,  j is size
        j=j-2;
        i++;
    }
    printf("\n\n");
    for(i=0;i<N;i++)
    {
        printf("\n");
        for(j=0;j<N;j++)
            printf("%d ",O[i][j]);
    }
return 0;
}

Reduce aabcccccaaa to a2b1c5a3 for new string is of smaller size otherwise live it original




#include<stdio.h>
#include<string.h>
int main()
{
     char s[20],o[20],temp;
     int i=0,j=0,c=0;
     gets(s);
     while(s[i]!=NULL)
     {
         temp=s[i];
         while(s[i]== temp)
         {
             i++;
             c++;
         }
         o[j++]=temp;
         o[j++]=c+48;// ASCII of 0 is 48
         c=0;
     }// end of while
     if(strlen(o)<strlen(s))
        puts(o);
    else
        puts(s);
return 0;
}

Write a method to decide if two strings are premutation of each other or not.


#include<stdio.h>
#include<string.h>
int check_permutation(char s1[], char s2[])
{
    int c[27],i;
    for(i=0;i<27;i++)
        c[i]=0;
    if(strlen(s1)!=strlen(s2))
        return 0;
    for(i=0;s1[i]!=NULL;i++)
    {
        c[0]= s1[i];
        if(c[0]>96)
             c[0]=c[0]-96;
        else c[0]=c[0]-64;
        c[c[0]]++;

        c[0]= s2[i];
        if(c[0]>96)
             c[0]=c[0]-96;
        else c[0]=c[0]-64;
        c[c[0]]--;
    }
    for(i=1;i<27;i++)
    {
        if(c[i]!=0)
            return 0;
    }
    return 1;
}

int main()
{
    char s1[20],s2[20];
    scanf("%s%s",&s1,&s2);
    if( check_permutation(s1,s2) )
       printf("\n Permutation of each other");
    else
        printf("\n NOT a Permutation ");
return 0;
}

Implement an algorithm to determine if a string has all unique characters.


#include<stdio.h>
#include<string.h>
int main()
{
    char input[27];
    int i,l,check[28];
    scanf("%s",&input);
    l=strlen(input);
    for(i=0;i<27;i++)
        check[i]=0;
    for(i=0;i<l;i++)
    {
        check[27]=input[i];
        if(check[27]>96)
            check[27]=check[27]-96;
        else
            check[27]=check[27]-64;
        if(!check[check[27]])
            check[check[27]]=1;
        else
        {
            printf("\nNOT");
            return 0;
        }
    }
    printf("\n unique");
    return 0;
}

Wednesday, 4 July 2012

sort an array a[n] containing 0s and 1s with time complexity n and you cant use more than 1 data structure



// TIME: O(n) || Space : O(1)
#include<stdio.h> int main() { int A[20],i,j,n,temp; printf("Enter n :"); scanf("%d",&n); for(i=0;i<n;i++) scanf("%d",&A[i]); printf("\n--"); i=0; j=n-1; while(i<j) { while(A[i]==0) i++; while(A[j]==1) j--; if(i<j) { temp=A[i]; A[i]=A[j]; A[j]=temp; i++; j--; } }// end of while printf("\n"); for(i=0;i<n;i++) printf("%d ",A[i]); printf("\n"); return 0; }