Saturday, December 8, 2018

VALUE INSERTION IN THE ARRAY(MODIFIED)

  • Problem Description
    Write a program using pointers to insert a value in an array.
    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.
  • CODING ARENA
  • #include <stdio.h>
    int main()
    {
       int j,i,n,b;
       scanf("%d", &n);
       int a[n];
       for (i=0;i<n;i++){
          scanf("%d",&a[i]);
       }
       scanf("%d", &b);
       scanf("%d", &j);
       for (i=n-1;i>=j-1;i--)
       {
          a[i+1] = a[i]; }
          a[j]=b;
       for (i=0;i<=n;i++){
          printf("%d\n",a[i]);
       }
       return 0;

    }
  • Test Case 1
    Input (stdin)
    3
    99 199 299
    399
    1
    Expected Output
    99
    399
    199
    299
  • Test Case 2
    Input (stdin)
    4
    1 2 3 4
    5
    2
    Expected Output
    1
    2
    5
    3
    4

VALUE INSERTION IN THE ARRAY

  • Problem Description
    Write a program using pointers to insert a value in an array.
    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.
  • CODING ARENA
  • #include <stdio.h>
    int main()
    {
       int j,i,n,b;
       scanf("%d", &n);
       int a[n];
       for (i=0;i<n;i++){
          scanf("%d",&a[i]);
       }
       scanf("%d", &b);
       scanf("%d", &j);
       for (i=n-1;i>=j-1;i--)
       {
          a[i+1] = a[i]; }
          a[j-1]=b;
       for (i=0;i<=n;i++){
          printf("%d\n",a[i]);
       }
       return 0;

    }
  • Test Case 1
    Input (stdin)
    3
    99 199 299
    399
    1
    Expected Output
    399
    99
    199
    299
  • Test Case 2
    Input (stdin)
    4
    1 2 3 4
    5
    2
    Expected Output
    1
    5
    2
    3
    4

Sunday, September 23, 2018

CASE OF THE ZEROS AND ONES

  • Problem Description

    Andrewid the Android is a galaxy-famous detective. In his free time he likes to think about strings containing zeros and ones.
    Once he thought about a string of length n consisting of zeroes and ones. Consider the following operation: we choose any two adjacent positions in the string, and if one them contains 0, and the other contains 1, then we are allowed to remove these two digits from the string, obtaining a string of length n-2 as a result.
    Now Andreid thinks about what is the minimum length of the string that can remain after applying the described operation several times (possibly, zero)? Help him to calculate this number.

    Input
    First line of the input contains a single integer n (1<=n<=2(10 Power of 5)), the length of the string that Andreid has.
    The second line contains the string of length n consisting only from zeros and ones.

    Output
    Output the minimum length of the string that may remain after applying the described operations several times.
  • CODING ARENA
  • #include<stdio.h>
    #include<stdlib.h>
    int main()
    {
    int n,i,c1=0,c0=0;
    char c[200001];
    scanf("%d",&n);
    scanf("%s",c);

    for(i=0;i<n;i++)
    if(c[i]=='1')
    c1++;
    else
    c0++;

    printf("%d",abs(c1-c0));
    return 0;
    }
  • Test Case 1

    Input (stdin)
    8
    
    11101111
    
    
    Expected Output
    6
  • Test Case 2

    Input (stdin)
    6
    
    110110
    
    
    Expected Output
    2

Remove Repeated Characters in a String

  • Problem Description

    Write a C program to remove all repeated characters in a string
  • CODING ARENA
  • #include <stdio.h>
    #include <string.h>
     
    int main()
    {
      char str[100];
      int i, j, k;

      scanf("%s",str);
      printf("%s\n",str);
     
      for(i = 0; i < strlen(str); i++)
      {
      for(j = i + 1; str[j] != '\0'; j++)
      {
      if(str[j] == str[i])  
    {
      for(k = j; str[k] != '\0'; k++)
    {
    str[k] = str[k + 1];
    }
      }
    }
    }
    printf("%s", str);
      return 0;
    }
  • Test Case 1

    Input (stdin)
    srmuniversity
    
    
    Expected Output
    srmuniversity
    
    srmunivety
  • Test Case 2

    Input (stdin)
    madam
    
    
    Expected Output
    madam
    
    mad

Print NCR

  • Problem Description

    Vidhya has a doubt in finding permutations and combinations. Can you help her to compute nCr of a given number n?
  • CODING ARENA
  • #include <stdio.h>
     
    int fact(int z);
     
    int main()
    {
        int n, r, ncr;
     
        scanf("%d%d", &n, &r);
        ncr = fact(n) / (fact(r) * fact(n - r));
        printf("%d", ncr);
      return 0;
    }
     
    int fact(int z)
    {
        int f = 1, i;
        if (z == 0)
        {
            return(f);
        }
        else
        {
            for (i = 1; i <= z; i++)
    {
                f = f * i;
    }
        }
        return(f);
    }
  • Test Case 1

    Input (stdin)
    2 1
    
    
    Expected Output
    2
  • Test Case 2

    Input (stdin)
    4 2
    
    
    Expected Output
    6

POWER OF 2

  • Problem Description

    Ram challenges Ganga to write a code to check whether an Integer Number is power of two (2) or not. Can you help Ganga to come out of this problem.
  • CODING ARENA
  • #include <stdio.h>
     
    int main()
    {
        int num;
        int tempNum,flag;
         

        scanf("%d",&num);
         
        tempNum=num;
        flag=0;
        /*check power of two*/
        while(tempNum!=1)
        {
            if(tempNum%2!=0){
                flag=1;
                break;
            }
            tempNum=tempNum/2;
        }
      
        if(flag==0)
            printf("YES");
        else
            printf("NO");
          
        return 0;
    }
  • Test Case 1

    Input (stdin)
    32
    
    
    Expected Output
    YES
  • Test Case 2

    Input (stdin)
    36
    
    
    Expected Output
    NO

Convert zerotofive

  • Problem Description

    Given a number your task is to complete the function convertFive which takes an integer n as argument and replaces all zeros in the number n with 5 .Your function should return the converted number .
  • CODING ARENA
  • #include<stdio.h>
    int convertFive(int n)
    {
        if(!n) return 0 ;
        int curr=n%10 ;
        n/=10 ;
        if(!curr) curr=5 ;
        return convertFive(n)*10+curr ;
    }
    int main()
    {
      int n;
      scanf("%d",&n);
      printf("%d",convertFive(n));
      return 0;
    }
  • Test Case 1

    Input (stdin)
    1004
    
    
    Expected Output
    1554
  • Test Case 2

    Input (stdin)
    5006
    
    
    Expected Output
    5556

Kitchen Timetable

  • Problem Description

    There are N students living in the dormitory of Berland State University. Each of them sometimes wants to use the kitchen, so the head of the dormitory came up with a timetable for kitchens usage in order to avoid the conflicts:

    The first student starts to use the kitchen at the time 0 and should finish the cooking not later than at the time A1.
    The second student starts to use the kitchen at the time A1 and should finish the cooking not later than at the time A2.
    And so on.
    The N-th student starts to use the kitchen at the time AN-1 and should finish the cooking not later than at the time AN

    The holidays in Berland are approaching, so today each of these N students wants to cook some pancakes. The i-th student needs Bi units of time to cook.

    The students have understood that probably not all of them will be able to cook everything they want. How many students will be able to cook without violating the schedule?
    Input

    The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows.

    The first line of each test case contains a single integer N denoting the number of students.

    The second line contains N space-separated integers A1, A2, ..., AN denoting the moments of time by when the corresponding student should finish cooking.

    The third line contains N space-separated integers B1, B2, ..., BN denoting the time required for each of the students to cook.
    Output

    For each test case, output a single line containing the number of students that will be able to finish the cooking.
    Constraints

    Should contain all the constraints on the input data that you may have. Format it like:

    1 <= T <= 10
    1 <= N <= 104
    0 < A1 < A2 < ... < AN < 109
    1 <= Bi <= 109 
  • CODING ARENA
  • #include<stdio.h>
    int main(){
    int T,N;
    scanf("%d",&T);
    while(T--){
    scanf("%d",&N);
    int A[N-1],i;
    for(i=0;i<N;i++){
    scanf("%d",&A[i]);
    }
    int B[N-1];
    for(i=0;i<N;i++){
    scanf("%d",&B[i]);
    }
    int count=0,temp=A[0];
    for(i=0;i<N;i++){
    if(B[i]<=temp){
    ++count;
    }
    temp=A[i+1]-A[i];
    }
    printf("%d\n",count);

    }
    return 0;
    }
  • Test Case 1

    Input (stdin)
    2
    
    3
    
    1 10 15
    
    1 10 3
    
    3
    
    10 20 30
    
    15 5 20
    
    
    Expected Output
    2
    
    1
  • Test Case 2

    Input (stdin)
    1
    
    3
    
    88 77 66
    
    15 25 35
    
    
    Expected Output
    1

Pattern

  • Problem Description

    Print the following pattern :

    If N = 1

    1

    If N = 2

    2 2 2
    2 1 2
    2 2 2

    If N = 3

    3 3 3 3 3
    3 2 2 2 3
    3 2 1 2 3
    3 2 2 2 3
    3 3 3 3 3

    and so on.
  • CODING ARENA
  • #include <stdio.h>

    int main(){
        int i, j, n;
        int number_to_print;


        scanf("%d", &n);
        number_to_print = n;

        for(i = 0; i < n; i++){
            //--next_number_to_repeat;
            number_to_print = n;

            for(j =0 ; j< (2*n-1); j++){

                printf("%d ", number_to_print);
                if( j < i   )   
                    --number_to_print;
                else if( j >  ( (n*2)- i -3  ) ) 
                    ++number_to_print;

            }   
            printf("\n");
        }   
    #if 1
        for(i = n-2; i >= 0; i--){
            number_to_print = n;

            for(j =0 ; j < (2*n-1); j++){

                printf("%d ", number_to_print);
                if( j < i   )   
                    --number_to_print;
                else if( j >  ( (n*2)- i -3  ) ) 
                    ++number_to_print;

            }   
            printf("\n");
        }   
            
    #endif
        return 0;

    }
  • Test Case 1

    Input (stdin)
    3
    
    
    Expected Output
    3 3 3 3 3 
    
    3 2 2 2 3 
    
    3 2 1 2 3 
    
    3 2 2 2 3 
    
    3 3 3 3 3
  • Test Case 2

    Input (stdin)
    2
    
    
    Expected Output
    2 2 2 
    
    2 1 2 
    
    2 2 2