tcs codevita 2016 solutions

CHRISTMAS TREE:

Chirag is a pure Desi boy. And his one and only dream is to meet Santa Claus. He decided to decorate a Christmas tree for Santa on coming Christmas. Chirag made an interesting Christmas tree that grows day by day.

The Christmas tree is comprised of the following
  1. Parts
  2. Stand
Each Part is further comprised of Branches. Branches are comprised of Leaves.

How the tree appears as a function of days should be understood. Basis that print the tree as it appears on the given day. Below are the rules that govern how the tree appears on a given day. Write a program to generate such a Christmas tree whose input is number of days.

Rules:

  1. If tree is one day old you cannot grow. Print a message "You cannot generate christmas tree"
  2. Tree will die after 20 days; it should give a message "Tree is no more"
  3. Tree will have one part less than the number of days.
    E.g.
    On 2nd day tree will have 1 part and one stand.
    On 3rd day tree will have 2 parts and one stand
    On 4th day tree will have 3 parts and one stand and so on.
  4. Top-most part will be the widest and bottom-most part will be the narrowest.
  5. Difference in number of branches between top-most and second from top will be 2
  6. Difference in number of branches between second from top and bottom-most part will be 1
Below is an illustration of how the tree looks like on 4th day



Input Format:

First line of input contains number of days denoted by N
Output Format:

Print Christmas Tree for given N

OR

Print "You cannot generate christmas tree" if N <= 1

OR

Print "Tree is no more" if N > 20
Constraints:
  1. 0<= N <=20

Sample Input and Output


SNo.InputOutput
12






SOLUTION:


#include<stdio.h>

int main()

{
int i,j,k,n,p,q,r,s,t;
scanf("%d",&n);
if(n<=1)
{
printf("You cannot generate christmas tree");
}
else if(n>20)
{
printf("Tree is no more");
}
else
{
for(i=0;i<n+1;i++)
{
for(j=i;j<n;j++)
{
printf(" ");
}
for(k=0;k<=(2*i);k++)
{
printf("*");
}
printf("\n");
}
if(n>2)
{
for(s=0;s<n-1;s++)
{
for(p=s;p<n-1;p++)
{
printf(" ");
}
for(q=0;q<(2*(s+2))-1;q++)
{
printf("*");
}
for(p=s;p<n-1;p++)
{
printf(" ");
}
printf("\n");
}
for(t=0;t<n-3;t++)
{
for(s=0;s<n-2;s++)
{
for(p=s;p<n-1;p++)
{
printf(" ");
}
for(q=0;q<(2*(s+2))-1;q++)
{
printf("*");
}
for(p=s;p<n-1;p++)
{
printf(" ");
}
printf("\n");
}
}
}
for(p=0;p<2;p++)
{
for(q=0;q<n;q++)
{
printf(" ");
}
printf("*");
for(q=0;q<n;q++)
{
printf(" ");
}
printf("\n");
}
}
return 0;
}



MIN PRODUCT ARRAY:

The task is to find the minimum sum of Products of two arrays of the same size, given that k modifications are allowed on the first array. In each modification, one array element of the first array can either be increased or decreased by 2.

Note- the product sum is Summation (A[i]*B[i]) for all i from 1 to n where n is the size of both arrays
Input Format: 
  1. First line of the input contains n and k delimited by whitespace
  2. Second line contains the Array A (modifiable array) with its values delimited by spaces
  3. Third line contains the Array B (non-modifiable array) with its values delimited by spaces

Output Format:

Output the minimum sum of products of the two arrays
Constraints:
  1. 1 ≤ N ≤ 10^5
  2. 0 ≤ |A[i]|, |B[i]| ≤ 10^5
  3. 0 ≤ K ≤ 10^9



Sample Input and Output


SNo.InputOutput
1
3 5
1 2 -3
-2 3 -5

-31
2
5 3
2 3 4 5 4
3 4 2 3 2

25

Explanation for sample 1:

Here total numbers are 3 and total modifications allowed are 5. So we modified A[2], which is -3 and increased it by 10 (as 5 modifications are allowed). Now final sum will be
(1 * -2) + (2 * 3) + (7 * -5)
-2 + 6 - 35
-31

-31 is our final answer.

Explanation for sample 2:

Here total numbers are 5 and total modifications allowed are 3. So we modified A[1], which is 3 and decreased it by 6 (as 3 modifications are allowed).
Now final sum will be
(2 * 3) + (-3 * 4) + (4 * 2) + (5 * 3) + (4 * 2)
6 - 12 + 8 + 15 + 8
25

25 is our final answer. 


SOLUTION:


#include<stdio.h>

int main()

{
unsigned long long int n,k,j,i,m,t,p=0,o=0;
long long int a[100001],b[100001],s[10001],min;
scanf("%llu%llu",&n,&k);
for(i=0;i<n;i++)
{
scanf("%lld",&a[i]);
if((a[i]>0 && a[i]>100000) || (a[i]<0 && a[i]<(-100000))) return 0;
}
for(i=0;i<n;i++)
{
scanf("%lld",&b[i]);
if((b[i]>0 && b[i]>100000) || (b[i]<0 && b[i]<(-100000))) return 0;
}
for(j=0;j<n;j++)
{
a[j] = a[j] + (k*2);
p=0;
for(t=0;t<n;t++)
{
p+=(a[t]*b[t]);
}
s[o] = p;
o++;
a[j]-=(k*2);
}
j=0;
for(m=o;m < 2*n,j<n;m++,j++)
{
a[j] = a[j] - (k*2);
p=0;
for(t=0;t<n;t++)
{
p+=(a[t]*b[t]);
}
s[o] = p;
o++;
a[j]+=(k*2);
}
 min = s[0];
 for(i = 0 ; i < 2*n ; i++)
 {
 if(min > s[i])
 min = s[i];
 }
 printf("%lld",min);
 return 0;
}

Comments

Post a Comment