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;
}

Friday, December 7, 2018

Leader No Problem (using 1D)

Write a Java program to print last LEADERS in the array. Go to the editor Note: An element is leader if it is greater than all the elements to its right side.
Input Format
5 1 4 3 9 4

Constraints
read n value and n number of integers

Output Format
9

Sample Input 0
5 1 2 3 9 5
Sample Output 0
9

Sample Input 1
10 12 3 1 19 1 1 56 4 3 2
Sample Output 1
56

import java.util.Scanner;
public class MyClass {
    public static void main(String args[]) {
        int i,n;
        int lno;
        int no[];
        Scanner s=new Scanner(System.in);
        n=s.nextInt();
        no=new int[n];
        for(i=0;i<n;i++)
        no[i]=s.nextInt();
int psum=0,j;
lno=no[0];
for(i=1;i<no.length;i++)
{
psum=0;
    for(j=0;j<i;j++)
    {
        psum+=no[j];
    }
    if(no[i]>=psum)
    lno=no[j];
}
System.out.println(lno);
    }
}

Wednesday, December 5, 2018

Array Coding Questions

Arrays exercises
1.  Write a Java program to sort a numeric array.
2. Write a Java program to sum values of an array
3. Write a Java program to calculate the average value of array elements.
4. Write a Java program to test if an array contains a specific value
5. Write a Java program to find the index of an array element
6. Java Program to Find the Largest Two Numbers in a Given Array
7. Java Program to Find the Second Largest &amp; Smallest Elements in an Array
8. Java Program to Find the Largest Number in an Array
9. Write a Java program to copy an array by iterating the array.
10. Write a Java program to remove a specific element from an array.
11.  Write a Java program to reverse an array of integer values.
12. Java Program to Put Even &amp; Odd Elements of an Array in 2 Separate Arrays
13. Java Program to Split an Array from Specified Position
14. Java program to check sparse matrix.
15. Java program to find the common elements in two integer arrays.
16. Java program to move all zero at the end of the array.
17. Java program to merge two one dimensional arrays.
18. Java program to create a matrix and fill it with prime numbers.
19. Java program to check whether a given matrix is Lower Triangular
Matrix or not.
20.  Write a Java program to find the duplicate values of an array of integer
values
21. Java program to remove duplicate elements from an array.
22. Java Program to Find 2 Elements in the Array such that Difference between them is
Largest
23. Java Program to Segregate 0s on Left Side &amp; 1s on Right Side of the Array
24. Java Program to Find the Number of Non-Repeated Elements in an Array
25.   Write a Java program to find all pairs of elements in an array whose
sum is equal to a specified number.
26.  Write a Java program to test the equality of two arrays
27. Write a Java program to find the number of even and odd integers in a
given array of integers.

28.  Write a Java program to find common elements from three sorted (in
non-decreasing order) arrays.
29.  Write a Java program to compute the average value of an array of
integers except the largest and smallest values
30. Write a Java program to check if an array of integers without 0 and -1.
31. Write a Java program to remove the duplicate elements of a given array
and return the new length of the array.
32. Write a Java program to print all the LEADERS in the array.   Go to the
editor
Note: An element is leader if it is greater than all the elements to its right
side.
33. Write a Java program to find the two elements from a given array of
positive and negative numbers such that their sum is closest to zero.
34. Write a Java program to find all combination of four elements of an given
array whose sum is equal to a given value
35. Write a Java program to count the number of possible triangles from an
given unsorted array of positive integers. 
36. Write a Java program to cyclically rotate a given array clockwise by one
37.  Write a Java program to check whether there is a pair with a specified
sum of a given sorted and rotated array.
38. Write a Java program to arrange the elements of an given array of
integers where all negative integers appear before all the positive
integers.
39. Write a Java program to arrange the elements of an given array of
integers where all positive integers appear before all the negative
integers. 
40. Write a Java program to sort an array of positive integers of an given
array, in the sorted array the value of the first element should be
maximum, second value should be minimum value, third should be
second maximum, fourth second be second minimum and so on.
41. Write a Java program to separate even and odd numbers of an given
array of integers. Put all even numbers first, and then odd numbers.
42.  Write a Java program to replace every element with the next greatest
element (from right side) in an given array of integers.
43. Java Program to Count the Number of Occurrence of an Element in an Array

44. Write a C program to count frequency of each element in an array.

Robo dialy activities Problem

Robo dialy activities first eat ,then computer and then sleep (ecs)
The day starts with eat he can do eat/computer/sleep
if the day starts with  computer he can do computer/sleep
The day starts with sleep he can do onlysleep
I/p
Ex:    ecs                             o/p   Success
Ex:    ces                             o/p Fail
Ex:     eeccsss                     o/p success


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


char pat[]="ecs";
int prev=0,curr=0;
int check(char ch1,char ch2)
{
int i;
for(i=0;pat[i]!=0;i++)
if(pat[i]==ch1)
curr=i;

for(i=0;pat[i]!=0;i++)
if(pat[i]==ch2)
prev=i;

if(curr<prev)
return 0;
else
return 1;
}

void main()
{
 /* ces  cceess */

 char str[]="ssss";
 int i=1;

  clrscr();


 while(str[i]!=0)
 {
 if(check(str[i],str[i-1]))
 {
 i++;
 }
 else
 break;
 }


 if(i==strlen(str))
printf("\n....Success",i);
else
printf("\nFail",i);


}