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

Monday, 25 February 2019

Pattern 3 44 555 6666 6666 555 44 3

Input:
4

Output:
3
44
555
6666
6666
555
44
3

Source code:
#include<stdio.h>
int main()
{
    int n,i,j,k,s=3;
    scanf("%d",&n);
    for(i=1;i<=n;i++)
    {
        for(k=1;k<=i;k++)
        {
            printf("%d",s);
        }
        s++;
        printf("\n");
    }
    s--;
    for(i=n;i>=1;i--)
    {
        for(k=1;k<=i;k++)
        {
            printf("%d",s);
        }
        s--;
        printf("\n");
    }
    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;
}



Saturday, 22 September 2018

1. To Print Butterfly Pattern using loops


Solution:

#include <stdio.h>

int main()

{
    int i,j,n;
    scanf("%d",&n);
    for(i=1;i<=n;i++)
    {
        for(j=1;j<=2*n;j++)
        {
            if(i<j)
            printf(" ");
            else
            printf("&");
            if(i<=2*n-j)
            printf(" ");
            else
            printf("&");
        }
        printf("\n");
    }
        for(i=1;i<=n;i++)
    {
        for(j=1;j<=2*n;j++)
        {
            if(i>(n-j+1))
            printf(" ");
            else
            printf("&");
            if((i+n)>j)
            printf(" ");
            else
            printf("*");
        }
        printf("\n");
    }
    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;
}

Thursday, 2 August 2018

 ByteLand String

  In the Byteland country a string "S" is said to super ascii string if and only if count of each character in the string is equal to its ascii value. In the Byteland country ascii code of 'a' is 1, 'b' is 2 ...'z' is 26.
Your task is to find out whether the given string is a super ascii string or not.

Input Format:
First line contains number of test cases T, followed by T lines, each containing a string "S".

Output Format:
For each test case print "Yes" if the String "S" is super ascii, else print "No"

Constraints:
1<=T<=100
1<=|S|<=400, S will contains only lower case alphabets ('a'-'z').

Note: All text in bold which is corresponds to input and rest output.
Sample Input and 
Output  :
2
bba

Yes

scca
No

Explanation:
In case 1. String "bba" -
The count of character 'b' is 2. Ascii value of 'b' is also 2.
The count of character 'a' is 1. Ascii value of 'a' is also 1.
Hence string "bba" is super ascii.


SOLUTION :

 #include<stdio.h>
#include<string.h>
int main()
{
    int a[26],i,k,n,c=0,j;
    char s[50];
    printf("enter the number of test cases\n");
    scanf("%d",&n);
for(i=0;i<n;i++)
{
        for(j=0;j<26;j++)
        {
            a[j]=0;
        }
    printf("enter the string\n");
    scanf("%s",&s);
    for(j=0;j<strlen(s);j++)
    {
        k=(int)s[j]-97;
        a[k]++;
    }
    for(j=0;j<26;j++)
    {
        if(a[j]==j+1||a[j]==0)
            c=1;
        else
        {
             c=0;
        break;
        }
    }
    if(c!=0)
        printf("Yes\n");
    else
        printf("No\n");
}
 return 0;
}



 

Tuesday, 31 July 2018

Question: 

To find the number of vowels present in a sentence.

Test Case:

Solution:

#include <stdio.h>
#include <conio.h>
int main()
{
     int cha,i,c=0;
     char ch,str[40]="";
     printf("enter string : ");
     scanf("\n %s",str);
     for(i=0;str[i]!='\0';i++)
     {
     cha = str[i];
     switch(cha){
          case'a' :
          case'A' :
          case'e' :
          case'E' :
          case'i' :
          case'I' :
          case'o' :
          case'O' :
          case'u' :
          case'U' :
          c++;
     }
     }

     printf("\nTotal number of vowels is %d",c);
return 0;
}
Number Challenge
Mike set off with great zeal to the "Kracker Jack Fun Fair 2017". There were numerous activities in the fair, though Mike being a math expert, liked to participate in the Number Challenge.

Mike was given a string D of numbers containing only digits 0's and 1's. His challenge was to make the number to have all the digits same. For that, he should change exactly one digit, i.e. from 0 to 1 or from 1 to 0. If it is possible to make all digits equal (either all 0's or all 1's) by flipping exactly 1 digit then he has to output "Yes", else print "No" (without quotes).

Write a program to help Mike win over his challenge.


Input Format:
First and only input contains a string D of numbers made of only digits 1's and 0's.

Output Format:
Output “Yes" or a "No", depending on whether its possible to make it all 0s or 1s or not.
Refer sample input and output for formatting specifications.


Sample Input1:
101
Sample Output1:
Yes

Sample Input2:
11
Sample Output2:
No


Solution:
#include<stdio.h>
#include<string.h>
int main()
{
    int sum=0,i,l;
    char d[50];
    scanf("%s",d);
    l=strlen(d);
    for(i=0;i<l;i++)
    if(d[i]=='1')
    sum=sum+1;
    if(sum==1||(sum==(l-1)))
    printf("Yes");
    else
    printf("No");
    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...