Showing posts with label TCS National Qualifier Test 2018 Problems. Show all posts
Showing posts with label TCS National Qualifier Test 2018 Problems. Show all posts

Sunday, 26 May 2019

Maximum in sub-arrays of length K:

Given an array of integers and a number k, where 1 <= k <= length of the array, compute the maximum values of each subarray of length k.

For example, given array = [10, 5, 2, 7, 8, 7] and k = 3, we should get: [10, 7, 8, 8], since:

10 because {10,5,2} (10 is maximum in sub array)
7   because {5,2,7} (7 is maximum in sub array)
8   because {2,7,8} (8 is maximum in sub array)
8   because {7,8,7} (8 is maximum in sub array)

Imput :
first line of  input contains integer N,
second line of input contains array of integers of length N;
third line of input contains integer K,the length of sub array

Output:
otuput consists of an array of maximum numbers of each subarray.

Program Code:

#include <stdio.h>
int main()
{
    int k,n,a[100],ind=0;
    int i,j,max[100],h,m;
    scanf("%d",&n);
    for(i=0;i<n;i++)
        scanf("%d",&a[i]);
    scanf("%d",&k);
    h=k;
    for(i=0;i<=n-k;i++)
    {
        
        m=a[i];
for(j=i+1;j<h;j++)
        {
if(a[j]>m)
            {
m=a[j];
            }
        }
        max[i]=m;
        h++;
    }
    
    for(i=0;i<=n-k;i++)
    {
printf("%d ",max[i]);
    }

    return 0;
}

Wednesday, 5 December 2018

                                               SWITCHING 100 BULBS

There are 100 light bulbs lined up in a row in a long room. Each bulb has its own switch and is currently switched off. The room has an entry door and an exit door. There are 100 people lined up outside the entry door. Each bulb is numbered consecutively from 1 to 100. So is each person.

Person No. 1 enters the room, switches on every bulb, and exits. Person No. 2 enters and flips the switch on every second bulb (turning off bulbs 2, 4, 6...). Person No. 3 enters and flips the switch on every third bulb (changing the state on bulbs 3, 6, 9...). This continues until all 100 people have passed through the room.

Find the state of nth switch(i.e.ON/OFF)?
Input:
Nth integer to find state
Output :
State of the Nth switch.
Assume 0 as OFF and 1 as ON.

Sample:
Input:
64
Output:
1

Program:
#include<stdio.h>
int a[150]={0};
int n=100;
int main()
{
    int i,j,k,swit;
    scanf("%d",&swit);
    for(i=1;i<=n;i++)
    {
        for(k=1;k<=n;k++)
        {
            if(k%i==0)
            {
                if(a[k]==0)
                    a[k]=1;
                else if(a[k]==1)
                    a[k]=0;
            }
        }
    }
    for(i=1;i<=n;i++)
    {
        if(i==swit)
        {
            printf("%d",a[i]);
        }
    }
    return 0;
}



Tuesday, 18 September 2018

TCS NINJA CODING Season-2

C program to print Nth number in a series

To find Nth number in series of 
0 0 1 2 2 4 3 6 4 8 5  ........................
The above series even index contains whole numbers and odd index contains even numbers.

When given Nth index number the number at that position is to be printed.
Input:
First line of input contains Nth index number
Output:
Number at Nth index should be printed

Example:

Input:
10
Output:
5
Explanation:
In the above example
When we enter 10 index number then at that 10 index number 5 is present.
0 0 1 2 2 4 3 6 4 8 5

SOURCE CODE:
#include<stdio.h>
int main()
{
    int n,i,j,e=0,od=0,k;
    long int a[10000]={0};
    scanf("%d",&n);
 
    for(i=2;i<=n;)
    {
        if(i%2==0)
        {
            e=e+1;
            a[i]=e;
        }
        else
        {
            od=od+2;
            a[i]=od;
        }
        i++;
    }
    for(i=0;i<=n;i++)
    {
       if(i==n)
        printf("%d ",a[i]);
    }
    return 0;

}

TCS NINJA CODING PROGRAM Season-2 NQT

C Program to replace lower case letters with upper case (Vice-versa) and replacing vowels with * 

Input:
First line of input contains string 1
Second line of input contains string 2
Third line of input contains string 3

Output:
First line of output were replaced by upper case letters 
Second line of output were replaced by lower case letters
Third Line of output contains * replacing vowels.

Example:

Input:
how 
ARE 
you

Output:
HOW
are
y**

SOURCE CODE:

#include<stdio.h>
#include<string.h>
int main()
{
    char str1[10],str2[10],str3[10];
    int i,j,c=0;
    gets(str1);
    gets(str2);
    gets(str3);
    
    
    for(i=0;str1[i]!='\0';i++)
    {  
        if(str1[i]>='a'&&str1[i]<='z')
        {
            str1[i]=str1[i]-32;
        }
    }
    for(i=0;str2[i]!='\0';i++)
    {  
        if(str2[i]>='A'&&str2[i]<='Z')
        {
            str2[i]=str2[i]+32;
        }
    }
  for(i=0;str3[i]!='\0';i++)
    {  
        if(str3[i]=='a'||str3[i]=='A'||str3[i]=='e'||str3[i]=='E'||str3[i]=='i'||str3[i]=='I'||str3[i]=='o'||str3[i]=='O'||str3[i]=='u'||str3[i]=='U')
        {
            str3[i]='*';
        }
    }
   
    puts(str1);
    puts(str2);
    puts(str3);
    return 0;
}

C program to print number of times a digit has been Repeated

Input:
First line contains 'N'
Second line of input contains N number of integers.

Output:
Number of times a digit has been repeated.

Example:

Input:
6
1 1 7 7 7 9 9 9

Output:
2 3 3

Explanation:
1 is repeated for 2 times 7 is repeated for 3 times and 9 is repeated for 3 times.

SOURCE CODE:

/*To find number of times a digit is repeated*/
#include<stdio.h>
int main()
{
    int a[50],n,i,j,c=0,r=0,b[10]={0},k;
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
    }
    
    for(i=0,k=0;i<n;i=j,k++)
    {
        c=0;
        for(j=i;;j++)
        {
            if(a[i]==a[j])
            {
                c++;
            }
            else
            {
                b[k]=c;
                break;
            }
        }
    }
    k=0;
    while(b[k]!=0)
    {
        printf("%d ",b[k]);
        k++;
    }
    return 0;
}

Random password generator in Java

       Random password generator in Java Source code: mport java.io.*; import java.util.*; public class Main { public static void main(Str...