Showing posts with label C. Show all posts
Showing posts with label C. Show all posts

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.

Wednesday 4 July 2012

Consider you have a grid of size m x n. There are stones placed randomly in some of the squares of this grid. Design a way to find out minimum rectangular area which covers all the stones in this grid.


Smallest Rectangle

Input :(place 1 as rock other wise 0) 5 4 0 0 0 0 0 1 0 0 0 0 0 1 1 0 0 0 0 0 0 0
Output: (1,0), (1,3), (3,0), (3,3) */
#include<stdio.h> #define MAX(a,b) (a>b?a:b) #define MIN(a,b) (a<b?a:b) int main() { int m,n,A[10][10],i,j,i1=10,i2=0,j1=10,j2=0; printf("enter m & n (m*n) :"); scanf("%d %d",&m,&n); printf("entet array (place 1 as rock other wise 0) :\n"); for(i=0;i<m;i++) for(j=0;j<n;j++) scanf("%d",&A[i][j]); for(i=0;i<m;i++) for(j=0;j<n;j++) if(A[i][j]==1) { i1=MIN(i1,i); i2=MAX(i2,i); j1=MIN(j1,j); j2=MAX(j2,j); } printf("\n(%d,%d), (%d,%d), (%d,%d), (%d,%d)\n",i1,j1,i1,j2,i2,j1,i2,j2); return 0; }

The character 'a' to 'z' are encoded as 1 - 26. Given a string of digits, compute the number of valid decodings of the string. For example, both 'aa' and 'k' can be encoded as '11'. Hence num_valid_encodings('11') = 2.

recursive

#include <stdio.h> #include <string.h> int count_decode(char S[10],int n) { int x1=S[n-1]-'0',x2=S[n-2]-'0',z; if(!n) return 0; else if(n==1) { return 1; } else { z=x2*10+x1; // printf("z=%d ",z); if(z>26) return (count_decode(S,n-1)); else if(n==2) return (count_decode(S,n-1) + count_decode(S,n-2)+1); else return (count_decode(S,n-1) + count_decode(S,n-2)); } } int main() { int i,j,n; char S[10]; gets(S); n=strlen(S); printf("< %d >",count_decode(S,n)); return 0; }

atoi is a function in the C programming language that converts a string into an integer numerical representation. atoi stands for ASCII to integer. It is included in the C standard library header file stdlib.h. Its prototype is as follows:

atoi : ASCII to integer

//INPUT : 24 83 0 2
//OUTPUT : 24 83 0 2
#include<stdio.h>
#include<string.h> int atoi(int K[10],char string[30]) { 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; }
int main() { char string[30]; int i,a,A[10]; printf("\nEnter String : "); gets(string); a=atoi(A,string); printf("\nIntegers are : "); for(i=0;i<a;i++) printf("%d ",A[i]); printf("\n"); return 0; }

String Masking : Given a string"S" and we have to remove a an occurance of a small string "M" from there.


Input:
KUMAR
UM

Output:
KAR



#include<stdio.h>
#include<string.h>
int str_com(char K[20],int i,int m,char M[10])
{
    int k=0;
    for(k=0;k<m;k++)
        if(K[i+k]!=M[k])
            break;
    if(k==m)
        return 1;
    return 0;
}

int main()
{
    char S[20],M[10];
    int i,j=0,l,m,k,flage;
    gets(S);
    gets(M);
    l=strlen(S);
    m=strlen(M);
    for(i=0;i<l;i++)
    {
        if(S[i]!=M[0])
            S[j++]=S[i];
        else
            if( str_com(S,i,m,M) )
                i+=m-1;
            else S[j++]=S[i];
    }
    S[j]=NULL;
    printf("< %s >",S);
return 0;
}