Breaking News

TCS Codevita Season 9 2020 Questions (Solved)

String Pair


Problem Description

One person hands over the list of digits to Mr. String, But Mr. String understands only strings. Within strings also he understands only vowels. Mr. String needs your help to find the total number of pairs which add up to a certain digit D.
The rules to calculate digit D are as follow
Take all digits and convert them into their textual representation
Next, sum up the number of vowels i.e. {a, e, i, o, u} from all textual representation
This sum is digit D


Now, once digit D is known find out all unordered pairs of numbers in input whose sum is equal to D. Refer example section for better understanding.

Constraints

1 <= N <= 100
1 <= value of each element in second line of input <= 100
Number 100, if and when it appears in input should be converted to textual representation as hundred and not as one hundred. Hence number of vowels in number 100 should be 2 and not 4

Input

First line contains an integer N which represents number of elements to be processed as input
Second line contains N numbers separated by space

Output

Lower case representation of textual representation of number of pairs in input that sum up to digit D
Note: - (If the count exceeds 100 print "greater 100")

Time Limit

1

Examples

Example 1
Input
5
1 2 3 4 5
Output
one
Explanation
1 -> one -> o, e
2 -> two -> o
3 -> three -> e, e
4 -> four -> o, u
5 -> five - > i, e
Thus, count of vowels in textual representation of numbers in input = {2 + 1 + 2 + 2 + 2} = 9. Number 9 is the digit D referred to in section above.
Now from given list of number {1,2,3,4,5} -> find all pairs that sum up to 9.
Upon processing this we know that only a single unordered pair {4, 5} sum up to 9. Hence the answer is 1. However, output specification requires you to print textual representation of number 1 which is one. Hence output is one.
Note: - Pairs {4, 5} or {5, 4} both sum up to 9. But since we are asking to count only unordered pair, the number of unordered pair is this combination is only one.
Example 2
Input
3
7 4 2
Output
zero
Explanation
7 -> seven -> e, e
4 -> four -> o, u
2 -> two -> o
Thus, count of vowels in textual representation of numbers in input = {2 + 2 + 1} = 5. Number 5 is the digit D referred to in section above.
Since no pairs add up to 5, the answer is 0. Textual representation of 0 is zero. Hence output is zero.

String Pair Solution



table = {0: 'zero', 1: 'one', 2: 'two', 3: 'three', 4: 'four', 5: 'five', 6: 'six', 7: 'seven', 8: 'eight', 9: 'nine', 10: 'ten', 11: 'eleven', 12: 'twelve', 13: 'thirteen', 14: 'fourteen', 15: 'fifteen', 16: 'sixteen', 17: 'seventeen', 18: 'eighteen', 19: 'nineteen', 20: 'twenty', 21: 'twentyone', 22: 'twentytwo', 23: 'twentythree', 24: 'twentyfour', 25: 'twentyfive', 26: 'twentysix', 27: 'twentyseven', 28: 'twentyeight', 29: 'twentynine', 30: 'thirty', 31: 'thirtyone', 32: 'thirtytwo', 33: 'thirtythree', 34: 'thirtyfour', 35: 'thirtyfive', 36: 'thirtysix', 37: 'thirtyseven', 38: 'thirtyeight', 39: 'thirtynine', 40: 'forty', 41: 'fortyone', 42: 'fortytwo', 43: 'fortythree', 44: 'fortyfour', 45: 'fortyfive', 46: 'fortysix', 47: 'fortyseven', 48: 'fortyeight', 49: 'fortynine', 50: 'fifty', 51: 'fiftyone', 52: 'fiftytwo', 53: 'fiftythree', 54: 'fiftyfour', 55: 'fiftyfive', 56: 'fiftysix', 57: 'fiftyseven', 58: 'fiftyeight', 59: 'fiftynine', 60: 'sixty', 61: 'sixtyone', 62: 'sixtytwo', 63: 'sixtythree', 64: 'sixtyfour', 65: 'sixtyfive', 66: 'sixtysix', 67: 'sixtyseven', 68: 'sixtyeight', 69: 'sixtynine', 70: 'seventy', 71: 'seventyone', 72: 'seventytwo', 73: 'seventythree', 74: 'seventyfour', 75: 'seventyfive', 76: 'seventysix', 77: 'seventyseven', 78: 'seventyeight', 79: 'seventynine', 80: 'eighty', 81: 'eightyone', 82: 'eightytwo', 83: 'eightythree', 84: 'eightyfour', 85: 'eightyfive', 86: 'eightysix', 87: 'eightyseven', 88: 'eightyeight', 89: 'eightynine', 90: 'ninety', 91: 'ninetyone', 92: 'ninetytwo', 93: 'ninetythree', 94: 'ninetyfour', 95: 'ninetyfive', 96: 'ninetysix', 97: 'ninetyseven', 98: 'ninetyeight', 99: 'ninetynine', 100: 'hundred'}
vowels = {'a', 'e', 'i', 'o', 'u'}

n = int(input())
ls = list(map(int, input().split()))

d = 0

def wordify(x) -> int:
    if x < 0 or x > 100:
        return
 
    su = 0
    for c in table[x]:
        if c in vowels:
            su += 1
    return su

def pair_sum(d, ls):
    res = []
    while ls:
        num = ls.pop()
        diff = d - num
        if diff in ls:
            res.append([diff, num])
    res.reverse()
    return res

for i in ls:
    d += wordify(i)

# print(d)
print(table[len(pair_sum(d, ls))])





Elections


Problem Description

Elections are going on, and there are two candidates A and B, contesting with each other. There is a queue of voters and in this queue some of them are supporters of A and some of them are supporters of B. Many of them are neutral. The fate of the election will be decided on which side the neutral voters vote. Supporters of A and supporters of B make attempt to win the votes of neutral voters.
The way this can be done is explained below:
1. The voter queue is denoted by three characters, viz {-, A, B}. The - denotes neutral candidate, A denotes supporter of candidate A and B denotes supporter of candidate B.
2. Supporters of A can only move towards the left side of the queue.
3. Supporters of B can only move towards the right side of the queue.
4. Since time is critical, supporters of both A and B will move simultaneously.
5. They both will try and influence the neutral voters by moving in their direction in the queue. If supporter of A reaches the neutral voter before supporter of B reaches him, then that neutral voter will become a supporter of candidate A.
6. Similarly, if supporter of B reaches the neutral voter before supporter of A reaches him, then that neutral voter will become a supporter of candidate B.
7. Finally, if both reach at the same time, the voter will remain neutral. A neutral vote cannot decide the outcome of the election.
8. If finally, the queue has more votes for candidate A, then A wins the election. If B has more votes, then B wins that election. If both have equal votes, then it will be a coalition government.
Refer Examples section for understanding the dynamics of how the supporters influence the neutral voters.
Your task is to find the outcome of the election.
Note: There are no test cases where all votes are neutral.

Constraints

1 <= length of queue <= 10 ^ 5

Input

First line contains an integer which is length of queue of voters.
Second line contains characters {-, A, B}, in which denotes
· A = voter who is supporter of candidate A
· B = voter who is supporter of candidate B
· - = neutral voter

Output

Print candidate with maximum number of votes. If they have equal number of votes, print “Coalition government“.

Time Limit

1

Examples

Example 1
Input
14
--AB--AB---A--
Output
A
Explanation:
For starting positions where there is no opposition from supporter of B, supporter of A can promote in left side of the queue. The voting queue will then look like below:
A A A B - - A B - - - A - -
From 4th place (in voting queue) B supporter is moving towards the right side, simultaneously 7th placed A supporter is also moving towards the left side. Then the voting queue will look like below:
A A A B B A A B - - - A - -
From 8th place B supporter is moving towards the right side, simultaneously 12th placed A supporter is also moving towards the left side. Then the voting queue will look like below:
A A A B B A A B B - A A - -
Since supporters of both A and B will reach the 10th voter at the same time, 10th voter will remain neutral.
Since supporter of A at 12th place cannot move towards right, last 2 voters will not be influenced and remain neutral. Then the voting queue will look like below:
A A A B B A A B B - A A - -
Since all voter have now cast their votes, election results can now be declared.
So final result is: A A A B B A A B B - A A - -
A has 7 votes, B has 4 votes hence, A wins the election.
Example 2
Input
4
A---
Output
A
Explanation:
Since supporter of A at 1st place cannot move towards right, last 3 voters will not be influenced and will remain neutral. Then the voting queue will look like below:
A - - -
Since all voter have now cast their votes, election results can now be declared.
So final result is: A - - -
A has 1 vote, B has 0 votes hence, A wins the election.
Example 3
Input
5
A---B
Output
Coalition government
Explanation:
Since supporter of A at 1st place cannot move towards right, supporter of B at 5th cannot move towards left, middle 3 voters will not be influenced and will remain neutral. Then the voting queue will look like below:
A - - - B
So final result is: A - - - B
A has 1 vote, B has 1 vote hence, output will be “Coalition government“.


Election Solutions


TCS CODE VITA 2020 solution 

#include<iostream>
using namespace std;
int main()
{
 int f;
 cin>>f;
 string s;
 cin >> s;
 int i=0;
 int v1 = 0;
 int v2 = 0;
 int j=0;
 while(s[j]!='\0')
 {
 if(s[j] == 'A')
 v1++;
 else if(s[j] == 'B')
 v2++;
 j++;
 }
 while(s[i]=='-')
 {
 i++;
 }
 if(s[i]=='A')
 {
 v1+=i;
 }
 int start = i;
 for(;i<f;)
 {
 while(s[i]=='-' && i<f)
 {
 i++;
 }
 if(i==f)
 {
 break;
 }
 if(s[i]=='A')
 {
 if(start == i)
 {
 i++;
 continue;
 }
 v1 = v1 + (i-start-1);
 start = i;
 i++;
 continue;
 }
 start = i;
 i++;
 while(s[i]=='-' && i<f)
 {
 i++;
 }
 if(i == f)
 v2 = v2 + (i-start-1);
 else
 {
 if(s[i] =='A')
 {
 v1 = v1 + (i-start-1)/2;
 v2 = v2 + (i-start-1)/2;
 start = i;
 i++;
 }
 else
 {
 v2 = v2 + (i-start-1);
 }
 }

 }
 if(v1 > v2)
 {
 cout << "A" << endl;
 }
 else if(v1 == v2)
 {
 cout << "Coalition government" << endl;
 }
 else
 {
 cout << "B" << endl;
 }
}




Moving Average


Problem Description


A stock price is dynamic. Its value can change multiple times in a fraction of a second or remain unchanged for several minutes. Analyzing the dynamics of stock price change can provide an indication for forth coming uptrend or downtrend in that stock. One such indicator is simple moving averages. Now, Harry wants to analyze the price trend of the stock on the basis of moving averages (MA).
Let's consider a moving average of 2-day and 4-day respectively. A 2-day moving average is calculated by taking average of closing price of 2 consecutive days. A 4-day moving average is calculated by taking average of closing price of 4 consecutive days. Now, according to experts whenever a faster moving average curve (2-day MA) cuts the slower moving average (4-day MA) from below, then it is an indication of uptrend in the stock. Similarly, whenever a faster moving averages curve (2-day MA) cuts the slower moving average curve (4-day MA) from above, then it is an indication of downtrend in the stock.
Help Harry in computing the number of uptrends and downtrends in the given time for which the data is provided.
com.tcs.cv.automata.ei.middleware.DocxToHtmlConverter@278cd5a:image1.jpeg
In this graph, there are three lines indicating stock closing price, moving average of two days and four days .Now we can see that between 13th and 15th there is an intersection. It is known as downtrend when moving average of fewer days is cutting downwards the more days moving average and vice versa.
Note1 - There will be no day1 moving average for 2-day MA. Similarly there will be no day1, day2, day3 moving average for 4-day MA. In general there will be no X-1, X-2, Y-1, Y-2, etc day point for X-day and Y-day moving average curve.
Note2 - All the computation has to be accurate up to 6 digits after the decimal point.

Constraints

1 <= X, Y < 10^4
1 <= N < 10^5

Input

First line contains two space separated integers which are the moving average days X and Y.
Second-line contains an integer N denoting number of stock prices.
Third line contains N space separated decimal values denoting the closing price of the stock for N days.

Output

Print the total number of times the stock will give uptrend or downtrend.

Time Limit

1

Examples

Example 1
Input
3 5
11
4.55 5.4 5.65 5.4 5.2 4.85 4.95 5.05 4.9 4.9 4.95
Output
3
Explanation
Here we have two uptrends and one downtrend.
Example 2
Input
2 4
14
69.849998 72.900002 74.449997 77.300003 75.050003 74.349998 75.449997 76.300003 74 69.349998 65.349998 67.349998 67.599998 68.449997
Output
4
Explanation
This example is depicted in the image above. Here we have two uptrends (17th and 24th) and two downtrends (15th and 20th).





Jogging Ground


Problem Description

There are 4 circular grounds of equal sizes. Their circumferences do not intersect. The radius and the distance of the center of each circle from the leftmost circle's center are given.
There are 4 joggers who can start at the same time from any of the points designated as { a, b, c, d } on the circumference of all the four circles as shown in the diagram below. All 4 joggers jog in different grounds along the circumference of that ground. They could jog in either clockwise (left to right) or anticlockwise (right to left) direction. Finally they may also jog at different speeds.
Given starting position, direction of jogging and speed of jogging of all the 4 joggers, find the summation of length of 3 segments between the four joggers at a given point in time since the start of the jog.
com.tcs.cv.automata.ei.middleware.DocxToHtmlConverter@369beeb9:image1.png
Note: All the computation has to be accurate up to 6 digits after the decimal point.

Constraints

1 <= N < 10^9

Input

First line contains 4 integers each denoting the following
  • R denotes the radius of all four circles
  • D1 denotes the distance centre of the second circle from left to the centre of the leftmost circle
  • D2 denotes the distance centre of the third circle from left to the centre of the leftmost circle
  • D3 denotes the distance centre of the last circle from left to the centre of the leftmost circle
Second line contains 4 space separated integers denoting the angle with point a of each of the 4 circles where 0 degree indicates point a itself, 90 degree indicates point b, 180 degree indicates point c and 270 degree indicates point d.
Third line contains 4 space separated integers denoting the velocity in degrees per second.
Fourth line contains 4 space separated integers denoting the direction of running for joggers (0=clockwise and 1=anticlockwise).
Fifth Line contains integer N denoting the time in seconds since the start of the jog.

Output

Print the summation of length of 3 segments between the four joggers after N seconds, rounded to the nearest integer.

Time Limit

1

Examples

Example 1
Input
10 25 50 75
0 0 0 0
1 1 1 1
1 1 1 1
90
Output
75
Explanation
Here every jogger is starting from point a and all have speed of 1 degree per second. So they will be at 90 degree after 90 seconds. After connecting these points we get segment lengths as (25 +25 +25) = 75
Example 2
Input
10 25 50 75
0 0 0 0
1 2 3 4
0 0 0 0
90
Output


91
Explanation
Here every jogger is starting from point a. They are jogging at the speed of 1, 2, 3 and 4 degrees per second respectively in clockwise direction. Hence after 90 seconds they will reach points where the segment length between them is (18.027800+36.400500+36.400500) = 90.8288. Hence, output is 91


Zoo Design


Problem Description

Aman is a rich businessman who want to build a zoo. He wants to make enclosures for terrestrial and aquatic animals. Terrestrial animals will be of two types, namely herbivorous and carnivorous animals. So there will be three different enclosures.
Herbivores like Elephant, Deer are prime attractions. Similarly, Lion and Tiger are prime attractions amongst carnivores. Finally, Dolphins and Shark are prime attractions amongst aquatics for tourists.
Aman being a savvy businessman realizes that in order to minimize the cost of building the zoo without compromising on the attractions, he has to decide how much area to allocate to each animal type. Each animal type requires a certain area to thrive in. This in turn impacts the area allocation, which in turn has cost implications.
Your task is to help Aman workout the mathematics such that the zoo building cost is minimized subject to the following constraints:
  • Zoo needs to have minimum of X herbivores, Y carnivores and Z aquatic animals
  • Different types of animals will need different minimum area to thrive in
  • For animals of a given type, the minimum area required is the same
  • There is also a maximum limit for the overall area allocated for each animal type
  • Cost of fencing etc. is included in cost of enclosure
  • Exclude the essentials like pathways for tourists, from area and cost calculations
  • Consider all areas in square meters and cost in Rupees. 

Constraints

0 < number of animals of each type <= 20
0 < max area for each animal type <= 500
0 < total area of land on which zoo is to be built <= 1000
0 < min area required by individual animals <= 15
0 < price of each type of enclosure <= 10000
Area of each type of enclosure should be rounded off to the nearest integer

Input

First line contains three space separated integers denoting the cost per square meter of building the enclosure for each type of animals viz. herbivorous, carnivorous and aquatic respectively
Second line contains three space separated integers denoting the maximum area that can be allocated to each type of animal viz. herbivorous, carnivorous and aquatic respectively
Next three lines, each will contain two space separated integers M and N, for each type of animal viz. herbivorous, carnivorous and aquatic respectively, where M denotes minimum number of animals of that type and N denotes minimum area required for that animal type
Last line contains an integer which represents the total area of land on which the zoo needs to be built

Output

Single integer containing the minimum cost required to build the zoo

Time Limit

1

Examples



Example 1
Input
10000 1000 1500
250 250 300
5 5
15 5
10 10
500
Output
837500
Explanation
·The cost of constructing the enclosure for herbivores is high. However, since we need to accommodate 5 herbivores as per given constraints, a 25 sq. meter land will need to allocated for the herbivores.
·Since the cost of constructing the enclosure for carnivores is cheapest we are able to allocate them the maximum limit that we can allocate. Thus we are allocating 250 sq. meters for carnivores.
·The remaining 225 sq. meters can thus be allocated to aquatics without violating any constraint.
·Thus the minimum cost of constructing the zoo adhering to all constraints is (25 * 10000 + 250 * 1000 + 225 * 1500) = 837500
Example 2
Input
100 1000 1500
250 250 300
5 5
15 5
10 10
500
Output
325000
Explanation
·Since the cost of constructing the enclosure for herbivores is cheapest we are able to allocate them the maximum limit that we can allocate. Thus we are allocating 250 sq. meters for herbivores.
·The cost of constructing the enclosure for aquatics is high. However, since we need to accommodate 10 aquatics as per given constraints, a 100 sq. meter land will need to allocated for the aquatic animals.
·The remaining 150 sq. meters can thus be allocated to carnivores without violating any constraint.
·Thus the minimum cost of constructing the zoo adhering to all constraints is (250 * 100 + 150 * 1000 + 100 * 1500) = 325000



Max Sum Solution

TCS Codevita Season 9 2020 solution


x,y=map(int,input().split())
n=int(input())
p=list(map(float,input().split()))
sum_x=0.0
sum_y=0.0
for i in range(x):
    sum_x += p[i]
for i in range(y):
    sum_y += p[i]
ma_x=20*[0]
ma_y=20*[]
ma_x[0]=sum_x/x
ma_y[0]=sum_y/y
j=1
for i in range(x,n):
    j+=1
    sum_x +=(-p[i-x] + p[i])
    ma_x[j] = sum_x/x
j=1
for i in range(y,n):
    j+=1
    sum_y +=(-p[i-y] + p[i])
    ma_y[j] = sum_y/y
    
above=0
count=0
if(ma_x[y-x] > ma_y[0] and above==0): 
    above = 1
if(ma_x[y-x] < ma_y[0]): 
    above = -1
    
for i in range(1,n-y+1):
    if(ma_x[y-x+i] < ma_y[i] and above == 1):
        count += 1
        above = -1
    elif(ma_x[y-x+i] > ma_y[i] and above == -1):
        count += 1
        above = 1
print(count)


Even Odd Solution 

TCS Codevita 2020 solutions

from itertools import product def sum_of_tup(n): sum=0 for i in range(len(n)): sum=sum+int(n[i]) return sum low,high=map(int,input().split()) k=int(input()) lst=[] for i in range(low,high+1): lst.append(str(i)) count=0 perm=product(lst,repeat=k) for i in perm: if (sum_of_tup(i)%2==0): count+=1 print(count%1000000007)



Factor Of 3 Solution

TCS Codevita Season 9 2020 Questions (Solved)


 def distantAdjacentElement(a, n): # dict used to count the frequency # of each element occurring in the # array m = dict() # In this loop we count the frequency # of element through map m for i in range(n): if a[i] in m: m[a[i]] += 1 else: m[a[i]] = 1 # mx store the frequency of element which # occurs most in array . mx = 0 # In this loop we calculate the maximum # frequency and store it in variable mx. for i in range(n): if mx < m[a[i]]: mx = m[a[i]] # By swapping we can adjust array only # when the frequency of the element # which occurs most is less than or # equal to (n + 1)/3. if mx > (n+1) // 3: print("Yes") else: print("No") # Driver Code if name == "main": a = [1,2,3,3] n = len(a) distantAdjacentElement(a, n)



Fill The Cube Solutions


TCS Codevita Season 9 2020 Questions (Solved)


 import math n=int(input()) l=[] c=0 for i in range(n): l.append(list(map(str,input().split()))) for j in range(n): for k in range(n): if l[j][k]=='D': c+=1 print(math.floor(math.sqrt(c)))





Factor Tree Solutions


TCS Codevita Season 9 2020 Questions (Solved)


 for _ in range(int(input())): n=int(input()) l=list(map(int,input().split())) a=[] for i in range(n): a.append(l[i]%3) z=a.count(0) o=a.count(1) t=a.count(2) if z==0 and o!=0 and t!=0: print('NO') elif z==0 and t==0 and o!=0: print('YES') elif z==0 and o==0 and t!=0: print('YES') elif z<=(t+o): print('YES') else: print('NO')




Palindrome 


TCS Codevita Season 9 2020 Questions (Solved)

def is_pallindrome(s): n = len(s) if n==1: return True for i in range(n//2): if s[i] != s[-i-1]: return False return True s = input() def f(s): for a in range(1,len(s)-2): if is_pallindrome(s[:a]): for b in range(a+1,len(s)): if is_pallindrome(s[a:b]) and is_pallindrome(s[b:]): print(s[:a]) print(s[a:b]) print(s[b:]) return print("not possible")






Highway Lane Solution


TCS Codevita Season 9 2020 Questions (Solved)

 #include using namespace std; int main() { int n; cin >> n; int speed[n]; for(int i=0;i> speed[i]; if (n == 0) { printf("%0.6f",float(n)); return 0; } if(n == 1) { printf("%0.6f",float(n)); return 0; } double per = 1,group = 0; for(int i=1; i<=n; i++) { per *= i; for(int j=1,mult =1 ;j<=i; mult *= ++j ) { group += (per/mult); } } if (n%2 == 0) group--; group -=n; printf("%0.6f",group/per); return 0; } 





Corona Virus Solutions


TCS Codevita Season 9 2020 Questions (Solved)



#include #include #include using namespace std; int main() { string fever,cough,fatigue,sneezing,aches,nose,throat,diarea,headache,breath; //declaring the variables cout << "\nCheck for whether you have coronavirus,cold or flu or none of them.: "; //asking the user to input the symptoms cout << "\nDo you have fever:(common/rare/no): "; cin >> fever; cout << "Do you have fatigue:(common/sometimes/NO): "; cin >> fatigue; cout << "Do you have cough:(common/mild/NO): "; cin >> cough; cout << "Do you have sneezing:(common/NO): "; cin >> sneezing; cout << "Do you have aches:(common/sometimes/NO): "; cin >> aches; cout << "Do you have Runny or stuffy nose:(common/rare/sometimes/NO): "; cin >> nose; cout << "Do you have Sore throat:(common/sometimes/NO): "; cin >> throat; cout << "Do you have diarrhea:(rare/sometimes/NO): "; cin >> diarea; cout << "Do you have headaches:(common/rare/sometimes/NO): "; cin >> headache; cout << "Do you have Shortness of breath:(sometimes/NO): "; cin >> breath; if(fever== "COMMON" && fatigue=="SOMETIMES" && cough== "COMMON" && sneezing == "NO" && aches== "SOMETIMES" && nose=="RARE" && throat=="SOMETIMES" && diarea=="RARE" && headache=="SOMETIMES" && breath=="SOMETIMES") { cout << "You have coronavirus!!!"; } else if(fever== "RARE" && fatigue=="SOMETIMES" && cough== "MILD" && sneezing == "COMMON" && aches== "COMMON" && nose=="COMMON" && throat=="COMMON" && diarea=="NO" && headache=="RARE" && breath=="NO") { cout << "You have cold"; } else if(fever== "COMMON" && fatigue=="COMMON" && cough== "COMMON" && sneezing == "NO" && aches== "COMMON" && nose=="SOMETIMES" && throat=="SOMETIMES" && diarea=="SOMETIMES" && headache=="COMMON" && breath=="NO") { cout << "You have Flu"; } else{ cout << "You dont have any of 3 viruses"; } } 





 Particle


TCS Codevita Season 9 2020 Questions (Solved)

#include<bits/stdc++.h>
#include<math.h>
#include<cmath>
using namespace std;
float dist(float x1,float x2,float y1,float y2,float z1,float z2){
 float d=0;
 d=sqrt(pow(x2 - x1, 2) + pow(y2 - y1, 2) + pow(z2 - z1, 2) * 1.0);
 return d;
}
float area(float side1, float side2, float side3 ){
 float s = (side1+side2+side3)/2;
 float are = sqrt(s*(s-side1)*(s-side2)*(s-side3));
 return are;
}
int main(){
 float h,a,b,c,d,va,vb,vc,vd;
 cin>>h>>a>>b>>c>>d>>va>>vb>>vc>>vd;
 char da,db,dc,dd;
 cin>>da>>db>>dc>>dd;
 if(da=='D'){
 va=va*(-1);
 }
 if(db=='D'){
 vb=vb*(-1);
 }
 if(dc=='D'){
 vc=vc*(-1);
 }
 if(dd=='D'){
 vd=vd*(-1);
 }
 float xa=0,ya=h*(-1);
 float xb=h,yb=h*(-1);
 float xc=h,yc=0;
 float xd=0,yd=0;
 float z[100][4];
 for( int i=0;i<100;i++){
 for( int j=0;j<4;j++){
 z[i][j]=0;
 }
 }
 z[0][0]=a;
 z[0][1]=b;
 z[0][2]=c;
 z[0][3]=d;
 for( int i=1;i<100;i++){
 z[i][0]=z[i-1][0]+va;
 z[i][1]=z[i-1][1]+vb;
 z[i][2]=z[i-1][2]+vc;
 z[i][3]=z[i-1][3]+vd;
 if(z[i][0] > h){
 z[i][0]=h;
 }
 if(z[i][0] < 0){
 z[i][0]=0;
 }
 if(z[i][1] > h){
 z[i][1]=h;
 }
 if(z[i][1] < 0){
 z[i][1]=0;
 }
 if(z[i][2] > h){
 z[i][2]=h;
 }
 if(z[i][2] < 0){
 z[i][2]=0;
 }
 if(z[i][3] > h){
 z[i][3]=h;
 }
 if(z[i][3] < 0){
 z[i][3]=0;
 }
 }
 float ab[100];
 for( int i=0;i<100;i++){
 ab[i]=dist(xa,xb,ya,yb,z[i][0],z[i][1]);
 }
 float bc[100];
 for( int i=0;i<100;i++){
 bc[i]=dist(xb,xc,yb,yc,z[i][1],z[i][2]);
 }
 float ac[100];
 for( int i=0;i<100;i++){
 ac[i]=dist(xa,xc,ya,yc,z[i][0],z[i][2]);
 }
 float ad[100];
 for( int i=0;i<100;i++){
 ad[i]=dist(xa,xd,ya,yd,z[i][0],z[i][3]);
 }
 float bd[100];
 for( int i=0;i<100;i++){
 bd[i]=dist(xb,xd,yb,yd,z[i][1],z[i][3]);
 }
 float cd[100];
 for( int i=0;i<100;i++){
 cd[i]=dist(xc,xd,yc,yd,z[i][2],z[i][3]);
 }

 float abc[100];
 for(int i=0;i<100;i++){
 abc[i]=area(ab[i],bc[i],ac[i]);
 }
 float adc[100];
 for(int i=0;i<100;i++){
 adc[i]=area(ad[i],cd[i],ac[i]);
 }
 float abd[100];
 for(int i=0;i<100;i++){
 abd[i]=area(ab[i],ad[i],bd[i]);
 }
 float bcd[100];
 for(int i=0;i<100;i++){
 bcd[i]=area(bc[i],cd[i],bd[i]);
 }

 float maxabc = abc[0];
 for (int i = 0; i < 100; i++){
 if (maxabc < abc[i])
 maxabc = abc[i];
 }
 float minabc = abc[0];
 for (int i = 0; i < 100; i++)
 {
 if (minabc > abc[i])
 minabc = abc[i];
 }
 float maxadc = adc[0];
 for (int i = 0; i < 100; i++)
 {
 if (maxadc < adc[i])
 maxadc = adc[i];
 }
 float minadc = adc[0];
 for (int i = 0; i < 100; i++)
 {
 if (minadc > adc[i])
 minadc = adc[i];
 }
 float ans1=4*pow((maxabc+maxadc),2);
 float ans2=4*pow((minabc+minadc),2);
 cout<<round(ans1)<<" "<<round(ans2)<<endl;
 return 0;

}





Single Lane Highway 


TCS Codevita Season 9 2020 Questions (Solved)

from itertools import permutations import math # def get_count(d): # c=0 # for i in d: # c+=1 # return c n=int(input()) l=list(map(int,input().split())) cc=[] # d1=permutations(l,n-1) # d2=permutations(l,n) # cc.append(get_count(d1)) # cc.append(get_count(d2)) s1=math.factorial(n)//math.factorial(n-(n)) s2=math.factorial(n)//math.factorial(n-(n-1)) cc.append(s1) cc.append(s2) if(n%2==0): t=sum(cc)+2 else: t=sum(cc)-1 print("%.6f"%(t/cc[-1]))




Binary Equivalent 


TCS Codevita Season 9 2020 Questions (Solved)


#include using namespace std; #define ull unsigned long long int #define ll long long int #define loop(i,s,e) for(ll i=(s);i<(e);i++) #define rloop(i,s,e) for(ll i=(s);i>=(e);i--) #define scan(any) for(auto &i:any) cin>>i; #define print(any) for(auto i:any) cout<>n #define input2(a, b) int a,b;cin>>a>>b #define Max(a,b) ((a)>(b)?(a):(b)) #define Min(a,b) ((a)<(b)?(a):(b)) #define rep(i,a,b) for (__typeof((b)) i=(a);i<(b);i++) #define ren(i,a,b) for(__typeof((a)) i=(a);i>=(b);i--) #define mp make_pair #define pb push_back #define fi first #define se second #define vi vector #define pii pair #define piii pair,int> #define all(v) (v).begin(), (v).end() #define sz(x) (int)x.size() #define set(a,n) memset(a,n,sizeof(a)) void calc(int i,vi &v1,int siz,int s,int &tot) { if(i==siz) { if(s==0) tot++; return; } calc(i+1,v1,siz,s+v1[i],tot); calc(i+1,v1,siz,s,tot); } int main() { int n; cin>>n; vi v(n); scan(v); int m=0; for(int i=0;im) m=v[i]; } int count=0; while(m) { count++; m=m>>1; } vi v1(n,0); for(int i=0;i>1; } } int j=0; for(int i=0;i 0) { bin[i] = tot &1; tot = tot>>1; i++; } for (int j = count - 1; j >= 0; j--) cout << bin[j]; return 0; } 



Path Through Graph


TCS Codevita Season 9 2020 Questions (Solved)



#include <bits/stdc++.h>
using namespace std;

#define int long long
#define endl '\n'
int get(int x)
{
 for(int i=2;i*i<=x;i++)
 {
 if(x%i==0)return x/i;
 }
 return 1;
}
void sol()
{
 int x,y;
 cin>>x>>y;
 if(x<y)swap(x,y);
 if(x==y){cout<<0;return;}
 map<int,int> m;

 int c=0;
 while(x!=1)
 {
 c++;
 x=get(x);
 m[x]=c;
 }

 c=0;
 while(!m.count(y))
 {
 c++;
 y=get(y);
 }
 cout<<c+m[y];
}
int32_t main()
{
 ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
 sol();

 return 0;

please share and subscribe Us





No comments