Friday, November 30, 2018

CodeMantra Interview coding Q1 & Q2


/* Find duplicate string count in n  number of strings */
import java.util.Scanner;
 class StringDuplicate{
 public StringDuplicate(){
   Scanner s=new Scanner(System.in);
   System.out.println("Enter number of Strings :");
   int n=s.nextInt();
   int flag,ctr;
   String[] str=new String[n];
    System.out.println("Enter "+n+" strings");
    for(int i=0;i<n;i++)
     str[i]=s.next();

   System.out.println("\n");
   for(int i=0;i<n;i++){
     ctr=1;
     flag=0;            
                /* check */
    for(int k=0;k<i;k++)
    {
                if(str[k].equals(str[i]))
                {
                    flag=1;
                break;
                }
   }          

  if(flag==0) {
    for(int j=i+1;j<n;j++){
      if(str[i].equals(str[j]))
                ctr++;
    }
    if(ctr>1)
     System.out.println(""+str[i]+" = "+ctr); 
   }
  }
 }
}

class eXecute{
 public static void main(String[] args){
   StringDuplicate obj=new StringDuplicate();
 }
}

/*  print true if array spli sum equals
ex:-  2+20 is equals to 4+5+13 */
public class MyClass {
    public static void main(String args[]) {
        int no[]={2,20,4,5,13};
        int lsum=0,rsum=0;
        int k;
        for(int i=0;i<no.length;i++)
        {
            lsum=rsum=0;
            for(k=0;k<=i;k++)
                lsum+=no[k];
                
            for(int j=i+1;j<no.length;j++)
            {
                rsum+=no[j];
            }
                System.out.println(lsum+"\t"+rsum+" pos ");
            
            if(lsum==rsum)
            {
                System.out.println("true "+ k);
            }
        }
        
        
    }
}


Sunday, November 25, 2018

1-D Array Interview questions in JAVA

--------------
ARRAYS :
------------
How to find the missing number in integer array of 1 to 100?

How to find duplicate number on Integer array in Java?
Solution :
public class MyClass {
    public static void main(String args[]) {
        int no[]={10,20,10,30,40,50,40,60,78,100};
        for(int i=0;i<no.length;i++)
        {
            for(int j=i+1;j<no.length;j++)
                if(no[i]==no[j])
                {
                    System.out.println(no[i]+" is duplicate");
                    break;
                }
         }
      }
}

How to find largest and smallest number in unsorted array?

How to find all pairs on integer array whose sum is equal to given number?

Write a program to remove duplicates from array in Java?

There is an array with every element repeated twice except one. Find that element? (solution)
This is an interesting array coding problem, just opposite of question related to finding duplicates in array. Here you need to find the unique number which is not repeated twice. For example if given array is {1, 1, 2, 2, 3, 4, 4, 5, 5} then your program should return 3. Also, don't forget to write couple of unit test for your solution.

How to find common elements in three sorted array? (solution)
Now we are coming on territory of tough array questions. Given three arrays sorted in non-decreasing order, print all common elements in these arrays.

Examples:
input1 = {1, 5, 10, 20, 40, 80}
input2 = {6, 7, 20, 80, 100}
input3 = {3, 4, 15, 20, 30, 70, 80, 120}
Output: 20, 80

How to remove duplicates from array in place?


How to reverse array in place in Java

How to check if array contains a duplicate

Thursday, November 22, 2018

assignment-1 for CSE RGAN & RPRA 2018

// add public void sort()  and public void sort(int) 


import java.util.*;
public class arraytest {
public int no[];
public int sz;
public Scanner s;
public arraytest()
{
s=new Scanner(System.in);
System.out.println("Array size?");
sz=s.nextInt();
this.sz=sz;
no=new int[sz];
}
public void populateArray()
{

for(int i=0;i<sz;i++)
{
no[i]=s.nextInt();
}

}

public void disArray()
{
for(int i=0;i<sz;i++)
{
System.out.println(no[i]);
}
}


public static void main(String[] args) {
arraytest a=new arraytest();
a.populateArray();
a.disArray();
System.out.println("Over...") ;
}

}

Monday, November 19, 2018

Wipro HackerRank Training Questions & Solutions


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