Thursday, July 19, 2018

Mock vita 1 (2018)

Triangles

Problem Description

Constraints

N<=50
-89 <= angle for any line <=90

Input Format

The first line of the input consists of a single integer, N.
The next line consists of a series of integers (positive, zero or negative), each corresponding to a separate line, and giving the angle that it makes with the x axis (measured in degrees and in anticlockwise direction).

Output

The output is a single integer giving the number of triangles formed by the lines

Test Case

TestCase 1 8,3 D,C,E,F,G,H C,A,E D,C,B,E A,B TestCase 2 8,3 D,C,E,F,G,H C,A,B,E D,B N/A

Explanation

Example 1
Input
5
20,-20,0,50,50
Output
7
Explanation
There are 5 lines, with angles at 20,-20,0, 50 and 50 degrees with the x axis. The figure looks like this
com.tcs.cv.automata.ei.middleware.DocxToHtmlConverter@3c0911ff:image2.png
There are 7 triangles, those formed by (L1,L2,L3),(L1,L2,L5), (L1,L2,L4), (L1,L3,L4), (L1,L3,L5), (L2,L3,L5), (L2,L3,L4). Hence the output is 7.
Example 2
Input
5
50,-50,50,-50,50
Output
0
Explanation
There are 5 lines with angles 50,-50,50,-50 and 50 degrees. The figure looks like this
com.tcs.cv.automata.ei.middleware.DocxToHtmlConverter@3c0911ff:image3.png
As L1,L3 and L5 are parallel, and L2 and L4 are parallel, no triangles are formed by any set of three lines. Hence, the output is 0.

Program:-
#include<stdio.h>
int main()
{
    int n,a[100],i,k,j,count=0;
   char b[50];
     scanf("%d",&n);
      for(i=0;i<n;i++)
      {
          scanf("%d",&a[i]);
        if(i!=n-1)
           scanf("%c",&b);
      }
     for(i=0;i<n;i++)
     {
         for(j=i+1;j<n;j++)
         {
           for(k=j+1;k<n;k++)
           {
               if(a[i]!=a[j]&&a[i]!=a[k]&&a[k]!=a[j])
                  count++;
             
           }
         }
     }
printf("%d\n",count);
  return 0;
}


Finding Product

Problem Description

You are given a set of N positive integers and two small prime numbers P and Q (not necessarily distinct). You need to write a program to count the number of subsets of the set of N numbers whose product is divisible by PQ (the product of P and Q). Since the number K of such sets can be huge, output K modulo 1009 (the remainder when K is divided by 1009).

Constraints

N <= 300
P,Q <=50
The integers are <= 10000

Input Format

First line three comma separated integers N, P,Q
The next line contains N comma separated integers

Output

One integer giving the number of subsets the product of whose elements is divisible by PQ. Give the result modulo 1009.

Test Case

TestCase 1 8,3 D,C,E,F,G,H C,A,E D,C,B,E A,B TestCase 2 8,3 D,C,E,F,G,H C,A,B,E D,B N/A

Explanation

Example 1
Input
4,5,7
5,49,10,27
Output
6
Explanation
N is 4, P is 5, Q is 7. We need to find subsets of the numbers given so that the product of the elements is divisible by 35 (the product of 5 and 7). These subsets are (5,49),(5,49,10),(5,49,27),(5,49,10,27), (49,10),(49,10,27). There are 6 subsets, and the output is 6.
Example 2
Input
4,11,13
3,7,12,13
Output
0
Explanation
N is 4, P is 11, Q is 13. We need to find subsets of the numbers given so that the product of the elements is divisible by 143 (the product of 11 and 13).As none of the N numbers is divisible by 11 (a prime number), there are no subsets for which the product of the elements is divisible by 143. Hence the output is 0.

 

Program:

#include <stdio.h>
int t;
static int count=0;
void combinationUtil(int arr[], int n, int r,
                     int index, int data[], int i);
void printCombination(int arr[], int n, int r)
{
    int data[r];
    combinationUtil(arr, n, r, 0, data, 0);
}
void combinationUtil(int arr[], int n, int r, int index,
                     int data[], int i)
{
    long int s;
    if (index == r) {
        s=1;
        for (int j = 0; j < r; j++)
        {
            s=s*data[j];
        }
        if(s%t==0)
        count++;
        return;
    }
    if (i >= n)
        return;
    data[index] = arr[i];
    combinationUtil(arr, n, r, index + 1, data, i + 1);
    combinationUtil(arr, n, r, index, data, i + 1);
}
int main()
{
    int arr[100],n,p,q,i;
    char ch;
    scanf("%d",&n);
    scanf("%c",&ch);
    scanf("%d",&p);
    scanf("%c",&ch);
    scanf("%d",&q);
    for(i=0;i<n;i++)
    {
    scanf("%d",&arr[i]);
    if(i<n-1)
    scanf("%c",&ch);
    }
    t=p*q;
    for(i=2;i<=n;i++)
    printCombination(arr, n, i);
    printf("%d ",count);
    return 0;
}

Largest Integer
#include<stdio.h>
int main()
{
    int a[20][20],i,j,m,n,f=0,c=0,r,p,
k;
    char ch;
    scanf("%d%c%d",&m,&ch,&n);
    for(i=0;i<m;i++)
    {
        for(j=0;j<n;j++)
        {
            scanf("%d",&a[i][j]);
            if(j<n-1)
            scanf("%c",&ch);
           
        }
    }
   
    while(f!=1)
    {
        j=n-1;c=-1;
     for(k=m-1;k>=0;k--)
     {
        if(a[k][j]>c)
        {
            c=a[k][j];
            r=k;
          
        }
       
     }
     for(p=j;p>0;p--)
     {
         a[r][p]=a[r][p-1];
     }
     a[r][p]=-1;
     if(c!=-1)
     printf("%d",c);
     else
     break;
    }
}

Code vita Round 1(2015)

Reverse Gear?

 #include <stdio.h>
int main()
{
    int F[20],B[20],T[20],D[20],i,n,j=1,b=0,f=0,t;
    scanf("%d",&n);
    for(i=1;i<=n;i++)
    scanf("%d%d%d%d",&F[i],&B[i],&T[i],&D[i]);
    while(j<=n)
    {
    for(i=1;D[j]>0;i++)
    {
        if(i%2==1)
        {
        D[j]=D[j]-B[j];
        b++;
        }
        else
        {
        D[j]=D[j]+F[j];
        f++;
        }
    }
    b=b*B[j];
    f=f*F[j];
    t=(b+f)*T[j]+(D[j]*T[j]);
    printf("%d ",t);
    b=0;f=0;
    j++;
    }
    return 0;
}

Catch 22?

#include <stdio.h>
int main()
{
    int F[20],B[20],T[20],FD[20],BD[20],i,n,j=1,b=0,f=0,t;
    char ch;
    scanf("%d",&n);
    for(i=1;i<=n;i++)
    scanf("%d %d %d %d %d",&F[i],&B[i],&T[i],&FD[i],&BD[i]);
    while(j<=n)
    {
    if(B[j]==F[j]&&F[j]<FD[j]&&B[j]<BD[j])
    printf("No Ditch\n");
    else
    {
    for(i=1;FD[j]>0&&BD[j]>0;i++)
    {
        if(i%2==0)
        {
        FD[j]=FD[j]+B[j];
        BD[j]=BD[j]-B[j];
        b++;
        }
        else
        {
        FD[j]=FD[j]-F[j];
        BD[j]=BD[j]+F[j];
        f++;
        }
    }
    b=b*B[j];
    f=f*F[j];
    if(BD[j]<0)
    {
    t=(b+f)*T[j]+(BD[j]*T[j]);
    ch='B';
    }
    else
    {
    t=(b+f)*T[j]+(FD[j]*T[j]);
    ch='F';
    }
    printf("%d %c\n",t,ch);
    b=0;f=0;
    }
    j++;
    }
    return 0;
}


Saving for a rainy day?

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
 float m,p,r;
 double x,b;
 int c;
 scanf("%f%f%f",&p,&m,&r);
 b=1200+r*(m-1);
 x=p*m*1200/b;
 c=(int)x;
 printf("%d\n",c);
 getch();
}

           (or)
#include<stdio.h>
int main()
{
    float m,r,r1,x;
    int t,i;
    scanf("%f%d%f",&m,&t,&r1);
    r=r1/12;
    x=m;
    for(i=t;i>0;i--)
    {
        x=(x*100)/(100+r);
        if(i!=1)
            x=x+m;
    }
    printf("%.0f",x);
    return 0;
}

Sheldon Cooper and his beverage paradigm?

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int main()
{
 int i,n,q,l,k,j,*a=NULL;
 scanf("%d",&n);
 a=(int*)calloc(n,sizeof(int));
 for(i=0;i<n;i++)
 {
  scanf("%d",&a[i]);
 }
 scanf("%d",&q);
 for(i=0;i<n;i++)
 {
  if(a[i]>q)
  {
   a[i]=a[n-1];
   n--;
   i--;
  }
 }
 for(i=0;i<n;i++)
 {
        k=a[i];  //selecting 1st element
        for(j=i+1;k+a[j]<q&&j<n;j++)
        {
   k=k+a[j];      //2nd element decided
   for(l=j+1;l<n;l++)
   {
    if(k+a[l]==q)         //checking for 3rd element
    {
     printf("True\n");
     goto end;
    }
   }
  k=k-a[j];
        }
 k=k-a[i];
  }
  printf("False");
      end:getch();
      exit(0);
return 0;
}

Credit and Risk calculator? 

 #include<stdio.h>
int min(int a,int b)
{
    if(a<b)
    return a;
    return b;
}
int main()
{
    float n[10],sv[10],csv[10],r[10],cr[10],sv1[10],r1[10],cv[10],ce[10],ca[10];
    int i,t,flag=0;
    scanf("%d",&t);
    for(i=0;i<t;i++)
    {
        scanf("%f%f%f%f%f",&n[i],&sv[i],&csv[i],&r[i],&cr[i]);
    }
    for(i=0;i<t;i++)
    {
        sv1[i]=sv[i]-csv[i];
        r1[i]=r[i]-cr[i];
        cv[i]=n[i]*(min(sv[i],sv1[i]));
        ce[i]=cv[i]/2;
        ca[i]=ce[i]*((float)min(r1[i],r[i])/100);
    }
for(i=0;i<t;i++)
{
    if(csv[i]<0)
    {
        if(cr[i]<=0)
        flag=1;
    }
    else
    flag=1;
    if(flag==1&&(sv1[i]>=20&&sv1[i]<=10000)&&(r1[i]>=0.01&&r1[i]<=99.99)&&(n[i]>=20000&&n[i]<=10000000)&&(sv[i]>=20.00&&sv[i]<=10000.00)&&(-2000.00<=csv[i]&&csv[i]<=2000.00)&&(0.01<=r[i]&&r[i]<=99.99)&&(-10.00<=cr[i]&&cr[i]<=10.00))
    printf("%.2f\n%.2f\n%.2f\n%.2f\n%.2f\n",sv1[i],r1[i],cv[i],ce[i],ca[i]);
    else
    printf("Invalid input\n");
    flag=0;
}
return 0;
}

Code Vita Round 1(2016)

Min product array?

 #include <stdio.h>
#include<stdlib.h>
int main()
{
int maxDiff = 0, minimumSum = 0,product,A[10],B[10],k,n,diff,temp,i;
scanf("%d",&n);
scanf("%d",&k);
for(i=0;i<n;i++)
scanf("%d",&A[i]);
for(i=0;i<n;i++)
scanf("%d",&B[i]);
for(i=0;i<n;i++)
{
product = A[i] * B[i];
if ( product < 0 && B[i] < 0 )
temp = (A[i] +  2  * k ) * B[i];
else if( product < 0 && A[i] < 0)
temp = (A[i] - 2 * k) * B[i];
else if( product > 0 && A[i] < 0)
temp = (A[i] + 2 * k) * B[i];
else if (product > 0 && A[i] > 0)
temp = (A[i] - 2 * k)  * B[i];
diff =  abs(product - temp);
if( diff > maxDiff )
maxDiff = diff;
minimumSum = minimumSum + product;
}

minimumSum = minimumSum - maxDiff;
printf("%d",minimumSum);
return 0;
}

Consecutive prime sum?

 #include<stdio.h>
#include<math.h>
int main()
{
        int n,c,i,j,sum=0,f=-1,k,l;
        scanf("%d",&n);
        for(i=1;i<n;i++)
        {
            c=0;
            for(j=1;j<i;j++)
            {
                if(i%j==0)
                  c++;
            }
            if(c==1)
            {
               k=0;
               sum=sum+i;
               printf(" %d ",sum);
               for(l=1;l<sum;l++)
               {
                   if(sum%l==0)
                     k++;
               }
               if(sum<=n)
               {
                   if(k==1)
                     f++;
                    
               
               }
            
                else
                 break;
                
              //  printf("  %d  ",sum);
            }
        }
        printf("\n%d",f);
       
       
        return 0;

Logic pyramid?

#include<stdio.h>
#include<math.h>
int main()
{
       int n,i,m,j,k;
       scanf("%d",&n);k=2;
       for(i=1;i<=n;i++)
       {
           j=i;
           while(j>0)
           {
             m=(2*(k*k))-k;
             printf("%.5d ",m);
             j--;
           }
           printf("\n");
       }
return 0;
}

Code Vita Round 1(2017)

Chakravyuha?

#include <stdio.h>

int main()
{
    int a[10][10],n,i,j,d=0,c=1;
    scanf("%d",&n);
    for(i=0;i<10;i++)
        for(j=0;j<10;j++)
        {
            if(i>=n || j>=n)
                a[i][j]=-1;
            else
            a[i][j]=0;
        }   
    for(c=1,i=0,j=0;c<=n*n;c++)
    {
        if(a[i][j]==0 && d==0)
        {
            a[i][j]=c;
            j++;
        }
        else if(a[i][j]==0 && d==1)
        {
            a[i][j]=c;
            i++;
        }
        else if(a[i][j]==0 && d==2)
        {
            a[i][j]=c;
            j--;
        }
        else if(a[i][j]==0 && d==3)
        {
            a[i][j]=c;
            i--;
        }
        if(a[i][j]!=0)
        {
            if(d==0)
            {
                i=i+1;
                j=j-1;
                d=1;
            }
            else if(d==2)
            {
                i=i-1;
                j=j+1;
                d=3;
            }
            else if(d==1)
            {
                i=i-1;
                j=j-1;
                d=2;
            }
            else if(d==3)
            {
                i=i+1;
                j=j+1;
                d=0;
            }
        }
    }
    for(i=0;i<n;i++)
    {
        printf("\n");
        for(j=0;j<n;j++)
            printf("%d ",a[i][j]);
    }
    return 0;
}


Spiral

The prime numbers are written in a spiral form staring at (0,0) and moving as shown in the diagram below. The numbers
shown on the right column and the bottom row are the column numbers and row numbers respectively (y and x coordinate
frames).

The objective is to find the position (x and y coordinates) of a given prime.
Input Format:
The input consists of multiple lines.
The first line gives the number of primes (N) in this test case.
The next N lines contain one prime in each line.
Output Format:
The output consists of N lines.
Each consists of a space separated pair of integers giving the x and y coordinates of the corresponding prime in the input.
Constraints:
N≤10
Each prime < 1000000
Example 1
Input
2
3
7
Output
1 0
0 1
Explanation
There are 2 primes in this test case (N=2). The primes are 3 and 7. The coordinates of these in the spiral is (1,0) and (0,1).
The output hence has these in space separated form.
Example 2
Input
3
5
11
13
Output
1 1
1
1
1
0
Explanation
There are 3 primes in this test case (N=2). The primes are 5, 11 and 13. The coordinates of these in the spiral is (1,1), (1,1)
and (1,0).
The output hence has these in space separated form.

Program:

#include<stdio.h>
int prime[100];
int a[9][10],num,search[100];
void create()
{
    int i=4,j=4,n=3,m=5,n1=5,m1=3,k=0,dire=0;
    //creating an array size of 9*10 which is shown in program
    while(i!=-1)
    {
        a[i][j]=prime[k];
        k++;
        if(dire==0)//Right direction set to 0
        {
            j++;
            if(j==m)
            {
            m++;
            dire=1;
            }
        }
        else if(dire==1)//Up direction set to 1
        {
            i--;
            if(i==n)
            {
                n--;
                dire=2;
            }
        }
        else if(dire==2)//Left direction set to 2
        {
            j--;
            if(j==m1)
            {
                m1--;
                dire=3;
            }
        }
        else if(dire==3)//Down direction set to 3
        {
            i++;
            if(i==n1)
            {
                n1++;
                dire=0;
            }
        }
    }
    //Serahing the element postion in the Array
    for(k=0;k<num;k++)
    {
    for(i=0;i<9;i++)
    {
        for(j=0;j<10;j++)
        {
        if(a[i][j]==search[k])
        {
            printf("%d %d\n",j-4,4-i);//printing the position as required
        }
        }
    }
    }
   
}
int main()
{
    int n, i = 3, count, c,j=0;
    n=100;
    //Reading Input
    scanf("%d",&num);
    for(j=0;j<num;j++)
    scanf("%d",&search[j]);
    //calculating 100 prime numbers and storing into array
    j=0;
        if ( n >= 1 )
        {
          prime[j]=2;
          j++;
        }
        for ( count = 2 ; count <= n ;  )
        {
          for ( c = 2 ; c <= i - 1 ; c++ )
          {
             if ( i%c == 0 )
                break;
          }
          if ( c == i )
          {
             prime[j]=i;
             j++;
             count++;
          }
          i++;
        }
        //Calling function to search and create the elements
        create();
return 0;
}