Thursday, 30 June 2016

LUCKY TIME PROBLEM (Given the time when she completed the code find a lucky time to execute it so that Alice need to wait as little as possible. Time is shown in 24 24 hour format as hh:mm:ss. Time is said to be lucky if all the 6 6 characters (except ':') are different.)

#include <bits/stdc++.h>

using namespace std;

bool vis [10 + 1];

bool rep(int num){

    if(num < 10){               // there is a 0 digit

        if(vis[0]) return true;
        vis[0] = true;
    }

    if(num == 0) return true;   // 2 zeros in the number

    while(num != 0){            // extract the number digit by digit

        int digit = num % 10;
        if(vis[digit]) return true;         // check if digits are repeated
        num /= 10;
        vis[digit] = true;
    }

    return false;
}



int main()
{
    int t, h , m , s;

    scanf("%d", &t);

    while(t--){

        scanf("%d:%d:%d", &h, &m, &s);

        bool found = false;
        bool st = false;

        for(int i = h; i < 24 && !found; i++){

            for(int j = 0; j < 60 && !found; j++){

                if(!st) j = m;

                for(int k = 0; k < 60 && !found; k++){

                    if(!st){

                        k = s;              // start from the given second
                        st = true;
                    }

                    memset(vis, false , sizeof vis);

                    if(!rep(i) && !rep(j) && !rep(k)){          // check if all cells are different

                        if(i < 10) printf("0");       // each number has 2 cells
                        printf("%d:", i);
                        if(j < 10) printf("0", j);
                        printf("%d:", j);
                        if(k < 10) printf("0", k);
                        printf("%d\n", k);
                        found = true;
                    }

                    if(i == 23 && j == 59 && k == 59){      // search in times before the given time

                        i = 0;
                        j = 0;
                        k = 0;
                    }
                }
            }
        }

    }

    return 0;
}

Wednesday, 22 June 2016

C PROGRAM QUIZ QUESTION

1)What is the output of this C code?
#include <stdio.h>
int main()
{
char *str = “hello, world”;
char *str1 = “hello, world”;
if (strcmp(str, str1))
printf(“equal”);
else
printf(“unequal”);
}
2)What does the following declaration mean?
int (*ptr)[10];
3)What will be output if you will compile and execute the following c code?
void main(){
int i=320;
char *ptr=(char *)&i;
printf(“%d”,*ptr);
}
4)Which  function sets first n characters of a string to a given character?
5)How will you print \n on the screen?
6)The library function used to find the last occurrence of a character in a string?
7)What is the output of this C code?
#include <stdio.h>
void main()
{        char *s = “hello”;

char*p = s;

printf("%p\t%p", p, s);

}
8)What is the output of this C code?
#include<stdio.h>
int main()
{
char str[]="c4learn";

printf("%d",*(str+strlen(str)));

return(0);
}
9)What is the output of this C code?
#include<stdio.h>
void main()
{
char arr[5*2/2] = {'a','b','c','d','e'};
printf("%c",arr[3]);
}

10)What is the output of this C code?
intmain()
{
    intarr[5];
    
    // Assume that base address of arr is 2000 and size of integer
        // is 32 bit
    arr++;
    printf("%u", arr);
    
    return0;
}
11)Output of following program?
# include <stdio.h>
voidfun(int*ptr)
{
    *ptr = 30;
}
intmain()
{
  inty = 20;
  fun(&y);
  printf("%d", y);
  return0;
}
12)What is the output of following program?
#include<stdio.h>
voidswap(char*str1, char*str2)
{
  char*temp = str1;
  str1 = str2;
  str2 = temp;
  
intmain()
{
  char*str1 = "Geeks";
  char*str2 = "Quiz";
  swap(str1, str2);
  printf("str1 is %s, str2 is %s", str1, str2);
  return0;
}
13)What is the return type of malloc() or calloc()?
14)Output of following program?
intmain()
{
  inta[] = {1, 2, 3, 4, 5, 6};
  int*ptr = (int*)(&a+1);
  printf("%d ", *(ptr-1) );
  return0;
}
15)Predict the output?
intmain()
{
 char*ptr = "GeeksQuiz";
 printf("%c\n", *&*&*ptr);
 return0;
}
16) syntax for the function strpbrk();?
17)syntax for the function memset();?
18)write the syntax of the function exit();?
19)what function is used to find the absolute value of an integer?write the syntax.
20)is there any standard library function to perform binary search,if it is write the syntax and write where it is?
                                                            ALL THE BEST


ZOHO INTERVIEW QUESTION Print the pattern N=4 4444444 4333334 4322234 4321234 4322234 4333334 4444444

#include<stdio.h>

#include<conio.h>



int main()

{

int i,j,k,n=4,l; //i for row, j for element no



printf("\t\t\tZOHO INTERVIEW QUESTION\n\n");



    for(i=1;i<=n;i++) // row count

    {                  

                       

     for(k=1,l=n;k<i && l>=1;k++,l--) // k prints the values from starting till the row no

      printf("%d",l);

   

      for(j=k;j<=(n+n-1)-i+1;j++) //j in every row inserts values starting from i-1 to 7-i+1

       printf("%d",l);

   

       for(k=j;k<=n+n-1;k++) // k prints the remaining values

        {

        if(l<=n)

        printf("%d",++l);

        else

        printf("%d",n);

        }

     printf("\n");

    }

 

    for(i=n-1;i>=1;i--)

    {

     for(k=1,l=n;k<i;k++,l--)

      printf("%d",l);

   

       for(j=k;j<=(n+n-1)-i+1;j++)

        printf("%d",l);

   

        for(k=j;k<=n+n-1;k++)

        {

        if(l<=n)

        printf("%d",++l);

        else

        printf("%d",n);

         }

     printf("\n");

    }

 

getch();

}



TRIANGLE PATTERN

#include<stdio.h>

#include<conio.h>



#define filled 1

#define empty 0



int left,diag,top,startx,starty;

int a[11][11],count[11][11],counter=0;

int N,i,j,num=1;



void init()

{

for(i=0;i<=10;i++)

 {

  for(j=0;j<=10;j++)

  {

   a[i][j]=0;

   count[i][j]=empty;

  }

 }

}



void move_diagnol()

{

 for(i=startx,j=starty;i<=diag && j<=diag ;i++,j++)

 {

  if(count[i][j]==filled)

  continue;

  a[i][j]=num;

  count[i][j]=filled;

  num++;

 }

}



void move_left()

{

 for(j=starty;j>=left;j--)

 {

  if(count[i][j]==filled)

  continue;

  a[i][j]=num;

  count[i][j]=filled;

  num++;

 }

}



void move_top()

{

 for(i=startx;i>=top;i--)

 {

  if(count[i][j]==filled)

  continue;

  a[i][j]=num;

  count[i][j]=filled;

  num++;

 }

}



void begin()

{

 while(counter<=3)

 {

 move_diagnol();

 --diag;



 starty=--j;

 startx=--i;

 

 move_left();

 left++;



 starty=++j;



 move_top();

 top++;

 ++i;

 

 startx=++i+1;

 starty=++j;



 counter++;

 }

}



void display()

{

int i,j;

char c=" ";

    for(i=1;i<=10;i++)

    {

     for(j=1;j<=10;j++)

      {

      if(a[i][j])

      printf("%4d",a[i][j]);

      else

      printf("%4c",c);

      }

     printf("\n");

    }

}







int main()

{



init();



printf("\nEnter the value of N: ");

scanf("%d",&N);



left=1;

top=1;

diag=N;



 startx=top;

 starty=top;

printf("\n\n");

begin();

display();



getch();

return 0;

}

SPIRAL MATRIX

#include<stdio.h>

#include<conio.h>

#define left 1

#define down 2

#define right 3

#define up 4



int matrix[5][5]={{1,2,3,4,5},{6,7,8,9,10},{11,12,13,14,15},{16,17,18,19,20,21},{22,23,24,25,26}};

int i,j,direction=1,order=5,final_left,final_right,final_up,final_down;



void spiral()

{

while(i!=final_up-1 || j!=final_left-1)

 {

 switch(direction)

  {

 case left:

        while(j>=final_left-1)

         printf("%d ",matrix[i][j--]);

         j++;

         if(i==final_up-1 && j==final_left-1)

         break;

         i++;

         direction=down;

        break;

       

 case down:

        while(i<=final_down+1)

         printf("%d ",matrix[i++][j]);

         i--;

         j++;

         direction=right;

        break;

       

 case right:

        while(j<=final_right+1)

         printf("%d ",matrix[i][j++]);

         j--;

         i--;

         direction=up;

        break;

                 

 case up:

         while(i>=final_up-1)

         printf("%d ",matrix[i--][j]);

         i++;

         j--;

         direction=left;

         break;

  }



 }

}



int main()

{

for(i=0;i<order;i++)

{

                for(j=0;j<order;j++)

                 printf("%4d",matrix[i][j]);

 printf("\n");

}



printf("\n\n");

i=order/2;j=order/2;



final_left=order/2;

final_right=order/2;

final_up=order/2;

final_down=order/2;



while(final_left>0 && final_up>0)

{

spiral();

 final_left--;

 final_up--;

 final_right++;

 final_down++;



 i=final_up;

 j=final_left-1;

}



getch();

return 0;

}

PERMUTATION OF A STRING,REMOVE THE DUPLICATES FROM THE STRING,PALINDROME OF A STRING

#include<stdio.h>

#include<string.h>

#include<conio.h>

#include<stdlib.h>



#define true 1

#define false 0



int used[15]={false};

char string1[50][15],string2[15];

char output[15];

int ptr=0,strcount=0;



void permute()

{

 if(strlen(output)==strlen(string2))

 {

  strcpy(string1[strcount],output);

  strcount++;

  return;

 }



 int i;



 for(i=0;i<strlen(string2);i++)

 {

  if(used[i])

  continue;

 

  output[ptr++]=string2[i];

  output[ptr]='\0';

  used[i]=true;

  permute();

  used[i]=false;

  ptr--;

 }

}



int check(int i)

{

 int j,k;

 for(j=0,k=strlen(string1[i])-1 ; j< strlen(string1[i])/2 ; j++,k--)

 {

  if(string1[i][j]!=string1[i][k])

  return -1;

 }

return 1;

}



void remove_duplicate()

{

 int i,j,k;

 for(i=0;i<strcount;i++)

  {

   for(j=i+1;j<strcount-1;j++)

    {

     if(strcmp(string1[i],string1[j])==0)

      {

       for(k=j;k<strcount-1;k++)

        {

         strcpy(string1[k],string1[k+1]);

        }

        strcount--;

      }

    }

  }

}



void palindrome()

{

 int i,pal;

 for(i=0;i<strcount;i++)

 {

  pal=check(i);

  if(pal==1)

  printf("\t \"%s\" can be converted to \"%s\" which is a palindrome\n\n",string2,string1[i]);

 }

}



int main()

{

printf("Enter String: ");

scanf("%s",string2);



printf("\n");

permute();

remove_duplicate();

palindrome();



getch();

return 0;

}

FIND THE Kth LARGEST ELEMENT

#include<stdio.h>

#include<conio.h>



int a[15]={1,4,5,3,12,45,63,32,134},n=9,k,temp[15],num;



void kth_largest()

{

int i,j;



 for(i=0;i<n;i++)

 temp[i]=a[i];



 for(i=0;i<k;i++)

 {

  for(j=0;j<n-1;j++)

   {

    if(temp[j]>temp[j+1])

    temp[j]=temp[j]+temp[j+1]-(temp[j+1]=temp[j]);

   }

 }



 printf("\n%d^th largest element= %d\n\n",k,temp[n-k]);



}



void kth_smallest()

{

 int i,j;



 for(i=0;i<n;i++)

 temp[i]=a[i];



  for(i=0;i<k;i++)

 {

  for(j=0;j<n-1;j++)

   {

    if(temp[j+1]>temp[j])

    temp[j]=temp[j]+temp[j+1]-(temp[j+1]=temp[j]);

   }

 }





 printf("\n%d^th smallest element= %d\n\n",k,temp[n-k]);



}



int main()

{

printf("\nEnter the value of k: ");

scanf("%d",&k);



int i;

for(i=0;i<n;i++)

printf("%d ",a[i]);

printf("\n\n");



kth_largest();

kth_smallest();



getch();

return 0;

}