Tuesday, December 11, 2018

Compete Cell & Pattern : Training Coding Questions


Pattern - Star Pyramid
C program to print following pyramid pattern of stars n=5
            *
         * * *
      * * * * *
   * * * * * * *
* * * * * * * * *
Input Format
Input contains n
Constraints
1 <= n <= 20
Output Format
Print the pattern
Sample Input 0
5
Sample Output 0
            *
         * * *
      * * * * *
   * * * * * * *
* * * * * * * * *
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {

  int row, c, n, s;

  scanf("%d", &n);

  s = n;

  for (row = 1; row <= n; row++)  // Loop to print rows
  {
    for (c = 1; c < s; c++)  // Loop to print spaces in a row
      printf("  ");

    s--;

    for (c = 1; c <= 2*row - 1; c++) // Loop to print stars in a row
      printf("* ");

    printf("\n");
  }

 
    return 0;
}


Compete Cell
There is a colony of 8 cells arranged in a straight line where each day every cell competes with its adjacent cells(neighbour). Each day, for each cell, if its neighbours are both active or both inactive, the cell becomes inactive the next day, otherwise it becomes active the next day.
Assumptions: The two cells on the ends have single adjacent cell, so the other adjacent cell can be assumed to be always inactive. Even after updating the cell state. consider its previous state for updating the state of other cells. Update the cell information of all cells simultaneously. Write a function cellCompete which takes takes one 8 element array of integers cells representing the current state of 8 cells and one integer days representing the number of days to simulate. An integer value of 1 represents an active cell and value of 0 represents an inactive cell.
Input Format
Input will have 8 array values and the no of days
Constraints
array size is 8 integers
Output Format
print the array
Sample Input 0
1 0 0 0 0 1 0 0
1
Sample Output 0
0 1 0 0 1 0 1 0 



#include <stdio.h>
void cellCompete(int[],int);
int main() {
    int arr[10],i,n;
    for(i=0;i<8;i++)
        scanf("%d ",&arr[i]);
    scanf("%d",&n);
    cellCompete(arr,n);
    for(i=0;i<8;i++)
        printf("%d ",arr[i]);
    return 0;
}
void cellCompete(int arr[],int n){
    int arr1[10],i;
    for(;n>=1;n--)
    {
       arr[-1]=0;
       arr[8]=0;
        for(i=0;i<=7;i++)
            arr1[i]=arr[i-1]^arr[i+1];
        /*copying arr1 to arr */
        for(i=0;i<=7;i++)
            arr[i]=arr1[i];
    }
}


















Count the occurrence of substring
Find the occurrence of a sub string in a parent string
Input Format
Input contains the string and the sub string
Constraints
1<=substring_length <= string_length<=1000
Output Format
print the count
Sample Input 0
hgjghjhab
ab
Sample Output 0
1

#include <stdio.h>
#include <string.h>

int main() {
    int i,j,k,ctr=0;
    int l1,l2;
    char str[1000],fstr[1000];
    fgets(str,1000,stdin);
   
    fgets(fstr,1000,stdin);
    l1=strlen(str);
    l2=strlen(fstr);
   
    for(i=0;str[i]!='\0';i++)
    {
        for(j=0,k=i;fstr[j]!='\0' && str[k]!='\0' ;j++,k++)
        {
            if(str[k]!=fstr[j])break;
           
        }
        if(j==l2){
            ctr++;
     i=k-1;
        }
       
    }

printf("%d",ctr);
    return 0;
}



Eliminate Repeated Elements
Given two positive integer arrays ary1 and arr2 of lengths len1 and len2 respectively. write a program to count the number of elements which are not common In the arrays.
The input to the function distinctElementCount of two arrays arr1 and arr2 and their lengths len1 and len2 respectively.
The function return the number of elements which are not common in both arrays.
Example.
arr1 = {1, 2,3, 4, 5, 6, 7, 8, 9, 10}, lent = 10
arr2 = {11, 12, 13, 4, 5, 6, 7, 18, 19, 20}, len2 = 10
The distinct elements are 1, 2, 3, 8, 9, 10, 11, 12, 13, 18, 19 and 20 so the function should return 12.
Input Format
Input contains the length of the arrays and the values
Constraints
1 ≤ n ≤105
1 ≤ values ≤ 109
Output Format
Print the count
Sample Input 0
5 6
34 89 12 45 93
12 93 45 23 78 35
Sample Output 0
5

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int distinctElementCount(int arr1[],int arr2[],int len1,int len2)
{
    int ctr=0;
    int i,j;
   
    for(i=0;i<len1;i++)
    {
        for(j=0;j<len2;j++)
        {
            if(arr1[i]!=arr2[j])
            continue;
            else
                break;
        }
        if(j==len2)
            ctr++;
    }
   
    for(i=0;i<len2;i++)
    {
        for(j=0;j<len1;j++)
        {
            if(arr2[i]!=arr1[j])
            continue;
            else
                break;
        }
        if(j==len1)
            ctr++;
    }
   
    return ctr;
}
int main() {
int a1[105],a2[105];
    int i,n1,n2;
    int ctr;
    scanf("%d%d",&n1,&n2);
    if(n1>=1 && n1<=105)
    if(n2>=1 && n2<=105)
    {
    for(i=0;i<n1;i++) scanf("%d",&a1[i]);
    for(i=0;i<n2;i++)scanf("%d",&a2[i]);     ctr=distinctElementCount(a1,a2,n1,n2);
    printf("%d",ctr);
    }
   
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */   
    return 0;
}




GCD Of N Numbers
Write a 'C' program to find the GCD of N numbers
Input Format
Input will have the no of elements and the values
Constraints
1 ≤ noe ≤ 100000
1 ≤ values ≤ 100000000007
Output Format
print the GCD
Sample Input 0
5 2 4 8 16 32
Sample Output 0
2

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int gcd(int a, int b)
{
    if (a == 0)
        return b;
    return gcd(b % a, a);
}
 
// Function to find gcd of array of
// numbers
int findGCD(int arr[], int n)
{ int i;
    int result = arr[0];
    for (int i = 1; i < n; i++)
        result = gcd(arr[i], result);
 
    return result;
}
 

int main() {

   int a[100],n,i;
    scanf("%d",&n);
    for(i=0;i<n;i++)
        scanf("%d",&a[i]);

    int res=findGCD( a,n);

    printf("%d",res);
    return 0;
}

No comments:

Post a Comment