# TCS CodeVita Coding Questions Rounds Venure
Pre – Qualifier Online
Qualifier Online
Grand Finale At the venue

## TCS CodeVita Coding Questions and Answers

TCS CodeVita Season 9 is the ninth-edition of the prestigious coding competition TCS CodeVita which is organized by TCS every year, in the search of world’s  best coder. TCS CodeVita is a global level coding competition, in which participants from all over the world compete for the title of “World’s Best Coder” and a prize money of 20,000 US\$.

## Consecutive Prime Sum

Problem Description

Question – :  Some prime numbers can be expressed as a sum of other consecutive prime numbers.

• For example
• 5 = 2 + 3,
• 17 = 2 + 3 + 5 + 7,
• 41 = 2 + 3 + 5 + 7 + 11 + 13.
Your task is to find out how many prime numbers which satisfy this property are present in the range 3 to N subject to a constraint that summation should always start with number 2.

Write code to find out the number of prime numbers that satisfy the above-mentioned property in a given range.

Input Format: First line contains a number N

Output Format: Print the total number of all such prime numbers which are less than or equal to N.

Constraints: 2<N<=12,000,000,000

```#include  <stdio.h>

int prime(int b);
int main()
{
int i,j,n,cnt,a,c,sum=0,count=0,k=0;
printf("Enter a number : ");
scanf("%d",&n);
for(i=2;i<=n;i++)
{
cnt=1;
for(j=2;j<=n/2;j++)
{
if(i%j==0)
cnt=0;
}
if(cnt==1)
{
a[k]=i;
k++;
}
}
for(i=0;i<k;i++)
{
sum=sum+a[i];
c= prime(sum);
if(c==1)
count++;
}
printf(" %d",count);
return 0;
}

int prime(int b)
{
int j,cnt;
cnt=1;
for(j=2;j<=b/2;j++)
{
if(b%j==0)
cnt=0;
}
if(cnt==0)
return 1;
else
return 0;
}```
```Output
Enter a number : 43
4```
```#include <iostream>
using namespace std;

int prime(int b)
{
int j,cnt;
cnt=1;
for(j=2;j<=b/2;j++)
{
if(b%j==0)
cnt=0;
}
if(cnt==0)
return 1;
else
return 0;
}
int main()
{
int i,j,n,cnt,a,c,sum=0,count=0,k=0;
cout<<"Enter a number : ";
cin>>n;
for(i=2;i<=n;i++)
{
cnt=1;
for(j=2;j<=n/2;j++)
{
if(i%j==0)
cnt=0;
}
if(cnt==1)
{
a[k]=i;
k++;
}
}
for(i=0;i<k;i++)
{
sum=sum+a[i];
c= prime(sum);
if(c==1)
count++;
}
cout<<count;
return 0;
}```
```Output
Enter a number : 5
1```
```import java.util.Scanner;
class Main {

static int prime(int b) {
int j,cnt;
cnt=1;
for (j = 2; j <= b/2; j++) {
if(b%j==0)
cnt=0;
}
if(cnt==0)
return 1;
else
return 0;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int i,j,n=0,cnt,c=0,sum=0,count=0,k=0;
Main t = new Main();
int[] a = new int;
System.out.println("Enter no");
n = sc.nextInt();
for (i = 2; i <=n ; i++) {
cnt=1;
for (j = 2; j <= n/2; j++) {
if(i%j==0)
cnt=0;
}
if(cnt==1) {
a[k]=i;
k++;
}

}
for (i = 0; i < k; i++) {
sum=sum+a[i];
c=t.prime(sum);
if(c==1)
count++;
}
System.out.println(count);
}

}```
```Output
43
4```
```num = int(input())
arr = []
sum = 0
count = 0
if num > 1:
for i in range(2, num + 2):
for j in range(2, i):
if i % j == 0:
break
else:
arr.append(i)
def is_prime(sum):
for i in range(2, (sum // 2) +2):
if sum % i == 0:
return False
else:
return True
for i in range(0, len(arr)):
sum = sum + arr[i]
if sum <= num:
if is_prime(sum):
count = count + 1

print(count)```
```Output
20
2```

## Bank Compare Problem

Problem Description

Question – : There are two banks – Bank A and Bank B. Their interest rates vary. You have received offers from both banks in terms of the annual rate of interest, tenure, and variations of the rate of interest over the entire tenure.You have to choose the offer which costs you least interest and reject the other. Do the computation and make a wise choice.

The loan repayment happens at a monthly frequency and Equated Monthly Installment (EMI) is calculated using the formula given below :

EMI = loanAmount * monthlyInterestRate / ( 1 – 1 / (1 + monthlyInterestRate)^(numberOfYears * 12))

Constraints:

• 1 <= P <= 1000000
• 1 <=T <= 50
• 1<= N1 <= 30
• 1<= N2 <= 30

Input Format:

• First line: P principal (Loan Amount)
• Second line: T Total Tenure (in years).
• Third Line: N1 is the number of slabs of interest rates for a given period by Bank A. First slab starts from the first year and the second slab starts from the end of the first slab and so on.
• Next N1 line will contain the interest rate and their period.
• After N1 lines we will receive N2 viz. the number of slabs offered by the second bank.
• Next N2 lines are the number of slabs of interest rates for a given period by Bank B. The first slab starts from the first year and the second slab starts from the end of the first slab and so on.
• The period and rate will be delimited by single white space.

Output Format: Your decision either Bank A or Bank B.

Explanation:

• Example 1
• Input
• 10000
• 20
• 3
• 5 9.5
• 10 9.6
• 5 8.5
• 3
• 10 6.9
• 5 8.5
• 5 7.9
• Output: Bank B
• Example 2
• Input
• 500000
• 26
• 3
• 13  9.5
• 3  6.9
• 10  5.6
• 3
• 14  8.5
• 6  7.4
• 6  9.6
• Output: Bank A

```#include stdio.h>
#include <math.h>

int main()
{
double p,s,mi,sum,emi,bank,sq;
int y,n,k,i,yrs,l=0;
scanf(" %lf",&p);
scanf(" %d",&y);
for(k=0;k<2;k++)
{
scanf(" %d",&n);
sum=0;
for(i=0;i<n;i++)
{
scanf(" %d",&yrs);
scanf(" %lf",&s);
mi=0;
sq=pow((1+s),yrs*12);
emi= (p*(s))/(1-1/sq);
sum= sum + emi;
}bank[l++]=sum;

}

if(bank<bank)

printf(" Bank A ");

else

printf(" Bank B ");

return 0;

}```
```Output
10000
20
3
5 9.5
10 9.6
5 8.5
3
10 6.9
5 8.5
5 7.9
Bank B```
```#include  <iostream>
#include  <math.h>
using namespace std;
int main()
{
double p,s,mi,sum,emi,bank,sq;
int y,n,k,i,yrs,l=0;

cin>>p;
cin>>y;
for(k=0;k<2;k++) { cin>>n;
sum=0;
for(i=0;i<n;i++) { cin>>yrs;
cin>>s;
mi=0;
sq=pow((1+s),yrs*12);
emi= (p*(s))/(1-1/sq);
sum= sum + emi;
}
bank[l++]=sum;

}

if(bank<bank)

cout<<("Bank A");

else

cout<<("Bank B");

return 0;

}```
```Output
10000
20
3
5 9.5
10 9.6
5 8.5
3
10 6.9
5 8.5
5 7.9
Bank B```
```import java.util.Scanner;
public class Main{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
double p,s,mi,sum,emi,sq;
int y,n,k,yrs,l=0;
double[] bank = new double;
System.out.println("Enter the principal amount");
p = sc.nextDouble();
System.out.println("Enter tenature year");
y = sc.nextInt();
for (k = 0; k < 2; k++) {
System.out.println("Enter the no of slabs");
n = sc.nextInt();
sum=0;
for (int i = 0; i < n; i++) {
System.out.println("Enter the period :");
yrs = sc.nextInt();
System.out.println("Enter the interest :");
s = sc.nextDouble();
mi=0;
sq=Math.pow((1+s), yrs*12);
emi=(p*(s))/(1-1/sq);
sum=sum+emi;
}
bank[l++]=sum;
}
if(bank<bank)
System.out.println("Bank A");
else
System.out.println("Bank B");

}

}```
```Output
10000
20
3
5 9.5
10 9.6
5 8.5
3
10 6.9
5 8.5
5 7.9
Bank B```
```bank = []
principal = int(input())
year = span style="color: #ffcc00;">int(input())
for i in range(0, 2): # 2 Banks
installments = int(input())
sum = 0
for i in range(0, installments):
time, roi = [float(i) for i in input().split()]
square = pow((1+roi), time*12)
emi = (principal*(roi)/(1-1/square))
sum = sum + emi
bank.append(sum)
if bank < bank:
print("Bank A")
else:
print("Bank B")```
```Output
10000
20
3
5 9.5
10 9.6
5 8.5
3
10 6.9
5 8.5
5 7.9
Bank B```

## Counting Rock Sample

Problem Description

Question – : Juan Marquinho is a geologist and he needs to count rock samples in order to send it to a chemical laboratory. He has a problem: The laboratory only accepts rock samples by a range of its size in ppm (parts per million).

Juan Marquinho receives the rock samples one by one and he classifies the rock samples according to the range of the laboratory. This process is very hard because the number of rock samples may be in millions.

Juan Marquinho needs your help, your task is to develop a program to get the number of rocks in each of the ranges accepted by the laboratory.

Input Format: An positive integer S (the number of rock samples) separated by a blank space, and a positive integer R (the number of ranges of the laboratory); A list of the sizes of S samples (in ppm), as positive integers separated by space R lines where the ith line containing two positive integers, space separated, indicating the minimum size and maximum size respectively of the ith range.

Output Format: R lines where the ith line contains a single non-negative integer indicating the number of the samples which lie in the ith range.

Constraints:

• 10 <= S <= 10000
• 1 <= R <= 1000000
• 1<=size of Sample <= 1000

Example 1

• Input: 10 2
• 345 604 321 433 704 470 808 718 517 811
• 300 350
• 400 700

Output: 2 4

Explanation:

There are 10 samples (S) and 2 ranges ( R ). The samples are 345, 604,811. The ranges are 300-350 and 400-700. There are 2 samples in the first range (345 and 321) and 4 samples in the second range (604, 433, 470, 517). Hence the two lines of the output are 2 and 4

Example 2

• Input: 20 3
• 921 107 270 631 926 543 589 520 595 93 873 424 759 537 458 614 725 842 575 195
• 1 100
• 50 600
• 1 1000

Output: 1 12 20

Explanation:

There are 20 samples and 3 ranges. The samples are 921, 107 195. The ranges are 1-100, 50-600 and 1-1000. Note that the ranges are overlapping. The number of samples in each of the three ranges are 1, 12 and 20 respectively. Hence the three lines of the output are 1, 12 and 20.

```#include  <stdio.h>
int main() {
int a,s,i,j,t,l1,l2,c=0;
printf("Enter the number of elements : ");
scanf("%d",&s);
printf("Enter the number of ranges : ");
scanf("%d",&t);
printf("Enter the elements : ");
for(i=0;i<s;i++)
scanf("%d",&a[i]);
printf("Enter the range : ");
for(i=0;i<t;i++)
{
scanf("%d %d",&l1,&l2);
for(j=0;j<s;j++) { if((a[j]>=l1)&&(a[j]<=l2))
c++;
}
printf("The desired output %d ",c);
c=0;
}
return 0;
}```
```Output
10 2
345 604 321 433 704 470 808 718 517 811
300 350
400 700
2 4```
```#include  <iostream>
using namespace std;

int main()
{
int a,s,i,j,t,l1,l2,c=0;
cout<<("Enter the number of elements : "); cin>>s;
cout<<("Enter the number of ranges : "); cin>>t;
cout<<("Enter the elements : ");
for(i=0;i<s;i++) cin>>a[i];
cout<<("Enter the range : ");
for(i=0;i<t;i++) { cin>>l1>>l2;
for(j=0;j<s;j++) { if((a[j]>=l1)&&(a[j]<=l2))
c++;
}
cout<<("The desired output %d ",c);
c=0;
}
return 0;
}```
```Output
Enter the number of elements : 10
Enter the number of ranges : 2
Enter the elements : 345 604 321 433 704 470 808 718 517 811
Enter the ranges : 300 350  400 700
The desired Output : 2 4```
```import java.util.Scanner;
class Main {

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int[] a = new int;
int s,i,j,t,l1=0,l2=0,c=0;

System.out.println("Enter the no of sample");
s = sc.nextInt();

System.out.println("Enter the no of range");
t = sc.nextInt();

System.out.println("Enter the numbers");
for (i = 0; i < s; i++) {
a[i] = sc.nextInt();
}

for (i = 0; i< t; i++) {
System.out.println("Enter the max and min range");
l1 = sc.nextInt();
l2 = sc.nextInt();
for (j = 0; j < s; j++) { if((a[j]>=l1)&&(a[j]<=l2))
c++;
}
System.out.println(c);
c=0;
}

}```
```samples, ranges =[int(i) for i in input().split()]
count = 0
final = []
arr = list(map(int, input().split()))
for i in range(0, ranges):
range1, range2 = [int(i) for i in input().split()]
for j in range(0, samples):
if range1 <= arr[j] <= range2:
count = count + 1
final.append(count)
count = 0
for i in range(0, len(final)):
print(final[i], end=" ")```
```Output
10 2
345 604 321 433 704 470 808 718 517 811
300 350
400 700
2 4```

## kth Largest factor of N

Problem Description

Question -: A positive integer d is said to be a factor of another positive integer N if when N is divided by d, the remainder obtained is zero. For example, for number 12, there are 6 factors 1, 2, 3, 4, 6, 12. Every positive integer k has at least two factors, 1 and the number k itself.Given two positive integers N and k, write a program to print the kth largest factor of N.

Input Format: The input is a comma-separated list of positive integer pairs (N, k).

Output Format: The kth highest factor of N. If N does not have k factors, the output should be 1.

Constraints:

• 1<N<10000000000
• 1<k<600.

You can assume that N will have no prime factors which are larger than 13.

Example 1

• Input: 12,3
• Output: 4

Explanation: N is 12, k is 3. The factors of 12 are (1,2,3,4,6,12). The highest factor is 12 and the third largest factor is 4. The output must be 4.

Example 2

• Input: 30,9
• Output: 1

Explanation: N is 30, k is 9. The factors of 30 are (1,2,3,5,6,10,15,30). There are only 8 factors. As k is more than the number of factors, the output is 1.

```#include  <stdio.h>
void main()
{
int number,pos_of_factor,i,c=0;
scanf("%d",&number);
scanf("%d",&pos_of_factor);
for(i=number;i>=1;i--)
{
if((number%i)==0)
c++;
if(c==pos_of_factor)
{
printf("%d",i);
break;
}
}
if(c<pos_of_factor)
printf("1");
}```
```Output
12,3
4```
```#include <iostream>
using namespace std;
int main()
{
int number,pos_of_factor,i,c=0;
cin>>number;
cin>>pos_of_factor;
for(i=number;i>=1;i--)
{
if((number%i)==0)
c++;
if(c==pos_of_factor)
{
cout<<i;
break;
}
}
if(c<pos_of_factor)
cout<<1;
return 0;
}```
```Output
12,3
4```
```import java.util.Scanner;
public class Main  {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);

int number,r,i,count=0;
number = sc.nextInt();
r = sc.nextInt();

for (i = number; i >= 1; i--)
{
if((number%i)==0)
count++;
if(count==r)
{
System.out.println(i);
break;
}
}
if(count!=r)
System.out.println(1);

}

}```
```Output
12,3
4```
```number, k = [int(i) for i in input().split(",")]
factor = []
count = 0
for i in range(1, number+1):
if number % i == 0:
count = count + 1
factor.append(i)

if count < k:
print("1")
else:
print(factor[k])```
```Output
12,3
4```

## Collecting Candies

Problem Description

Question:- Krishna loves candies a lot, so whenever he gets them, he stores them so that he can eat them later whenever he wants to.

He has recently received N boxes of candies each containing Ci candies where Ci represents the total number of candies in the ith box. Krishna wants to store them in a single box. The only constraint is that he can choose any two boxes and store their joint contents in an empty box only. Assume that there are an infinite number of empty boxes available.

At a time he can pick up any two boxes for transferring and if both the boxes contain X and Y number of candies respectively, then it takes him exactly X+Y seconds of time. As he is too eager to collect all of them he has approached you to tell him the minimum time in which all the candies can be collected.

Input Format:

• The first line of input is the number of test case T
• Each test case is comprised of two inputs
• The first input of a test case is the number of boxes N
• The second input is N integers delimited by whitespace denoting the number of candies in each box

Output Format: Print minimum time required, in seconds, for each of the test cases. Print each output on a new line.

Constraints:

• 1 < T < 10
• 1 < N< 10000
• 1 < [Candies in each box] < 100009
S. No.InputOutput
11
4
1 2 3 4
19
21
5
1 2 3 4
34

Explanation for sample input-output 1:

4 boxes, each containing 1, 2, 3 and 4 candies respectively.Adding 1 + 2 in a new box takes 3 seconds.Adding 3 + 3 in a new box takes 6 seconds.Adding 4 + 6 in a new box takes 10 seconds.Hence total time taken is 19 seconds. There could be other combinations also, but overall time does not go below 19 seconds.

Explanation for sample input-output 2:

5 boxes, each containing 1, 2, 3, 4 and 5 candies respectively.Adding 1 + 2 in a new box takes 3 seconds.Adding 3 + 3 in a new box takes 6 seconds.Adding 4 + 6 in a new box takes 10 seconds.Adding 5 + 10 in a new box takes 15 seconds.Hence total time taken is 34 seconds. There could be other combinations also, but overall time does not go below 33 seconds.

```#include  <stdio.h>
int main()
{
int i,j;
int num_box=0,k=0,sum=0,times=0,tst_case,temp=0;
long c,s;
printf("Enter the number of test case:");
scanf("%d",&tst_case);
printf("Enter the number of boxes:");
for(int l=0;l<tst_case;l++)
{
scanf("%d",&num_box);
}
printf("Enter the number of candies in each box:");
for(i=0;i<num_box;i++)
{
scanf("%ld",&c[i]);
}
for(i=0;i<num_box;i++)
{
for(j=i+1;j<num_box;j++)
{
if(c[i]>c[j])
{
temp=c[i];
c[i]=c[j];
c[j]=temp;
}
}
}
sum=0;
k=0;
for(i=0;i<num_box;i++)
{
sum=sum+c[i];
s[k]=sum;
k++;
}
times=0;
printf("Minimum time requried:");
for(i=1;i<k;i++)
times=times+s[i];

printf("%d\n",times);

return 0;
}```
```Output
1
4
1 2 3 4
19```
```#include <iostream>
using namespace std;

int main()
{
int i,j;
int num_box=0,k=0,sum=0,times=0,tst_case,temp=0;
long c,s;
cout<<("Enter the number of test case:");
cin>>tst_case;
cout<<("Enter the number of boxes:");
for(int l=0;l<tst_case;l++) { cin>>num_box;
}
cout<<("Enter the number of candies in each box:");
for(i=0;i<num_box;i++) { cin>>c[i];
}
for(i=0;i<num_box;i++)
{
for(j=i+1;j<num_box;j++) { if(c[i]>c[j])
{
temp=c[i];
c[i]=c[j];
c[j]=temp;
}
}
}
sum=0;
k=0;
for(i=0;i<num_box;i++)
{
sum=sum+c[i];
s[k]=sum;
k++;
}
times=0;
cout<<("Minimum time requried:");
for(i=1;i<k;i++)
times=times+s[i];

cout<<times;

return 0;
}```
```Output
1
4
1 2 3 4
19```
```import java.util.Scanner;
public class Main {

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n, i, k = 0, sum = 0, s1 = 0, t, temp = 0, j;
long c[] = new long;
long s[] = new long;
System.out.println("Enter a no");
t = sc.nextInt();
for (int l = 0; l < t; l++)
{
n = sc.nextInt();
for (i = 0; i < n; i++)
c[i] = sc.nextLong();
for (i = 0; i < n; i++) {
for (j = i + 1; j < n; j++) { if (c[i] > c[j])
{
temp = (int) c[i];
c[i] = c[j];
c[j] = temp;
}
}
}
sum = 0;
k = 0;
for (i = 0; i < n; i++)
{
sum = (int) (sum + c[i]);
s[k] = sum;
k++;
}
s1 = 0;
for (i = 1; i < k; i++)
s1 = (int) (s1 + s[i]);
System.out.println(s1);
}
}

}```
```Output
1
4
1 2 3 4

19```
```T =  int(input())
arr = []
arr1 = []
for i in range(0, T):
N = int(input())
for i in range(0, N):
arr.append(int(input()))
arr.sort()
count = arr
for i in range(1, len(arr)):
count = count + arr[i]
arr1.append(count)
print(sum(arr1))```
```Output
1
4
1 2 3 4
19```

## Staircase Problem

Problem Description

There are n stairs, a person standing at the bottom wants to reach the top. The person can climb either 1 stair or 2 stairs at a time.

• Count the number of ways, the person can reach the top.
```#include  <stdio.h>
int calc(int n);
int count(int x);

int main ()
{
int n ;
printf("Enter number of stairs : ");
scanf("%d", &n);
printf("Number of ways = %d", count(n));
getchar();
return 0;
}
int count(int x)
{
return calc(x + 1);
}
int calc(int n)
{
if (n <= 1)
return n;
return calc(n-1) + calc(n-2);
}```
```Output
Enter number of stairs : 5
Number of ways = 8```
```#include  <iostream>
using namespace std;

int calc(int n);
int count(int x);

int main ()
{
int n ;
cout<<("Enter number of stairs : ");
cin>>n;
cout<<("Number of ways = %d", count(n));

return 0;
}
int count(int x)
{
return calc(x + 1);
}
int calc(int n)
{
if (n <= 1)
return n;
return calc(n-1) + calc(n-2);
}```
```Output
Enter number of stairs : 5
8```
```import java.util.Scanner;
public class Main {

static int path(int m , int n) {
if(m==1 || n==1)
return 1;

return path(m-1,n)+path(m,n-1);
}
public static void main(String[] args) {

System.out.println(path(4,4));

}

}```
```Output
5
8```
```n = int(input("Enter number of Stairs:"))
def calc(x):
if x <= 1:
return x
return calc(x - 1) + calc(x - 2)

def count(n):
return calc(n + 1)

print("Number of ways:", count(n))```
```Output
5
8```

## Maneuvering a Cave Problem

Problem Description

The task is to count all the possible paths from top left to bottom right of a m x n matrix with the constraints that from each cell you can either move only to right or down.

Input:

• First line consists of T test cases. First line of every test case consists of N and M, denoting the number of rows and number of columns respectively.

Output:

• Single line output i.e count of all the possible paths from top left to bottom right of a m x n matrix..

Constraints:

• 1<=T<=100
• 1<=N<=100
• 1<=M<=100
```#include  <stdio.h>

int calc(int x, int y) ;
int main()
{
int a,b;
printf("Enter the number of rows of the  matrix : ");
scanf("%d",&a);
printf("Enter the number of columns of the matrix : ");
scanf("%d",&b);
printf("%d", calc(a,b));
return 0;
}
int calc(int x, int y)
{
if (x == 1 || y == 1)// If there is a singular matrix
{
return 1;
}
else
{
return calc(x - 1, y) + calc(x, y - 1);
}
}```
```Output
Enter the number of rows of the matrix : 3
Enter the number of columns of the matrix : 3
6```
```#include  <iostream>
using namespace std;
int calc(int x, int y) ;

int main()
{
int a,b,n;
cout<<("Enter the number of test cases");
cin>>n;
while(n!=0)
{
cout<<("Enter the number of rows of the  matrix : ");
cin>>a;
cout<<("Enter the number of columns of the matrix : ");
cin>>b;
cout << "Number of ways - "<< calc(a,b)<<"\n";
n--;
}
return  0;

}
int calc(int x, int y)
{
if (x == 1 || y == 1)// If there is a singular matrix
{
return  1;
}
else
{
return calc(x - 1, y) + calc(x, y - 1);
}

}```
```Output
Enter the number of test cases
2
Enter the number of rows of the matrix : 2
Enter the number of columns of the matrix : 2
Number of ways - 2
Enter the number of rowa of the matrix : 3
Enter the number of columns of the matrix : 3
Number of ways - 6```
```import java.util.Scanner;
public class Main {

static int path(int m , int n) {
if(m==1 || n==1)
return 1;

return path(m-1,n)+path(m,n-1);
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a,b,c;
System.out.println("Enter test cases : ");
c=sc.nextInt();
while(c-- > 0)
{
System.out.println("Enter number of rows and columns : ");
a=sc.nextInt();
b=sc.nextInt();

System.out.println(path(a,b));

}

}
```
```Output
Enter number of test cases :
2
Enter number of rows and columns :
3
3
6
Enter number of rows and columns :
4
2
4```
```a = int(input())
b = int(input())

def calculate(a, b):
if a == 1 or b == 1:
return 1
else:
return calculate(a - 1, b) + calculate(a, b - 1)
print(calculate(a, b))```
```Output
3
3
6```

## Houses Problem

Problem Description

Question:- There are n houses build in a line, each of which contains some value in it.

A thief is going to steal the maximal value of these houses, but he can’t steal in two adjacent houses because the owner of the stolen houses will tell his two neighbours left and right side.

What is the maximum stolen value?

Sample Input: val[] = {6, 7, 1, 3, 8, 2, 5}

Sample Output: 20

```#include <iostream>
using namespace std;
int maxLoot(int *hval, int n) ;
int main()
{
int hval[]= {6, 7, 1, 3, 8, 2, 4, 12};
int n = sizeof(hval)/sizeof(hval);
cout << "Maximum loot possible : "
<< maxLoot(hval, n);
return 0;
}
// calculate the maximum stolen value
int maxLoot(int *hval, int n)
{
if (n == 0)
return 0;
if (n == 1)
return hval;
if (n == 2)
return max(hval, hval);

// dp[i] represent the maximum value stolen
// so far after reaching house i.
int dp[n];

// Initialize the dp and dp
dp = hval;
dp = max(hval, hval);

// Fill remaining positions
for (int i = 2; i<n; i++)
dp[i] = max(hval[i]+dp[i-2], dp[i-1]);

return dp[n-1];
}```
```Output
6 7 1 3 8 2 5
20```
```import java.util.Scanner;
public class Main {

static int prep(int hval[],int n) {
if(n==0)
return 0;
if(n==1)
return hval;
if(n==2)
return Math.max(hval,hval);

int[] dp = new int[n];
dp=hval;
dp=Math.max(hval,hval);

for (int i = 2; i < n; i++) {
dp[i]=Math.max(hval[i]+dp[i-2],dp[i-1]);
}
return dp[n-1];
}

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);

int hval[] = {3,2,4,6,7,1};
int n=hval.length;
System.out.println("Max loot value :"+prep(hval,n));
}

}```
```Output
6 7 1 3 8 2 5
20```
```num = list(map(int, input().split()))
sum = num
for i in range(0, len(num)//2):
sum = sum + int(num[i])
print(sum)
```
```Output
6 7 1 3 8 2 5
20```

## Square Free Numbers Problem

Problem Description

In the theory of numbers, square free numbers have a special place.  A square free number is one that is not divisible by a perfect square (other than 1).  Thus 72 is divisible by 36 (a perfect square), and is not a square free number, but 70 has factors 1, 2, 5, 7, 10, 14, 35 and 70.  As none of these are perfect squares (other than 1), 70 is a square free number.

For some algorithms, it is important to find out the square free numbers that divide a number.  Note that 1 is not considered a square free number.

In this problem, you are asked to write a program to find the number of square free numbers that divide a given number.

#### Input:-

• The only line of the input is a single integer N which is divisible by no prime number larger than 19.

#### Output:-

• One line containing an integer that gives the number of square free numbers (not including 1).

• N   < 10^9

• Simple

• 1

### Examples

#### Example 1

• Input:-
20
• Output:-
3

Explanation

N=20

If we list the numbers that divide 20, they are

1, 2, 4, 5, 10, 20

1 is not a square free number, 4 is a perfect square, and 20 is divisible by 4, a perfect square.  2 and 5, being prime, are square free, and 10 is divisible by 1,2,5 and 10, none of which are perfect squares.  Hence the square free numbers that divide 20 are 2, 5, 10.  Hence the result is 3.

#### Example 2

• Input:-
72
• Output:-
3

Explanation

N=72.  The numbers that divide 72 are

1, 2, 3, 4, 6, 8, 9, 12, 18, 24, 36, 72

1 is not considered square free.   4, 9 and 36 are perfect squares, and 8,12,18,24 and 72 are divisible by one of the.  Hence only 2, 3 and 6 are square free.  (It is easily seen that none of them are divisible by a perfect square).  The result is 3.

```#include <stdio.h>
#include <math.h>
int main()
{
long int n;
double sqnum;
long int i,j=0,flag,chksqr,temp;
int count=0,k;
printf("Enter the number:");
scanf("%ld",&n);

for(i=2;i<=n/2;i++)
{
if(n%i==0)
{
count++;
sqnum=sqrt(i);
chksqr=sqnum;
if(chksqr==sqnum)
{
count--;
temp[j]=i;
j++;
}
else
{
for(k=0;k<j;k++) { if(i>temp[k] && j!=0)
{
if(i%temp[k]==0)
{
count--;
k=j+1;
}
}
else
break;
}
}
}
}

printf("%d",count);
return 0;
}```
```Output
Enter the number : 20
3
```
```#include  <iostream>
#include  <math.h>
using namespace std;
int main()
{
long int n;
double sqnum;
long int i,j=0,flag,chksqr,temp;
int count=0,k;
cout<<"Enter the number:";
cin>>n;

for(i=2;i<=n/2;i++)
{
if(n%i==0)
{
count++;
sqnum=sqrt(i);
chksqr=sqnum;
if(chksqr==sqnum)
{
count--;
temp[j]=i;
j++;
}
else
{
for(k=0;k<j;k++) { if(i>temp[k] && j!=0)
{
if(i%temp[k]==0)
{
count--;
k=j+1;
}
}
else
break;
}
}
}
}

cout<<count;
return 0;
}```
```Output
Enter the number : 20
3
```

### Coin Distribution Problem

Problem Statement

Find the minimum number of coins required to form any value between 1 to N,both inclusive.Cumulative value of coins should not exceed N. Coin denominations are 1 Rupee, 2 Rupee and 5 Rupee.Let’s Understand the problem using the following example. Consider the value of N is 13, then the minimum number of coins required to formulate any value between 1 and 13, is 6. One 5 Rupee, three 2 Rupee and two 1 Rupee coins are required to realize any value between 1 and 13. Hence this is the answer.However, if one takes two 5 Rupee coins, one 2 rupee coin and two 1 rupee coin, then too all values between 1 and 13 are achieved. But since the cumulative value of all coins equals 14, i.e., exceeds 13, this is not the answer.

• Input Format:
• A single integer value.
• Output Format:
• Four space separated integer values.
• 1st – Total number of coins.
• 2nd – number of 5 Rupee coins.
• 3rd – number of 2 Rupee coins.
• 4th – number of 1 Rupee coins.
• Constraints:
• 0 < n < 1000

Refer the sample output for formatting

Sample Input

13

Sample Output

6 1 3 2

Explanation

• The minimum number of coins required is 6 with in it:
• minimum number of 5 Rupee coins = 1
• minimum number of 2 Rupee coins = 3
• minimum number of 1 Rupee coins = 2

Using these coins, we can form any value with in the given value and itself, like below:

Here the given value is 13

• For 1 = one 1 Rupee coin
• For 2 = one 2 Rupee coin
• For 3 = one 1 Rupee coin and one 2 Rupee coins
• For 4 = two 2 Rupee coins
• For 5 = one 5 Rupee coin
• For 6 = one 5 Rupee and one 1 Rupee coins
• For 7 = one 5 Rupee and one 2 Rupee coins
• For 8 = one 5 Rupee, one 2 Rupee and one 1 Rupee coins
• For 9 = one 5 Rupee and two 2 Rupee coins
• For 10 = one 5 Rupee, two 2 Rupee and one 1 Rupee coins
• For 11 = one 5 Rupee, two 2 Rupee and two 1 Rupee coins
• For 12 = one 5 Rupee, three 2 Rupee and one 1 Rupee coins
• For 13 = one 5 Rupee, three 2 Rupee and two 1 Rupee coins
`number = int (input ())  five = int ((number - 4) / 5) if ((number - 5 * five) % 2)== 0:  one = 2  else:  one = 1 two = (number - 5 * five - one)	//2    print (one + two + five, five, two, one)`
```import java.util.*;
public class Main
{
public static void main (String[]args)
{
System.out.println ("Enter the Number");
Scanner s = new Scanner (System.in);
int number = s.nextInt ();
int one = 0, two = 0;
int five = (number - 4) / 5;
if (((number - 5 * five) % 2) == 0)
{
one = 2;
}
else
{
one = 1;
}
two = (number - 5 * five - one) / 2;
System.out.println (one + two + five);
System.out.println (five);
System.out.println (two);
System.out.println (one);

}
}```

### 12 comments on “TCS CodeVita Coding Questions”

• Abhishek

can u give round 2 codevita solution 0
• HelpPrepInsta 0
• srija

can you provide solution to codevita Digitpairs problem and Deathbattle problem. 0
• HelpPrepInsta

may you please post the question here in the comment section, then we’ll surely provide you the code 1
• Srichandra

Death Battle
Problem Description
In a crossover fantasy universe, Houin Kyoma is up in a battle against a powerful monster Nomu that can kill him in a single blow. However being a brilliant scientist Kyoma found a way to pause time for exactly M seconds. Each second, Kyoma attacks Nomu with certain power, which will reduce his health points by that exact power. Initially Nomu has H Health Points. Nomu dies when his Health Points reach 0. Normally Kyoma performs Normal Attack with power A. Besides from Kyoma’s brilliance, luck plays a major role in events of this universe. Kyoma’s Luck L is defined as probability of performing a super attack. A super attack increases power of Normal Attack by C. Given this information calculate and print the probability that Kyoma kills Nomu and survives. If Kyoma dies print “RIP”.
Constraints
0 < T <= 50
1 <= A, H, C, L1, L2 <= 1000
1 <= M <= 20.
L1<=L2
Input Format
First line is integer T denoting number of test cases.
Each test case consist of single line with space separated numbers A H L1 L2 M C. Where luck L is defined as L1/L2. Other numbers are, as described above.
Output
Print probability that Kyoma kills Nomu in form P1/P2 where P1<=P2 and gcd(P1,P2)=1. If impossible, print “RIP” without quotes.
Timeout
1
Explanation
Example 1
Input
2
10 33 7 10 3 2
10 999 7 10 3 2
Output
98/125
RIP 0
• Vaibhav Jain

Thanks Srichandra for providing us this questions. We surely provide the answer for this question, as soon as we could 0
• Srichandra

Digit Pairs
Problem Description
Given N three-digit numbers, your task is to find bit score of all N numbers and then print the number of pairs possible based on these calculated bit score.
1. Rule for calculating bit score from three digit number:
From the 3-digit number,
• extract largest digit and multiply by 11 then
• extract smallest digit multiply by 7 then
• add both the result for getting bit pairs.
Note: – Bit score should be of 2-digits, if above results in a 3-digit bit score, simply ignore most significant digit.
Consider following examples:
Say, number is 286
Largest digit is 8 and smallest digit is 2
So, 8*11+2*7 =102 so ignore most significant bit , So bit score = 02.
Say, Number is 123
Largest digit is 3 and smallest digit is 1
So, 3*11+7*1=40, so bit score is 40.
2. Rules for making pairs from above calculated bit scores
Condition for making pairs are
• Both bit scores should be in either odd position or even position to be eligible to form a pair.
• Pairs can be only made if most significant digit are same and at most two pair can be made for a given significant digit.
Constraints
N<=500
Input Format
First line contains an integer N, denoting the count of numbers.
Second line contains N 3-digit integers delimited by space
Output
One integer value denoting the number of bit pairs.
Timeout
1
Explanation
Example 1
Input
8 234 567 321 345 123 110 767 111
Output
3
Explanation
After getting the most and least significant digits of the numbers and applying the formula given in Rule 1 we get the bit scores of the numbers as:
58 12 40 76 40 11 19 18
No. of pair possible are 3:
40 appears twice at odd-indices 3 and 5 respectively. Hence, this is one pair.
12, 11, 18 are at even-indices. Hence, two pairs are possible from these three-bit scores.
Hence total pairs possible is 3 0
• Vaibhav Jain

Thanks Srichandra for providing us this questions. We surely provide the answer for this question, as soon as we could 0
• Sowndhar

i need balancing star solution 0
• HelpPrepInsta

can you comment the entire question here, so that we can code for that 0
• kajol

In ConsecutivePrimeSum problem output is wrong.
For n= 43,the output should be 3 not 4. Because there are only 5, 17, 41 prime numbers which satisfies the condition 1
• HelpPrepInsta

Ohk Kajol, we’ll surely fix it, thank you for pointing out our silly mistake 1