CODING PROBLEMS

CODING PROBLEMS

1.Decipher my Ciphertext
Description:

In the language of cryptography, ciphertext refers to a message encoded with a particular keyPlaintext refers to the original, unencoded text. In this problem, both the ciphertext and the key are simply strings of upper-case characters. The ciphertext is generated from the plaintext by “adding” corresponding characters of the plaintext and the key together. If the plaintext is shorter than the key, only some of the key will be used. Similarly, if the plaintext is shorter than the key, the key will be used multiple times.

For example, to encode the plaintext “HELLO” with the key “CAT”:
Plaintext: HELLO
Key: CATCA
Ciphertext: KFFOP

And to encode the plaintext “DOG” with the key “FIDO”:
Plaintext: DOG
Key: FID
Ciphertext: JXK

To add two letters together, use the following convention: A=1, B=2, …, Z=26. If the sum of two letters is greater than 26, subtract 26 from the sum. For example: A + E = 1 + 5 = 6 = F, and D + X = 4 + 24 = 28 = 2 = B.

Given a ciphertext/key pair, determine the corresponding plaintext.

Input Format :
Input will consist of pairs of lines, with the first line being the ciphertext and the second line being the key. Both will consist of only uppercase letters.

Output Format:
For each ciphertext/key pair, print the corresponding plaintext message.

Example:

Sample Input:
HELLO
CAT

Sample Output:
KFFOP


SOLUTION:-


#include<stdio.h>
#include<string.h>
void cipherText(char str1[],char str2[],int n)
{
    int i;

    for(i=0;i<n;i++)
    {
        int s=0;
        str1[i]-=64;
        str2[i]-=64;
        s=str1[i]+str2[i];
        if(s>26)
        s-=26;
    
        s+=64;
        printf("%c",s);
    }
    return;
}
int main(void)
{
    int n,i;
    char str1[50],str2[50];
    scanf("%s",str1);
    scanf("%s",str2);
    int n1=strlen(str1);
    int n2=strlen(str2);
    if(n1<n2)
    {
    n=strlen(str1);    
    }
    else if(n1>n2)
    {
        int k=0;
        for(i=n2;i<n1;i++)
        {str2[i]=str2[k];
        k++;}
        n=strlen(str1);
    }
    else n=n1;
    cipherText(str1,str2,n);
    return 0;
}


 2.Indian Railway – Seating arrangement for Sleeper Class
Write a program to determine the type of berth when the seat/berth number in the train is given.

Input Format:
Input consists of a single integer. Assume that the range of input is between 1 and 72.

Output Format:
Output consists of a single string. [Upper or Middle or Lower or Side Lower or Side Upper]

Sample Input 1:
9

Sample Output 1:
Lower

Sample Input 2:
72

Sample Output 2:
Side Upper

SOLUTION:-

#include<stdio.h>

void checkBerth(int n)

{

    if(n<=72 && n>=1)

    {

        if(n%8==0)

        {printf("Side Upper");

        return;}

        

        {
        int i=1,j=0,p;
        do
            {
            if(p==n)
           { printf("Side Lower");
            return ;}
            else{
             p=(7*i)+j; 
            i++;
            j++;}}while(p<=n);
        }
        
    { int i;
            for(i=1;i<=68;)
            { 
                if(i==n)
                {printf("Lower"); return;}
                if(i%2==0)
                i+=5;
                else i+=3;
            }
            
        }
        
        {int i;
          for(i=2;i<=69;)
        {
            if(i==n)
            {printf("Middle"); return ;}
            if(i%2==0)
            i+=3;
            else i+=5;
            }
            
        }
        
        {int i;
        for(i=3;i<=70;)
        {
            if(i==n)
            {printf("Upper"); return ;}
            if(i%2==0)
            i+=5;
            else i+=3;
        }
    }
}}

int main(void)
{
    int n;
    scanf("%d",&n);
    checkBerth(n);
    return 0;
}

3. ENCODING

In this program you will be given a letter to encode. The difference here is that different rules are used for different letters and the counting process can cause you to wrap around the alphabet. Using the numerical value of each letter (A=1, B=2, ... Z= 26) the rules are as follows:

If the letter is between the given letters, inclusive:
The number of letters to count is given by:
A–E
Multiply its numerical value by 2
F–J
Divide its numerical value by 3. Multiply the integer remainder by 5
K–O
Divide its numerical value by 4. Multiply the integer remainder by 8.
P–T
Add 10.
U- Z
Find the largest integer factor of its numerical value less than the value itself. Multiply it by 12.

As an example if the letter to encode is a B, the B has a numerical value of 2 and encodes to a  4 and becomes a D, the 4th letter of the alphabet.
The G has a numerical value of 7. It encodes to a 5 and becomes an E.
The numerical value of Z is 26. Its largest factor is 13. You must count 156 (13*12) letters. This has the effect of wrapping around the alphabet 6 complete times andending at Z. If a numerical value of zero is evaluated print a # symbol.
[Hint: ASCII value of A is 65].
INPUT:
Input consists of an upper case letter.
OUTPUT:
Print the encoded letter it produces.
SAMPLE INPUT 1
B
SAMPLE OUTPUT 1
D
SAMPLE INPUT 2
Z
SAMPLE OUTPUT 2
Z

SOLUTION:-

#include<stdio.h>
void encode(char ch)
{
    int opt,count=0,i,j,k,c;

    if(ch<='E' && ch>='A')
    opt=1;
    else if(ch>='F' && ch<='J')
    opt=2;
    else if(ch>='K' && ch<='O')
    opt=3;
    else if(ch>='P' && ch<='T')
    opt=4;
    else opt=5;
    for(i='A'; i<='Z' ;i++)
    {
        count++;
        if(ch==i)
        ch=count;
    }
    switch(opt)
    {
        case 1: 
        ch *= 2;
        if(ch==0)
        {
        printf("#");
            return;
        }
        if(ch>26)
        {
            ch%=26;
            if(ch==0)
            ch=26;
        }
        ch+=64;
        printf(" %c",ch);
        break;
        
        case 2:
        ch=ch%3;
        ch=ch*5;
        if(ch==0)
        {printf("#"); return;}
         if(ch>26)
        {
            ch%=26;
            if(ch==0)
            ch=26;
        }
        ch+=64;
        printf("%c",ch);
        break;
        
        case 3:
        ch%=4;
        ch*=8;
        if(ch==0)
        {
            printf("#");
            return ;
        }
         if(ch>26)
        {
            ch%=26;
            if(ch==0)
            ch=26;
        }
            ch+=64;
            printf("%c",ch);
            break;
            
         case 4:
         ch+=10;
         if(ch==0)
         {printf("#");
             return;
         }
          if(ch>26)
        {
            ch%=26;
            if(ch==0)
            ch=26;
        }
         ch+=64;
         printf("%c",ch);
         break;
         
         case 5:
         for(j=1;j<ch;j++)
         {
             if(ch%j==0)
             k=j;
         }
         c=k*12;
         if(c==0)
         {
             printf("#");
             return;
         }
         if(c>26)
         {
             c%=26;
             if(c==0)
             c=26;}
             c+=64;
             printf("%c",c);
             break;
             
             default:return ;
         }
    
}
int main(void)
{
    char ch;
    scanf(" %c",&ch);
    encode(ch);
    return 0;
}

4.TENNIS COURT

Many of the current final year students of a college are excellent tennis players. They always emerge as champions in the State Level Inter-Collegiate Tennis Meet. They want their juniors to carry on their legacy in the field of tennis and they started training their juniors in tennis. They were happy to see them play very well during their coaching sessions. They had a very comfortable win over one of the top teams in the game played at their college. But to their dismay, their juniors performed very poorly in the games conduced at other venues.

The think-tank of the College Tennis club was pondering over the reasons for their poor performance in the meets.  David came up with a possible reason for the poor performance of their junior team. The tennis court in their college is a grass court but the tennis courts in most other competitive venues are synthetic courts. Their junior team has learnt playing tennis in the grass court and they found it very difficult to adapt their game to synthetic courts. The entire team agreed with this reason and they planned to request the chairman to convert the grass tennis court to synthetic tennis court.

They sent a request to the Chairman and he readily agreed but he asked to do it with minimal expenditure. He gave them square stones.

The tennis court in the college is rectangular in shape of size n*m metres. We need to pave the court with square stones. The square stone is of size a*a.

What is the least number of stones needed to pave the tennis court? It's allowed to cover the surface larger than the Tennis Court, but the court has to be covered. It's not allowed to break the stones. The sides of stones should be parallel to the sides of the court.
Input Format:
Input consists of 3 integers. The first integer corresponds to 'n', the length of the court. The second integer corresponds to 'm', the width of the court. The third integer corresponds to 'a', the side of the square stone.
Output Format:
Output consists of ta single integer k, where k is the least number of stones needed.

Sample Input:
6
6
4

Sample Output:
4


SOLUTION:-

#include<stdio.h>
void countStones(int n,int m,int a)
{
    int count1,count2;
    count1=n/a;
    if(n%a!=0)
    count1++;
    
    count2=m/a;
    if(m%a!=0)
    count2++;

    printf("%d",count1*count2);
    return;
}
int main(void)
{
    int n,m,a;
    scanf("%d%d%d",&n,&m,&a);
    countStones(n,m,a);
    return 0;
}

5.The Next Palindrome
A positive integer is called a palindrome if its representation in the decimal system is the same when read from left to right and from right to left. For a given positive integer K, write the value of the smallest palindrome larger than K to output.
Input
The first line contains an integer, which corresponds to K. Assume that K is less than 200000.
Output
Output consists of a single integer, which corresponds to the smallest palindrome larger than K.
Sample Input 1:
808
Sample Output 1:
818
Sample Input 2:
2133
Sample Output 2:
2222
SOLUTION:-

#include<stdio.h>
void palindrome(int n)
{
    int i;
    for(i=n+1;i>=n;i++)
    {
        int k=i,rem,res=0;
        while(k!=0)
        {
            rem=k%10;
            res=res*10+rem;
            k/=10;
        }
        if(res==i)
        {
            printf("%d",res);
            return;
        }
    }
}
int main(void)
{
    int n;
    scanf("%d",&n);
    palindrome(n);
    return 0;
}


6.Zero Duplicates

Write a program that will read in a list of numbers and will then print out the same list except numbers that have already been printed will be printed as a zero instead.
Input Format:
Input consists of n+1 integers where n is the number of elements in the list. The first integer corresponds to n. The remaining n integers correspond to the elements in the list.
Assume that the maximum value of n is 100.

Output Format:
Output consists of n integers on separate lines.

Sample Input :
5
2
3
4
3
6

Sample Output:
2
3
4
6

SOLUTION:-

#include<stdio.h>
void printno(int a[],int n)
{
    int i;
    for(i=0;i<n;i++)
    {int j;
        for(j=i+1;j<n;j++)
        {
            if(a[i]==a[j])
            a[j]=0;
        }
    }
    for(i=0;i<n;i++)
    printf("%d\n",a[i]);
    return;
}
int main(void)
{
    int n,i;
    int a[100];
    scanf("%d",&n);
    for(i=0;i<n;i++)
    scanf("%d",&a[i]);
    
    printno(a,n);
    return 0;
}

7.Anagrams

Write a program to find whether the 2 given strings are anagrams or not.
Anagrams are words or phrases made by mixing up the letters of other words or phrases,

Input and Output Format:
Input consists of 2 string. Assume that all characters in the string are lowercase letters or spaces and the maximum length of the string is 100.
Refer sample input and output for formatting specifications.
All text in bold corresponds to input and the rest corresponds to output.

Sample Input and Output 1:
Enter the first string
anitha
Enter the second string
amphisoft
anitha and amphisoft are not anagrams

Sample Input and Output 2:
Enter the first string
the eyes
Enter the second string
they see
the eyes and they see are anagrams

SOLUTION:-

#include<stdio.h>
#include<string.h>
int main()
{
    int v1[26],v2[26],i;
    char s1[100],s2[100];
    printf("Enter the first string\n");
    gets(s1);
    printf("Enter the second string\n");
    gets(s2);
    for(i=0;i<26;i++)
    v1[i]=v2[i]=0;
    
    for(i=0;i<strlen(s1);i++)
    v1[s1[i]-'a']++;
    
    for(i=0;i<strlen(s2);i++)
    v2[s2[i]-'a']++;
    
    for(i=0;i<26;i++)
    {
        if(v1[i]!=v2[i])
        {
            printf("%s and %s are not anagrams\n",s1,s2);
            return 0;
    }}
    
    printf("%s and %s are anagrams\n",s1,s2);
    return 0;
}


8.Vowel or Consonant

Write a program to determine whether the input character is a vowel or consonant.

Input and Output Format:
Input consists of a single character.
Output consists of a string --- “Vowel” / “Consonant” / “Not an alphabet”
Refer sample input and output for formatting specifications.
All text in bold corresponds to input and the rest corresponds to output.

Sample Input and Output 1:
Enter a character
a
Vowel

Sample Input and Output 2:
Enter a character
Z
Consonant

Sample Input and Output 3:
Enter a character
#
Not an alphabet

SOLUTION:-

#include<stdio.h>

#include<ctype.h>

int main(void)

{

    char ch;

    printf("Enter a character\n");

    scanf(" %c",&ch);

    if(isalpha(ch))

    {
        ch=toupper(ch);
        if(ch=='A'||ch=='E'||ch=='I'||ch=='O'||ch=='U')
        printf("Vowel");
        else printf("Consonant");
    }
    else printf("Not an alphabet");
    return 0;
}

9.Fibonacci Roots

The first two terms in the Fibonacci sequence are 0 and 1, respectively, and each subsequent term is the sum of the previous two. Using this definition to calculate the first several terms in the sequence, we get
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
Let us define the Fibonacci roots of a positive integer n to be the two smallest consecutive Fibonacci numbers whose sum is greater than or equal to n.

Input Format:
Input consists of a single integer which corresponds to n.
Assumption:
Assume that n is less than or equal to 2000.
Output Format:
Output consists of integers, separated by a space.

Sample Input 1 :
31
Sample Output 1:
13 21

Sample Input 2 :
89
Sample Output 2:
34 55
SOLUTION:-

#include<stdio.h>

int main(void)

{

    int n,first=0,second=1,i,sum;

    

    scanf("%d",&n);

     for(i=0;i<=n;i++)

        {

            sum=first+second;
            if(sum>=n)
            {printf("%d %d",first,second);
            break;}
            first=second;
            second=sum;
            
        }
    return 0;
}

10.SMS Language

SMS language or textese (also known as txt-speak, txtese, chatspeak, txt, txtspk, txtk, txto, texting language, txt lingo, SMSish, txtslang,or txt talk) is a term for theabbreviations and slang commonly used with mobile phone text messaging.

Some of the abbreviations used are
  • s for yes
  • u for you
  • 2day for today
  • y for why

Many grandpa's have started sending SMSes to their grand children. But they are not familiar with the SMS lingo.

Can you help them by writing a program that would convert a given text in proper English to SMS lingo? Consider only the 4 words listed above.

Input Format:
Input consists of a single string. Assume that the maximum length of the string is 200 and all letters are in lower-case.

Output Format:
Output consists of a single string.

Sample Input 1:
where were you yesterday?

Sample Output 1:
where were u sterday?

Sample Input 2:
why is today a working day for you?

Sample Output 2:
y is 2day a working day for u?

SOLUTION:-

#include<stdio.h>
#include<string.h>
void smsLingo(char str1[],int size)
{
    int i,j;
    for(i=0;i<size;i++)
    {
        if(str1[i]=='y' && str1[i+1]=='e' && str1[i+2]=='s')
        {
            str1[i]='s';
            for(j=i+1;j<size;j++)
            str1[j]=str1[j+2];
            
            
        }
        
        
        if(str1[i]=='y' && str1[i+1]=='o' && str1[i+2]=='u')
        {
            str1[i]='u';
            for(j=i+1;j<size;j++)
            str1[j]=str1[j+2];
            
            
        }
        
        
        if(str1[i]=='t' && str1[i+1]=='o' && str1[i+2]=='d' && str1[i+3]=='a' && str1[i+4]=='y')
        {
            str1[i]='2';
            str1[i+1]='d';
            str1[i+2]='a';
            str1[i+3]='y';
            for(j=i+4;j<size;j++)
            str1[j]=str1[j+1];
            
            
        }
        
        if(str1[i]=='w' && str1[i+1]=='h' && str1[i+2]=='y')
        {
            str1[i]='y';
            for(j=i+1;j<size;j++)
            str1[j]=str1[j+2];
            
            
        }
    }
    printf("%s",str1);
    return;
}
int main(void)
{
    char str1[100];
    scanf("%[^\n]s",str1);
    int size=strlen(str1);
    smsLingo(str1,size);
    return 0;
}

                                  11.problem statement
Given a string , write a program to title case every first letter of words in string.

Input:
The first line consists of an integer i.e number of test cases. The first and only line of each test case consists of a string s

Output:
Print the required output.

Constraints: 
1<=T<=100
1<=|Length of string|<=1000

Example:
Input:

1
I love programming

Output:
I Love Programming



first solution in C->


#include<stdio.h>
#include<string.h>
int lower(char c)
{
    if(c>='a' && c<='z')
        return 1;
    return 0;    
}
int main() {
int t;
scanf("%d\n",&t);
while(t--)
{
    char s[1000];
    gets(s);
    int l=strlen(s),i;
    if(lower(s[0]))
       s[0]=s[0]-32;
    for(i=0;i<l-1;i++)
    {
        if(s[i]==' ')
        {
            if(lower(s[i+1]))
               s[i+1]=s[i+1]-32;
        }
    }
  printf("%s\n",s);  
}
return 0;
}
second solution in C->

#include<string.h>
#include<stdio.h>
#include<ctype.h>
int main(void)
{
    char a[1000];
    int t,i;
    scanf("%d",&t);
    while(t>=1)
    {
        scanf("\n");
        scanf("%[^\n]s",a);
        int n=strlen(a);
        for(i=0;i<n;i++)
        {
            a[0]=toupper(a[0]);
            if(a[i]==' ')
                a[i+1]=toupper(a[i+1]);

        }

        printf("%s",a);
        t--;
    }
    return 0;

}

                                                 12.Segregate 0's and 1's

You are given an array of 0s and 1s in random order. Segregate 0s on left side and 1s on right side of the array. Traverse array only once.

Example:
Input:
1 0 1 1 0
Output:
0 0 1 1 1
Input:
1 0 1 1 1 1 1 0 0 0
Output:
0 0 0 0 1 1 1 1 1 1

solution:-
#include <stdio.h>
int main() {
int t, n,i;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
int a[n];
for(i=0;i<n;i++)
{
    scanf("%d",&a[i]);
}
 segregate(a, n); 
 print(a, n);
 printf("\n");
}
return 0;
}
void segregate(int a[], int n)
{
    int count = 0,i; 
    for(i=0;i<n;i++) {
        if (a[i]==0)
            count++;
    }
    for(i=0;i<count;i++)
        a[i]=0;
    for(i=count;i<n;i++)
        a[i]=1;
}
void print(int a[], int n)
{
    for(int i=0;i<n;i++)
        printf("%d ", a[i]);
}


                                                         15.Find Majority Element


Write a program to find the majority element in the array. A majority element in an array A[] of size n is an element that appears more than n/2 times (and hence there is at most one such element).  If input array doesn't contain a majority element, then output "NO Majority Element"
Input:  The first line of the input contains T denoting the number of testcases. The first line of the test case will be the size of array and second line will be the elements of the array.

Output: For each test case the output will be the majority element of the array.

Constraints:
1 <=T<= 100
1 <=N<= 100
0 <= a[i]<= 100

Example:
Input:
2
5
3 1 3 3 2
3
1 2 3
Output:
3
NO Majority Element
SOLUTION:-
#include<stdio.h>
int main(void)
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int n,i,j,k=0,count=0;
        scanf("%d",&n);
        int a[n];
        for(i=0;i<n;i++)
          scanf("%d",&a[i]);

        for(i=0;i<n;i++)
        {   count=0;
            for(j=0;j<n;j++)
                if(a[i]==a[j])
                    count++;

                if(count>n/2)
                  {     k=1;
                       printf("%d",a[i]);
                      break;}
        }
        if(k==0)
        printf("NO majority element");
        }
       return 0;
}

                                         16.Traverse Matrix In Spiral Form
Given a m*n matrix, traverse the matrix in spiral form.
Input: 
The first line of the input contains a single integer T, denoting the number of test cases. Then T test cases follow. Each test case has 2 lines
m and n separated by a space
The mxn values separated by spaces
Output:
Spiral array will be displayed in a single line.
Constraints:
1 <=T<= 100
2<=m,n<=10
0<=A[i]<=100
Example:
Input:
1
4 4
1    2     3   4 
5     6    7   8 
9    10  11 12 
13  14  15 16
Output:
1   2      3   4 
8   12   16 15 
14 13   9    5 
6    7   11   10


how to traverse a matrix in spiral form->



solution:-

#include<stdio.h>
int main() {

int t;
scanf("%d",&t);
while(t--){
int m,n,i,j;
scanf("%d %d",&m,&n);
int a[m][n];
for(i=0;i<m;i++){
    for(j=0;j<n;j++){
        scanf("%d",&a[i][j]);
    }
}
int l=0,k=0;
while(l<m&&k<n){
    for(i=k;i<n;i++){
        printf("%d ",a[l][i]);
    }
    l++;
    for(i=l;i<m;i++){
        printf("%d ",a[i][n-1]);
    }
    n--;
    if(l<m){
        for(i=n-1;i>=k;i--){
            printf("%d ",a[m-1][i]);
        }
        m--;
    }
    if(k<n){
        for(i=m-1;i>=l;i--){
            printf("%d ",a[i][k]);
        }
        k++;
    }
    
}
printf("\n");
}
return 0;

}

                                          17.ZERO MATRIX

Design and implement an algorithm such that if an element in an MxN matrix is 0, its entire row and column is set to 0. Don't use any additional buffer.

Sample Input and Output :
Enter the size of the matrix
3

Enter the elements in the matrix rowwise
3
5
9
2
1
0
0
2
8

The input matrix is

3  5  9
2  1  0
0  2  8

The processed matrix is

0  5  0
0  0  0

0  0  0
SOLUTION:-