Tcs codevita season V round 2 solutions


VITA SUM:

Tom the cat is brushing up his Math skills. He has a bag containing N balls of different colors. Now Tom can randomly pick any even number of balls from the bag. Tom wants to find out the sum of all such combinations of balls that he can pull out from the bag. He can pull out at max K balls in one pick.
Input Format:

First line contains two space separated numbers N and K
Output Format:

The output is the sum of all the combinations of balls he can pull out modulo 10^9+7 i.e. (1000000007)
Constraints:
  1. 0<=N,k<=10^14
  2. N >= k

Sample Input and Output

SNo.InputOutputExplanation

1

4 4

8

We need 4C0 + 4C24C4= 1+6+1=8 

2

8 3

29

We need 8C0 + 8C2= 1+28=29
SOLUTION:

#include<stdio.h>
unsigned long long int fact(unsigned long long int);
int main()
{
unsigned long long int k,s=0,i,n;
scanf("%llu %llu",&n,&k);
if(n>0 && n>=k && k<=100000000000000)
{
for(i=0;i<=k;i+=2)
{
s+=(fact(n)/(fact(i)*fact(n-i)));
printf("%llu\n",s);
}
s = s % 1000000007;
printf("%llu",s);
}
return 0;
}
unsigned long long int fact(unsigned long long int n)
{
   if(n==1)
       return 1;
       else if(n==0) return 1;
   else
       return(n*fact(n-1));
}


VERIFY JSON OBJECT VALIDITY:

A JSON object is a key-value pair data structure that is enclosed within { }. A sample JSON object would look like
{
"key1":"value1",
"key2":"value2",
"key3": {
"key4":"value4",
"key5":"value5"}
"key6":"value6",
"key7":[
{
"key8":"value8"
}]
}

Given a JSON object, ignore the literal values of the object and check whether the distinguishing characters and notation of the object are valid to determine if the JSON object is valid or not.

Note:
  1. Key3 points to another JSON object (Concept of nesting of JSON objects).
  2. Key7 points to an array of JSON objects.
You may wish to refer site1 to get a more formal description of JSON grammar. site2,site3; are also good resources to understand JSON specifications. 

Input Format:
  1. First line contains a pattern of JSON without any literal

Output Format:

Print 1 if pattern is valid, -1 otherwise.
Constraints:
  1. A JSON object should start with '{' and ends with a '}'.
  2. The key and value should be separated by a ':'.
  3. A ',' suggests an additional JSON property.
  4. An array only consists of JSON objects. It cannot contain a "key":"value" pair by itself.

Example 1:

Input
{:[{},{}]}

Output
1

Explanation
{
"Key": [{
"Key": "Value"
}, {
"Key": "Value"
}]
}
Pattern is following all constraints hence prints 1
Example 2:

Input
{:{[]},{}}

Output
-1

Explanation
Convert this pattern in a JSON Object

{
"Key": {
[
"Key": "Value"
]
},
{
"Key": "Value"
}
}

Constraint 4 "An array only consists of JSON objects. It cannot contain a "key":"value" pair by itself." not followed here, so it's a invalid pattern, hence prints -1

SOLUTION:

#include<stdio.h>
#include<string.h>
int main()
{
char arr[10000];
int i,length,e=0,m=0,l=0;
scanf("%s",arr);
length=strlen(arr);
if(arr[0]!='{' || arr[length-1]!='}')
e++;
for(i=1;i<length-1;i++)
{
if(arr[i]==':' && arr[i+1]=='[' && (m==0))
{
m++;
}
else if(arr[i]==']' && (m==1))
m--;
else if(arr[i]=='{' && (l==0))
l++;
else if(arr[i]=='}' && (l>0))
l--;
else if(arr[i]==',' && arr[i+1] == '{') continue;
else if(arr[i] == '[') continue;
else
e++;
}
if(e!=0 || m!=0 || l!=0)
printf("-1");
else
printf("1");
return 0;
}


Happy Coding!!! :) :P xD


Comments

  1. This comment has been removed by the author.

    ReplyDelete
    Replies
    1. This comment has been removed by the author.

      Delete
  2. solution for vitasum will give tle
    input:-
    100000 100000

    ReplyDelete
  3. Nee Valga. Un Kulam Valga......

    ReplyDelete

Post a Comment