Tuesday, May 28, 2019

Parity

  • Problem Description

    Ram and Sita playing the parity game. Two types of parity are there. One is odd parity and next is even parity. Ram will say the parity type, number of bits present in the message. Sita will find out the parity value and then she append the parity bit with the message bit. Sita will calculate the parity bit by counting the total number of one present int the message told by Ram. If she need to convert to even parity, the total count of one is even then the parity value is 0 otherwise 1 vice verse for odd parity conversion. calculate the parity value for message need to send. First line the type of parity.Second line is the number of bits present in the message.Followed by the Message bits.
  • Test Case 1

    Input (stdin)
    even

    5

    1 1 1 0 1

    Expected Output
    111010
  • Test Case 2

    Input (stdin)
    odd

    5

    1 1 1 0 1

    Expected Output
    111011
Solution

#include<stdio.h>
#include<string.h>
int main()
{
    char parity_value[7],parity_check[7];
    int message_index,count=0;
    int message[100];
    int i;
    scanf ("%[^\n]s",parity_value);
    scanf("%d",&message_index);

    for (i=0;i<message_index;i++)
    {
        scanf("%d",&message[i]);
    }
     for(i=0;i< message_index;i++)
    {
        if(message[i]==1)
            count++;
    }
        if(count%2==0)
        {
        strcpy(parity_check,"even");
        }
    else
    {
       strcpy(parity_check,"odd");
    }

    if(strcmp(parity_check,parity_value)==0)
    {
        message[message_index]=0;
    }

    else
    {
       message[message_index]=1;
    }

    for(i=0;i<=message_index;i++)
    {
        printf("%d",message[i]);
    }
    return 0;
}

Tuesday, May 14, 2019

Day by Date

  • Problem Description

    "Computation of the date either previous or forthcoming dates is quiet easy. But it is quiet difficult to calculate the day from a particular given date. You are required to find a day from a particular date given to you.
    Input
    It consists of a single line entry consisting of date in format dd mm yyyy. i.e. the input line consists of the three numbers written in order followed by spaces. Eg. Input for 18-12-1990 is be written as 18 12 1990
    Output
    It consists of single line output showing the day for that particular date.
    "
  • Test Case 1

    Input (stdin)
    14 3 2012

    Expected Output
    Wednesday
  • Test Case 2

    Input (stdin)
    25 01 2011

    Expected Output
    Tuesday

Solution

#include<stdio.h>
int main()
{
int month[]={31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},i,d,m,y;
char* day[]= {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
            scanf("%d %d %d", &d, &m, &y);
    if (y % 4 == 0 && (y % 100 != 0 || y % 400 == 0))
    month[1] = 29;
     
    if (y < 1900 || m < 1 || m > 12 || d < 1 || d > month[m - 1]) {
    // printf("This is an invalid date.\n");
    return 1;
    }
     
    for (i = 1900; i < y; i++)
    if (i % 4 == 0 && (i % 100 != 0 || i % 400 == 0))
    d += 366;
    else
    d += 365;
     
    for (i = 0; i < m - 1; i++)
    d += month[i];
     
    printf("%s\n", day[d % 7]);
    return 0;
}
 

Biased Chandan

  • Problem Description


    Chandan is an extremely biased person, and he dislikes people who fail to solve all the problems in the interview he takes for hiring people. There are n people on a day who came to be interviewed by Chandan. Chandan rates every candidate from 0 to 10. He has to output the total ratings of all the people who came in a day. But, heres the problem: Chandan gets extremely frustrated when someone ends up scoring a 0 in the interview. So in frustration he ends up removing the candidate who scored that 0, and also removes the candidate who came before him. If there is no candidate before the one who scores a 0, he does nothing.Youve to find the summation of all the ratings in a day for Chandan.

    Input constraints:

    The first line of input will contain an integer n. The next n lines will contain an integer, where the ith integer represents the rating of the ith person.

    Output constraints:
    Print the required sum.
    Constraints:
    1 <= n <= 5 * 103
    0 <= Value of ratings <=10
  • Test Case 1

    Input (stdin)
    5

    2 3 0 7 0

    Expected Output
    2
  • Test Case 2

    Input (stdin)
    4

    4 3 7 6

    Expected Output
    20
Solution

#include<stdio.h>
#define SIZEN 5003
 
int main()
{
int N, rating[SIZEN];
int i, j, total;
//freopen("input.txt", "r", stdin);
scanf("%d", &N);
for (i = 0; i < N; i++) {
scanf("%d", &rating[i]);
if (0 == rating[i]) for (j = i - 1; j >= 0; j--) if (rating[j]) {
rating[j] = 0;
break;
}
}
total = 0;
for (i = 0; i < N; i++)
total += rating[i];
printf("%d\n", total);
return 0;
}

Shape Up

  • Problem Description

    Write a program that determines the name of a shape from its number of sides. Read the number of sides from the user and then report the appropriate name as part of a meaningful message. 

    Your program should support shapes with anywhere from 7 up to (and including) 10 sides. 

    If a number of sides outside of this range is entered then your program should display an appropriate error message.

    1. If the input is 9 then display as Nanogon

    2. If the input is 10 display as Decagon

    3. If the input is any another number then display the message "Input should be from 7 to 10"

    Mandatory:

    Use Switch case
  • Test Case 1

    Input (stdin)
    7

    Expected Output
    Heptagon
  • Test Case 2

    Input (stdin)
    8

    Expected Output
    Octagon
Solution

#include <stdio.h>
int main()
{
int a;
  scanf ("%d",&a);
  switch (a)
  {
    case 7: 
      printf ("Heptagon");
      break;
    case 8: 
      printf ("Octagon");
      break;
    case 9: 
      printf ("Nanogon");
      break;
    case 10: 
      printf ("Decagon");
      break;
    default: 
      printf ("Not Identified");
  }
  
            return 0;
}

Sunday, May 12, 2019

Pointers - 24

  • Problem Description

    Write a function that accepts a string using pointers. In the function ,delete all the occurrences of a given character and display the modified string on the screen

    Input and Output Format:

    Refer sample input and output for formatting specification.

    All float values are displayed correct to 2 decimal places.

    All text in bold corresponds to input and the rest corresponds to output.

  • Test Case 1

    Input (stdin)
    SRM University

    S

    Expected Output
    RM University
  • Test Case 2

    Input (stdin)
    SRM University

    R

    Expected Output
    SM University
  • Program
  • #include <stdio.h>
    #include<string.h>
    int main()
    {
    char str[15],ch,cat[10];
    scanf("%s%s",str,cat);
    scanf("%s",&ch);
    int i=0,j,len;
    len=strlen(str);
    for(i=0;i<len;i++)
    {
    if(str[i]==ch)
    {
    for(j=i;j<len;j++)
    {
    str[j]=str[j+1];
    }
    len--;
    i--;
    }
    }
    printf("%s ",str);
    printf("%s",cat);

    return 0;
    }

LOWER TRIANGULAR MATRIX

  • Problem Description

    A lower triangular matrix is a square matrix in which all the elements above the diagonal are zero.

    That is, all the non-zero elements are in the lower triangle:

    Write a C program to find whether a given matrix is a lower triangular matrix or not.

    Input Format:

    The input consists of (n*n+1) integers. The first integer corresponds to the number of rows/columns in the matrix. The remaining integers correspond to the elements in the matrix. The elements are read in rowwise order, first row first, then second row and so on. Assume that the maximum value of m and n is 5.


    Output Format:

    Print yes if it is a lower triangular matrix . Print no if it is not a lower triangular matrix.
  • Test Case 1

    Input (stdin)
    3 3

    1 0 0

    2 1 0

    1 1 1

    Expected Output
    yes
  • Test Case 2

    Input (stdin)
    3 3

    1 1 0

    2 2 0

    1 2 1

    Expected Output
    no
  • Program
  • #include <stdio.h>

    #include<math.h>
    int main()
    {
    int i,j,x,y,flag=0;
    scanf("%d%d",&x,&y);
    int a[x][y];
    for(i=0;i<x;i++)
    {
    for(j=0;j<y;j++)
    {
    scanf("%d",&a[i][j]);
    }
    }
    for(i=0;i<x;i++)
    {
    for(j=i+1;j<y;j++)
    {
    if(a[i][j]!=0)
    {
    flag=1;
    }
    }
    }
    if(flag==0)
    printf("yes");
    else
    printf("no");




    return 0;
    }

Pointers 5

  • Problem Description

    Write a program to find Largest of three integer values using pointers

    Input and Output Format:

    Refer sample input and output for formatting specification.

    All float values are displayed correct to 2 decimal places.

    All text in bold corresponds to input and the rest corresponds to output.

    Mandatory:

    Use pointers concepts
  • Test Case 1

    Input (stdin)
    3

    4 3 1

    Expected Output
    4 is largest
  • Test Case 2

    Input (stdin)
    3

    723 4444 123

    Expected Output
    4444 is largest
  • Program
  • #include <stdio.h>
    int main()
    {
    int a[100],i,j,n,t,*b;

    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
    scanf("%d",&a[i]);
    b=&a[i];
    }
    for(i=0;i<n;i++)
    {
    for(j=i+1;j<n;j++)
    {
    if(a[i]>a[j])
    {
    t=a[i];
    a[i]=a[j];
    a[j]=t;
    }
    }
    }
    b=&a[n-1];
    printf("%d is largest",*b);




    return 0;
    }

IO 3

  • Problem Description

    Write a program to calculate the perimeter of a triangle using Heros formula

    Use the Following Formula:

    s = (a+b+c)/2

    area = sqrt(s*(s-a)*(s-b)*(s-c))

    Input and Output Format:

    Refer sample input and output for formatting specification.

    All float values are displayed correct to 2 decimal places.

    All text in bold corresponds to input and the rest corresponds to output.

  • Test Case 1

    Input (stdin)
    2.0 3.0 4.0

    Expected Output
    perimeter of triangle is=2.904737
  • Test Case 2

    Input (stdin)
    3.4 5.6 7.9

    Expected Output
    perimeter of triangle is=8.178576
  • Program
  • #include <stdio.h>
    #include<math.h>
    int main()
    {
    float a,b,c,s,area;
    scanf("%f%f%f",&a,&b,&c);
    s=(a+b+c)/2;
    area=sqrt(s*(s-a)*(s-b)*(s-c));
    printf("perimeter of triangle is=%f",area);
    return 0;

Tick Tick Tick

  • Problem Description


    Hemas teacher give one assignment to him. she should write a code to find the time entered in hours, minutes and seconds into seconds. Help him to solve this task
  • Test Case 1

    Input (stdin)
    2 30 4

    Expected Output
    9004
  • Test Case 2

    Input (stdin)
    10 5 60

    Expected Output
    36360
  • Program
  • #include <stdio.h>
    int main()
    {
    int a,b,c,d;
    scanf("%d%d%d",&a,&b,&c);
    d=(a*60*60)+(b*60)+(c);
    printf("%d\n",d);
    return 0;
    }

Catch the Second..

  • Problem Description

    A bag contains set of integers and it is discovered by Ram. There is sheet in that bag. The person who found the bag should find the second largest number from the set of numbers. If the rightly found the will get a gold.. If you wish get the prize you can also try...
  • Test Case 1

    Input (stdin)
    5

    5 3 1 2 6

    Expected Output
    5
  • Test Case 2

    Input (stdin)
    6

    16 58 2 1 6 99

    Expected Output
    58
  • Program
  • #include <stdio.h>
    int main()
    {
    int a[50],size,i,j=0,big,secondbig;
    scanf("%d",&size);
    for(i=0;i<size;i++)
    scanf("%d",&a[i]);
    big=a[0];
    for(i=1;i<size;i++)
    {
    if(big<a[i])
    {
    big=a[i];
    j=i;
    }
    }
    secondbig=a[size-j-1];
    for(i=1;i<size;i++)
    {
    if(secondbig<a[i]&&j!=i)
    secondbig=a[i];
    }
    printf("%d",secondbig);
    return 0;
    }

Duplicate Removal

  • Problem Description

    Program to Delete duplicate elements from an array
  • Test Case 1

    Input (stdin)
    5

    1 3 4 5 3

    Expected Output
    1 3 4 5
  • Test Case 2

    Input (stdin)
    3

    1 3 3

    Expected Output
    1 3
  • Program
  • #include <stdio.h>
    int main()
    {
    int arr[20], i, j, k, size;


    scanf("%d", &size);


    for (i = 0; i < size; i++)
    scanf("%d", &arr[i]);

    printf("\n");
    for (i = 0; i < size; i++) {
    for (j = i + 1; j < size;) {
    if (arr[j] == arr[i]) {
    for (k = j; k < size; k++) {
    arr[k] = arr[k + 1];
    }
    size--;
    } else
    j++;
    }
    }

    for (i = 0; i < size; i++) {
    printf("%d ", arr[i]);
    }
    return 0;
    }

Compare 2 arrays

  • Problem Description

    Write a program to find whether 2 arrays are the same.

    Input Format:

    Input consists of 2n+1 integers. The first integer corresponds to "n" , the size of the array. The next "n" integers correspond to the elements in the first array. The next "n" integers correspond to the elements in the second array.Assume that the maximum value of n is 15.

    Output Format:

    Print yes if the 2 arrays are the same. Print no if the 2 arrays are different.
  • Test Case 1

    Input (stdin)
    5

    2 3 6 8 -1

    2 3 6 8 -1

    Expected Output
    yes
  • Test Case 2

    Input (stdin)
    5

    2 3 6 8 -1

    2 3 6 8 10

    Expected Output
    no
  • Program
  • #include <stdio.h>
    int main()
    {
    int i,j,n,a[50],b[50],count=0;
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
    scanf("%d ",&a[i]);
    scanf("%d ",&b[i]);
    }
    for(i=0;i<n;i++)
    {
    for(j=0;j<n;j++)
    if(a[i]==b[j])
    count=count+1;
    }
    if(count==n)
    printf("yes");
    else
    printf("no");
    return 0;
    }

Alternate

  • Problem Description

    There is a machine in a lab which is working not properly. It displays the alternative elements in a set of input. The lab instructor asked the students to write a C Program for this scenario. i.e. to Print the Alternate Elements in an Array
  • Test Case 1

    Input (stdin)
    5

    21 8 41 13 15

    Expected Output
    21 41 15
  • Test Case 2

    Input (stdin)
    9

    2 8 4 1 3 6 8 11 38

    Expected Output
    2 4 3 8 38
  • Program
  • #include <stdio.h>
    #include <stdlib.h>

    int main()
    {
    int i,n;

    scanf("%d",&n);
    int a[n];
    for(i=0;i<n;i++)
    {

    scanf("%d",&a[i]);
    }
    printf("\n");
    for(i=0;i<n;i++)
    //printf("%d \t",a[i]);

    printf("\n\t");
    for(i=0;i<n;i=i+2)
    printf("%d \t",a[i]);

    return 0;
    }

W String

  • Problem Description

    Kira likes to play with strings very much. Moreover he likes the shape of W very much. He takes a string and try to make a W shape out of it such that each angular point is a # character and each sides has same characters. He calls them W strings.

    For example, the W string can be formed from "aaaaa#bb#cc#dddd" such as:

    a
    a d
    a # d
    a b c d
    a b c d
    # #
    He also call the strings which can generate a W shape (satisfying the above conditions) W strings.

    More formally, a string S is a W string if and only if it satisfies the following conditions (some terms and notations are explained in Note, please see it if you cannot understand):

    The string S contains exactly 3 # characters. Let the indexes of all # be P1 < P2 < P3 (indexes are 0-origin).
    Each substring of S[0, P11], S[P1+1, P21], S[P2+1, P31], S[P3+1, |S|1] contains exactly one kind of characters, where S[a, b] denotes the non-empty substring from a+1th character to b+1th character, and |S| denotes the length of string S (See Note for details).
    Now, his friend Ryuk gives him a string S and asks him to find the length of the longest W string which is a subsequence of S, with only one condition that there must not be any # symbols between the positions of the first and the second # symbol he chooses, nor between the second and the third (here the "positions" we are looking at are in S), i.e. suppose the index of the #s he chooses to make the W string are P1, P2, P3 (in increasing order) in the original string S, then there must be no index i such that S[i] = # where P1 < i < P2 or P2 < i < P3.

    Help Kira and he wont write your name in the Death Note
  • Test Case 1

    Input (stdin)
    1

    aaaaa#bb#cc#dddd

    Expected Output
    16
  • Test Case 2

    Input (stdin)
    1

    acb#aab#bab#accba

    Expected Output
    10
  • Program
  • #include<stdio.h>
    #include<string.h>
    #include<malloc.h>

    //using namespace std;
    void program();
    char s[10003];
    int *max1, *max2, *max3;
    int k1=0;

    void fxn(int n)
    {
    int x[30]={0};
    int x2[30]={0};
    int x3[30]={0};
    int max=0,i,max_2=0,max_3=0;
    int j,k=0;
    for(i=0;i<n;i++)
    {
    //printf("%c",s[i]);
    if(s[i]=='#')
    {
    max1[k]=max;
    max2[k]= max_2;
    for(j=0;j<30;j++) x2[j]=0;
    max_2=0;
    k++;
    continue;
    }
    x[s[i]-'a']++;
    x2[s[i]-'a']++;
    if(x2[s[i]-'a']>max_2)
    max_2= x2[s[i]-'a'];

    if(x[s[i]-'a']>max)
    max= x[s[i]-'a'];
    }
    k--;
    k1=k;
    for(i=n-1;i>=0;i--)
    {
    if(s[i]=='#')
    {
    max3[k]= max_3;
    k--;
    continue;
    }
    x3[s[i]-'a']++;
    if(x3[s[i]-'a'] >max_3)
    max_3= x3[s[i]-'a'];
    }


    }
    int main()
    {
    int t;
    scanf("%d",&t);
    while(t--)
    program();
    return 0;
    }

    void program()
    {
    //char s[10003];
    scanf("%s",s);
    //int x[30]= {0};
    int max=0,t,i,count=0;
    //vector<int> v;
    int n;
    for(i=0;s[i]!='\0';i++)
    {
    if(s[i]=='#')
    count++;
    }
    max1= (int*)malloc(sizeof(int)*count);
    max2= (int*)malloc(sizeof(int)*count);
    max3= (int*)malloc(sizeof(int)*count);
    for(i=0;i<count;i++)
    {
    max1[i]=0;
    max2[i]=0;
    max3[i]=0;
    }
    n=strlen(s);
    max=0;
    fxn(n);
    int a,b,c,d;
    for(i=2;i<count;i++)
    {
    a= max1[i-2];
    b= max2[i-1];
    c= max2[i];
    d= max3[i];
    //printf("%d %d %d %d \n",a,b,c,d);
    if(a!=0 && b!=0 && c!=0 && d!=0)
    {
    t= a+b+c+d;
    if(t>max)
    max=t;
    }
    }
    if(max==0)
    printf("%d\n",max);
    else
    printf("%d\n",max+3);
    }

Consecutive String Copy

  • Problem Description

    Write a program that will copy m consecutive characters from a string s1 beginning at position n into another string s2

    Refer sample Input and Output:
    1. The first input corresponds to the string input
    2. The Second Input corresponds to the number of characters to be copied
    3. The third input corresponds to the string index

    Sample Input 1:
    SRMUniversity
    5
    0
    Output 1:
    SRMUn

    Explanation:
    The String index starts from 0 and add five characters after that
    S=0
    R=1
    M=2
    U=3
    n=4

    Sample Input 2:
    SRMAPPLELAB
    6
    3

    Output:
    APPLEL


    Explanation:
    The String index starts from 3 and add six characters after that
    S=0
    R=1
    M=2
    A=3
    P=4
    P=5
    L=6
    E=7
    L=8
    A=9
    B=10

    Output: APPLEL
  • Test Case 1

    Input (stdin)
    SRMUniversity

    5

    0

    Expected Output
    SRMUn
  • Test Case 2

    Input (stdin)
    SRMAPPLELAB

    6

    3

    Expected Output
    APPLEL
  • Program
  • #include <stdio.h>
    #include<string.h>
    #include <stdlib.h>

    int main()
    {

    char a[50];
    int c,b,i;
    scanf("%s",a);
    scanf("%d%d",&c,&b);
    for(i=b;i<b+c;i++)
    printf("%c",a[i]);

    return 0;
    }

Parity

Problem Description Ram and Sita playing the parity game. Two types of parity are there. One is odd parity and next is even parity. Ram will...