Friday, July 17, 2020

You are given a two-dimensional 3*3 array starting from A [0][0]. You should add the alternate elements of the array and print its sum. It should print two different numbers the first being sum of A 0 0, A 0 2, A 1 1, A 2 0, A 2 2 and A 0 1, A 1 0, A 1 2, A 2 1.

Question:

You are given a two-dimensional 3*3 array starting from A [0][0]. You should add the alternate elements of the array and print its sum. It should print two different numbers the first being sum of A 0 0, A 0 2, A 1 1, A 2 0, A 2 2 and A 0 1, A 1 0, A 1 2, A 2 1. 
 
Input Format 
 
First and only line contains the value of array separated by single space. 
 
 
Output Format 
 
First line should print sum of A 0 0, A 0 2, A 1 1, A 2 0, A 2 2 Second line should print sum of A 0 1, A 1 0, A 1 2, A 2 1 
 
SAMPLE INPUT  
 
1 2 3 4 5 6 7 8 9 
 
SAMPLE OUTPUT  
 
25 20 


Answer:

#include <stdio.h>

int main()
{
    int a[3][3], odd_sum=0,even_sum=0;
    for(int i=0;i<3;i++)
    {
        for(int j=0;j<3;j++)
        {
            scanf("%d",&a[i][j]);
        }
    }
    
    for(int i=0;i<3;i++)
    {
        for(int j=0;j<3;j++)
        {
            if((i+j)%2==0)
               even_sum=even_sum+a[i][j];
            else
                odd_sum=odd_sum+a[i][j];
        }
    }
    
    printf("%d\n",even_sum);
    printf("%d\n",odd_sum);

    return 0;
}


OUTPUT:

1 2 3 4 5 6 7 8 9                                                                                          
25                                                                                                         
20  

Wednesday, July 15, 2020

Download Java Interview Questions

To crack the interview, study following link.

Link for Download Java Questions

Thanks and All the best.

You are given an array of n integer numbers a1, a2, . . . , an. Calculate the number of pair of indices (i, j) such that 1 ≤ i < j ≤ n and ai xor aj = 0.

Question:

You are given an array of n integer numbers a1, a2, . . . , an. Calculate the number of pair of indices (i, j) such that 1 ≤ i < j ≤ n and ai xor aj = 0. 
 
Input format 
 
- First line: n denoting the number of array elements
 - Second line: n space-separated integers a1, a2, . . . , an. 
 
Output format 
 
Output the required number of pairs. 
 
Constraints 
 
1 ≤ n ≤ 106 1 ≤ ai ≤ 109 
 
SAMPLE INPUT  
 
1 3 1 4 3 
 
SAMPLE OUTPUT  
 
 
Explanation 
 
The 2 pair of indices are (1, 3) and (2,5). 

ANSWER:

#include <stdio.h>

int main()
{
    int n, arr[1000000], i, j, count; 
    scanf("%d", &n); 
    for (i = 0; i < n; i++) 
    {
     scanf("%d", &arr[i]);
    }
    count = 0; 
    for (i = 0; i < n - 1; i++) 
    { 
      for (j = i + 1; j < n; j++) 
      { 
       if (arr[i] == arr[j]) 
        {
          count++; 
        }
      } 
     } 
 printf("%d", count); 

    return 0;
}


OUTPUT:

5                                                                                                          
1 3 1 4 3                                                                                                  
2  

A new deadly virus has infected large population of a planet. A brilliant scientist has discovered a new strain of virus which can cure this disease. Vaccine produced from this virus has various strength depending on midichlorians count. A person is cured only if midichlorians count in vaccine batch is more than midichlorians count of person. A doctor receives a new set of report which contains midichlorians count of each infected patient, Practo stores all vaccine doctor has and their midichlorians count. You need to determine if doctor can save all patients with the vaccines he has. The number of vaccines and patients are equal.

Question:

A new deadly virus has infected large population of a planet. A brilliant scientist has discovered a new strain of virus which can cure this disease. Vaccine produced from this virus has various strength depending on midichlorians count. A person is cured only if midichlorians count in vaccine batch is more than midichlorians count of person. A doctor receives a new set of report which contains midichlorians count of each infected patient, Practo stores all vaccine doctor has and their midichlorians count. You need to determine if doctor can save all patients with the vaccines he has. The number of vaccines and patients are equal. 
 
Input Format 
 
First line contains the number of vaccines - N. Second line contains N integers, which are strength of vaccines. Third line contains N integers, which are midichlorians count of patients. 
 
Output Format 
 
Print a single line containing 'Yes' or 'No'. 
 
Input Constraint 
 
1 < N < 10 
 
Strength of vaccines and midichlorians count of patients fit in integer. 
 
SAMPLE INPUT  
 
123 146 454 542 456 
100 328 248 689 200 
 
SAMPLE OUTPUT  
 
No 
 
ANSWER:

#include <stdio.h>

int main()
{
    int n; 
    scanf("%d",&n); 
    int vaccines[n]; 
    for(int i=0;i<n;i++)
   { 
        scanf("%d",&vaccines[i]); 
    } 
    int patients[n]; 
    for(int j=0;j<n;j++)
   { 
        scanf("%d",&patients[j]); 
    } 
    int flag=1; 
    for(int k=0;k<n;k++)
    { 
        if(vaccines[k]<patients[k]) 
            flag=0; 
    } 
    
   if(flag)
       printf("Yes"); 
    else 
       printf("No"); 

    return 0;
}


OUTPUT:

5                                                                                                          
123 146 454 542 456                                                                                        
100 328 248 689 200                                                                                        
No   

You are given an array A of non-negative integers of size m. Your task is to sort the array in non-decreasing order and print out the original indices of the new sorted array.

Question:

You are given an array A of non-negative integers of size m. Your task is to sort the array in non-decreasing order and print out the original indices of the new sorted array. 
 
Example: 
 
A={4,5,3,7,1} 
 
After sorting the new array becomes A={1,3,4,5,7}. 
 
The required output should be "4 2 0 1 3"    
 
INPUT : 
 
The first line of input consists of the size of the array The next line consists of the array of size m 
 
OUTPUT : 
 
Output consists of a single line of integers 
 
CONSTRAINTS: 
 
1<=m<=106 0<=A[i]<=106 
 
NOTE: The indexing of the array starts with 0. 
 
SAMPLE INPUT  
 
5 4 5 3 7 1 
 
SAMPLE OUTPUT  
 
4 2 0 1 3 
 
ANSWER:

#include <stdio.h>

int main()
{
    int n; 
        scanf("%d",&n); 
        int a[n],b[n],i,j,temp,flag=0; 
        for(i=0;i<n;i++)
       { 
            scanf("%d",&a[i]); 
            b[i]=a[i]; 
        } 
        for(i=0;i<n-1;i++)
       { 
            flag=0; 
            for(j=0;j<n-i-1;j++)
            { 
                if(b[j]>b[j+1])
                { 
                    temp=b[j]; 
                    b[j]=b[j+1]; 
                    b[j+1]=temp; 
                    flag=1; 
                } 
            } 
            if(flag==0)
           { 
                break; 
            } 
        } 

        for(i=0;i<n;i++)
       { 
            for(j=0;j<n;j++)
           { 
                if(b[i]==a[j] && a[j]!=-1)
                { 
                    printf("%d ",j); 
                    a[j]=-1; 
                    break; 
                } 
            } 
        } 

    return 0;
}


OUTPUT:

5                                                                                                          
4 5 3 7 1                                                                                                  
4 2 0 1 3  
Question:

Coders here is a simple task for you, you have given an array of size N and an integer M. 
 
Your task is to calculate the difference between maximum sum and minimum sum of N-M elements of the given array. 
 
Constraints: 
 
1<=t<=10 1<=n<=1000 1<=a[i]<=1000 
 
Input: 
 
First-line contains an integer T denoting the number of test cases. The first line of every test case contains two integer N and M. Next line contains N space-separated integers denoting the elements of an array 
 
Output: 
 
For every test case print your answer in a new line 
 
SAMPLE INPUT  
 
1
5 1
1 2 3 4 5 
 
SAMPLE OUTPUT  
 
4 
 
Explanation 
 
M is 1 and N is 5 so you have to calculate maximum and minimum sum using (5-1 =) 4 elements. The maximum sum using the 4 elements would be (2+3+4+5=)14. The minimum sum using the 4 elements would be (1+2+3+4=)10. The difference will be 14-10=4. 
 
 Answer:

#include <stdio.h>

int main()
{
   int t; 
    scanf("%d",&t); 
    while(t--)
    { 
        int n,m; 
        scanf("%d %d",&n,&m); 
        int arr[n]; 
        for(int i=0;i<n;i++)
       { 
            scanf("%d",&arr[i]); 
        } 

        for(int k=0;k<n;k++)
       { 
            int smallest=k; 
            for(int l=k+1;l<n;l++)
           { 
                if(arr[smallest]>arr[l]){ 
                    smallest=l; 
             } 
        } 
            int temp=arr[k]; 
            arr[k]=arr[smallest]; 
            arr[smallest]=temp; 
      } 

        long max=0; 
        for(int j=m;j<n;j++)
       { 
            max+=arr[j]; 
        } 
        
        long min=0; 
        for(int j=0;j<n-m;j++)
       { 
            min+=arr[j]; 
        } 
        printf("%d\n",max-min); 
    } 
    return 0;
}


Output:
1                                                                                                          
5 1                                                                                                        
1 2 3 4 5                                                                                                  

Wednesday, July 1, 2020

Find Second largest element in array using JAVA

Find Second largest element in an array using JAVA

PROGRAM:

public class second_largest {

    public static void main(String args[])
    {
        int a[]={ 14, 46, 47, 86, 92, 52, 48, 36, 66, 87 };
        int max=a[0];
        int second_max=a[0];
        for(int i=1;i<a.length;i++)
        {
            if(a[i]>max)
              {
                   second_max=max;
                   max=a[i];
               }
            else if(a[i]>second_max)
            {
                second_max=a[i];
            }
        }
       
        System.out.println(second_max);
    }

}

OUTPUT:
87