 TCS Digital is an advanced opportunity offered to the students by TCS. This is a much better position than TCS Ninja, in terms of salary and working experience. For getting qualified for this post you have to clear TCS Digital Advance Coding Section, which is an additional section after TCS NQT exam for getting qualified for Digital TCS Digital is an advanced opportunity offered to the students by TCS. This is a much better position than TCS Ninja, in terms of salary and working experience. For getting qualified for this post you have to clear TCS Digital Advance Coding Section, which is an additional section after TCS NQT exam for getting qualified for Digital

TopicsNo. of Questions
Number of Questions1
Time Limit60 mins
Difficulty levelhigh
Package Offered6.5 LPA – 7 LPA ### Lets Prepare

Question 1

Problem Statement-:  You have M questions and N seconds to complete a test. Each question has some points and takes some time to solve (which will be given as input). Find the maximum mark that can be scored by the student within the given time N.

Sample test case:

• 4 // number of questions
• 10 // Total time to attend the test
• 1 2 // one mark question – 2 seconds to solve.
• 2 3 // two mark question – 3 seconds to solve.
• 3 5 // three mark question – 5 seconds to solve.
• 4 7 // 4 mark question – 7 seconds to solve.
```#include <bits/stdc++.h>
using namespace std;
int MaximumMarks(int marksarr[], int timearr[], int h, int n, int p)
{
int no_of_topics = n + 1;
int total_time = h + 1;

int T[no_of_topics][total_time];
for (int i = 0; i < no_of_topics; i++)
{
T[i] = 0;
}
for (int j = 0; j < total_time; j++)
{
T[j] = 0;
}
for (int i = 1; i < no_of_topics; i++)
{
for (int j = 1; j < total_time; j++)
{
if (j < timearr[i]) { T[i][j] = T[i - 1][j]; } else { T[i][j] = max(marksarr[i] + T[i - 1][j - timearr[i]], T[i - 1][j]); } } } int i = no_of_topics - 1, j = total_time - 1; int sum = 0; while (i > 0 && j > 0)
{
if (T[i][j] == T[i - 1][j])
{
i--;
}
else
{
sum += timearr[i];
j -= timearr[i];
i--;
}
}
int marks = T[no_of_topics - 1][total_time - 1];

if (marks < p)
return -1;
return sum;
}

int main()
{
int n = 4, h = 10, p = 10;
int marksarr[n + 1] = { 0, 6, 4, 2, 8 };
int timearr[n + 1] = { 0, 4, 6, 2, 7 };
cout << MaximumMarks(marksarr, timearr, h, n, p);
return 0;
}
```
```import java.util.*;
public class MaximumTopics
{
static int MaximumMarks(int marksarr[], int timearr[],  int h, int n, int p)
{
int no_of_topics = n + 1;
int total_time = h + 1;
int T[][] = new int[no_of_topics][total_time];
for (int i = 0; i < no_of_topics; i++)
{
T[i] = 0;
}
for (int j = 0; j < total_time; j++)
{
T[j] = 0;
}
for (int i = 1; i < no_of_topics; i++)
{

for (int j = 1; j < total_time; j++)
{
if (j < timearr[i])
{

T[i][j] = T[i - 1][j];
}
else
{
T[i][j] = Math.max(marksarr[i] + T[i - 1][j - timearr[i]], T[i - 1][j]);
}
}
}
int i = no_of_topics - 1, j = total_time - 1;
int sum = 0;
while (i > 0 && j > 0)
{
if (T[i][j] == T[i - 1][j])
{

i--;
}
else
{
sum += timearr[i];
j -= timearr[i];
i--;
}
}
int marks = T[no_of_topics - 1][total_time - 1];
if (marks < p)
return -1;
return sum;
}

public static void main (String[] args)
{
int n = 4, h = 10, p = 10;
int marksarr[] = { 0, 6, 4, 2, 8 };
int timearr[] = { 0, 4, 6, 2, 7 };
System.out.println( MaximumMarks(marksarr, timearr, h, n, p));
}
}

```

Question 2

Problem Statement-:  You have 100 cards, numbered 1 to 100. You distribute them into k piles and collect back the piles in order. For example, if you distribute them into 4 piles, then the first pile will contain the cards numbered 1, 5, 9, … and the 4 th pile will contain the cards numbered 4, 8, 12, …. While collecting back the cards you collect first the last pile, flip it bottom to top, then take the third pile, flip it bottom to top and put the cards on top of the 4th pile and so on. Next round, you distribute the cards into another set of piles and collect in the same manner (last pile first and first pile last).

• If we have 10 cards, and put them into 2 piles, the order of the cards in the piles (top to bottom) would be 9, 7, 5, 3, 1 and 10, 8, 6, 4, 2
• We flip the piles to get the order 1, 3, 5, 7, 9 and 2, 4, 6, 8, 10
• We put the second pile at the bottom and first on top of it to get the deck 1, 3, 5, 7, 9, 2, 4, 6, 8, 10
• Given the number of rounds (m), the number of piles in each round (ki), you need to write a program to find the Nth card from the top at the end of the final round.

Input:

• The input consists of a single line of (m+2) comma-separated integers.
• The first number is m, the number of rounds. The next m numbers are ki which represent the number of piles in each round.
• The last number in the input is N, the position in the final pile whose value is to be determined.

Output: One integer representing the Nth card after all rounds have been played.

Constraints: Number of rounds <= 10, number of piles in each round <= 13.

Example 1

Input: 2, 2, 2, 4

Output: 13

Explanation:

• m = 2, k1 = 2, k2 = 2 and N = 4.
• We have two rounds. The first round has two piles. At the end of the round, the deck is in the following order: 1, 3, 5, …, 99, 2, 4, 6, …, 100
• The next round also has 2 piles and after the second round, the cards are in the order 1, 5, 9, 13, ….
• The fourth card from the top has number 13.

Example 2

Input: 3, 2, 2, 3, 2

Output: 13

Explanation:

• m = 3, k1 = 2, k2 = 2, k3 = 3 and N = 2.
• After the second round, the cards are in the order 1, 5, 9, 13, …
• The third round has 3 piles. Thus after this round the cards will be in the order 1, 13, …. the Second card is 13
`#include<stdio.h>int  main ()  {    int m;    scanf ("%d", &m);    int pile[m];    for (int i = 0; i < m; i++)    {        scanf ("%d", &pile[i]);                }     int N;    scanf ("%d", &N);    int numbers;    int num = 1;    for (int i = 0; i < 100; i++)    {        numbers[i] = num++;    }     int arr;    int count = 0;    while (count < m)    {        int r = pile[count];        int c = 100 / pile[count];        if (pile[count] % 2 != 0)        {            c = c + 1;        }        int num = 0;        int x = 0;        int flag = 0;        for (int i = 0; i < r; i++)        {            num = x;            for (int j = 0; j < c; j++)            {                if (j == 0)                {                    arr[i][j] = numbers[num];                    num = num + pile[count];                }                else                {                    int val = numbers[num];                    num = num + pile[count];                    if (val > 100)                    {                        break;                        }                        else                        {                            arr[i][j] = val;                        }                    }                }            x++;        }    int index = 0;    for (int i = 0; i < r; i++)    {      for (int j = 0; j < c; j++)         {          if (arr[i][j] != 0)            {              numbers[index++] = arr[i][j];            }        }    }    count++;}    printf ("%d", numbers[N - 1]);}`
`#include <iostream>using namespace std;int  main ()  {    int m;    cin>> m;    int pile[m];    for (int i = 0; i < m; i++)    {        cin>> pile[i];                }     int N;    cin>>N;    int numbers;    int num = 1;    for (int i = 0; i < 100; i++)    {        numbers[i] = num++;    }     int arr;    int count = 0;    while (count < m)    {        int r = pile[count];        int c = 100 / pile[count];        if (pile[count] % 2 != 0)        {            c = c + 1;        }        int num = 0;        int x = 0;        int flag = 0;        for (int i = 0; i < r; i++)        {            num = x;            for (int j = 0; j < c; j++)            {                if (j == 0)                {                    arr[i][j] = numbers[num];                    num = num + pile[count];                }                else                {                    int val = numbers[num];                    num = num + pile[count];                    if (val > 100)                    {                        break;                        }                        else                        {                            arr[i][j] = val;                        }                    }                }            x++;        }    int index = 0;    for (int i = 0; i < r; i++)    {      for (int j = 0; j < c; j++)         {          if (arr[i][j] != 0)            {              numbers[index++] = arr[i][j];            }        }    }    count++;}    cout<< (numbers[N - 1]);}`

Question 1

Problem Statement-: Given a square matrix, calculate the absolute difference between the sums of its diagonals.
1 2 3
4 5 6
9 8 9

The left-to-right diagonal = 1+5+9 = 15, The right to left diagonal = 3+5+9 = 17, Their absolute difference is |15-17| = 2

Function Description:

diagonalDifference takes the following parameter:

• int arr[n][m]: an array of integers

Input Format:

• The first line contains a single integer, n the number of rows and columns in the square matrix arr Each of the next n lines describes a row, arr[i] and consists of n space-separated integers arr[i][j]

Output Format:

• Return the absolute difference between the sums of the matrix’s two diagonals as a single integer.

Sample Input:

3
11 2 4
4 5 6
10 8 -12

Sample Output:

15

```import java.util.Scanner;
public class Main
{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int arr[][] = new int [n][n];
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
arr[i][j] = sc.nextInt();
}
}
int sum1 = 0, sum2 = 0;
for(int i=0;i<n;i++){
sum1+=arr[i][i];
sum2+=arr[i][n-i-1];
}
int ans = Math.abs(sum1-sum2);
System.out.println(ans);
}
}```
`def diagonalDifference(arr):	sum1 = 0	sum2 = 0	for i in range(len(arr)):    	sum1+=arr[i][i]    	sum2+=arr[i][n-i-1]	return abs(sum1-sum2)n = int(input())arr = []for _ in range(n):    	arr.append(list(map(int, input().rstrip().split())))result = diagonalDifference(arr)`
```#include <iostream>;

using namespace std;

int main(){
int n,abc,sum=0,pro=0,a;
cin >> n;
for(int i = 0;i < n;i++)
{
for(int j = 0;j < n;j++)
{
cin >> a[i][j];
}
}
for(int i = 0;i < n;i++)
{
for(int j = 0;j < n;j++)
{
if(i==j)
{
sum+=a[i][j];

}

}
}
for(int i = 0;i < n;i++){
for(int j = 0;j < n;j++){
if(i==(n-j-1))
{ pro+=a[i][j];}

}
}
abc=sum-pro;
if(abc<0){
abc= abc*(-1);
cout<<abc;
}
else
cout<<abc;
return 0;
}```
`#include<stdio.h>#include<stdlib.h>int main(){    int n, i, j, sum1 = 0, sum2 = 0, result;    scanf("%d",&n);    int array[n][n];    for(i=0; i<n; i++)    {       for(j=0; j<n; j++)        {            scanf("%d",&array[i][j]);        }    }    for(i=0; i<n; i++)    {       sum1 = sum1 + array[i][i];        sum2 = sum2 + array[i][n-i-1];    }    result = abs(sum1 - sum2);    printf("%d",result);    return 0;}`

Question 2

Problem Statement:- Jaya invented a Time Machine and wants to test it by time-traveling to visit Russia on the Day of Programmer (the 256thday of the year) during a year in the inclusive range from 1700 to 2700. From 1700 to 1917 , Russia’s official calendar was the Julian Calendar since 1919 they used the Gregorian calendar system. The transition from the Julian to Gregorian calendar system occurred in 1918 , when the next day after 31 January was February 14 . This means that in 1918, February 14 was the 32nd day of the year in Russia. In both calendar systems, February is the only month with a variable amount of days; it has 29 days during a leap year, and 28 days during all other years. In the Julian calendar, leap years are divisible by 4 ; in the Gregorian calendar, leap years are either of the following:

• Divisible by 400
• Divisible by 4 and not divisible by 100

Given a year, y, find the date of the 256th day of that year according to the official Russian calendar during that year. Then print it in the format dd.mm.yyyy, where dd is the two-digit day, mm is the two-digit month, and yyyy is y.

For example, the given year is 1984.1984 is divisible by 4, so it is a leap year. The 256 day of a leap year after 1918 is September 12, so the answer is 12.9.1984.

Function Description

• Complete the programmerday function in the editor below. It should return a string representing the date of the 256th day of the year given.
• programmerday has the following parameter(s):
• year: an integer

Input Format

• A single integer denoting year y.

Output Format

• Print the full date of programmerday during year y in the format dd.mm.yyyy, where dd is the two-digit day, mm is the two-digit month, and yyyy is y.

Sample Input

2017

Sample Output

13.09.2017

```import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int year = sc.nextInt();
if(year>=1700 && year<=1917){
if(year%4==0){
System.out.println("12.09."+year);
}
else{
System.out.println("13.09."+year);
}

}
else if(year==1918){
System.out.println("26.09."+year);
}
else{
if(( year%400 == 0) | (( year%4 == 0 ) && ( year%100 != 0))){
System.out.println("12.09."+year);
}
else{
System.out.println("13.09."+year);
}
}
}
}```
```def dayOfProgrammer(year):
if year>=1700 and year<=1917:
if year%4==0:
return "12.09."+str(year)
else:
return "13.09."+str(year)
elif year==1918:
return "26.09."+str(year)
else:
a=not(year%100)
if (( year%400 == 0)or (( year%4 == 0 ) and ( year%100 != 0))):
return "12.09."+str(year)
else:
return "13.09."+str(year)
year = int(input().strip())
result = dayOfProgrammer(year)
print(result)```
`Output201713.09.2017`
```#include<;iostream>
#include <math.h>;
using namespace std;

int main() {

int n;
cin>>n;
if(n==1918)
cout<<"26.09.1918";
else if(n==1700 || n==1800 || n==1900)
cout<<"12.09."<<n;
else if(n%400==0)
cout<<"12.09."<<n;

else if(n%4==0)
{
if(n%100==0)
cout<<"13.09."<<n;

else
cout<<"12.09."<<n;

}
else
cout<<"13.09."<<n;;
return 0;
}```
`#include<stdio.h>int main(){    int year;    scanf("%d",&year);    if(year>=1700 && year<=1917)    {       if(year%4==0)       {           printf("12.09.%d",year);       }       else       {           printf("13.09.%d",year);       }    }    else if(year==1918)    {        printf("26.09.%d",year);    }    else    {        if((year%400==0)||((year%4==0)&&(year%100!=0)))        {            printf("12.09.%d",year);        }        else        {            printf("13.09.%d",year);        }           }   return 0; }` Question 3

Problem Statement:-  Hobo’s Drawing teacher asks his class to open their books to a page number. Hobo can either start turning pages from the front of the book or from the back of the book. He always turns pages one at a time. When she opens the book, page 1 is always on the right side: When he flips page 1, he sees pages 2 and 3. Each page except the last page will always be printed on both sides. The last page may only be printed on the front, given the length of the book. If the book is n pages long, and he wants to turn to page p, what is the minimum number of pages he will turn? He can start at the beginning or the end of the book. Given n and p, find and print the minimum number of pages Hobo must turn in order to arrive at page p

Function Description

Complete the countpage function in the editor below. It should return the minimum number of pages Hobo must turn.

countpage has the following parameter(s):

• n: the number of pages in the book
• p: the page number to turn to

Input Format

• The first line contains an integer n, the number of pages in the book.
• The second line contains an integer, p, the page that Hobo’s teacher wants her to turn to.

Output Format

• Print an integer denoting the minimum number of pages Hobo must turn to get to page p

Sample Input

6
2

Sample Output

1

```import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int p = sc.nextInt();
System.out.println(Math.min(p/2, n/2 - p/2));
}
}```
```def pageCount(n, p):
return min(p//2, n//2 - p//2)
n = int(input())
p = int(input())
result = pageCount(n, p)
print(result)```
```Output
8
4
2
```
```#include<;iostream>
using namespace std;
int main()
{
int n=0,p=0,min;
cin >> n;
cin >> p;
min=(n/2)-(p/2);
if(min>p/2)
min=p/2;
cout << min;
return 0;
}```
`#include <stdio.h>int main(){    int n, p, min;    scanf("%d",&n);    scanf("%d",&p);    min = (n/2)-(p/2);    if(min>p/2)    {        min = p/2;    }    printf("%d",min);    return 0;}`

Question 4

Problem Statement:-  Chacha is playing an arcade game and wants to climb to the top of the leader board and wants to track her ranking. The game uses Dense Ranking so its leader board works like this:

• The player with the highest score is ranked number 1 on the leader board.
• Players who have equal scores receive the same ranking number, and the next player(s) receive the immediately following ranking number.

For example, the four players on the leader board have high scores of 100, 90, 90, and 80. Those players will have ranks 1, 2, 2, and 3, respectively. If Chacha’s scores are 70, 80 and 105, her rankings after each game are 4th, 3rd and 1st.

Function Description

Write a function climbing. It should return an integer array where each element res[j] represents Chacha’s rank after the j game.

climbing has the following parameter(s):

• scores: an array of integers that represent leaderboard scores
• alice: an array of integers that represent Chacha’s scores

Input Format

• The first line contains an integer n, the number of players on the leaderboard.
• The next line contains n space-separated integers scores[i], the leaderboard scores in decreasing order.
• The next line contains an integer, m, denoting the number games Chacha plays.
• The last line contains m space-separated integers chacha[j], the game scores.

Sample Input

7

100 100 50 40 40 20 10

4

5 25 50 120

Sample Output

6

4

2

1

`def climbing(scores, chacha):    unique_scores = list(reversed(sorted(set(scores))))    i = len(chacha)-1    j = 0    ans = []    while i >= 0:        if j >= len(unique_scores) or unique_scores[j] <= chacha[i]:            ans.append(j+1)            i -= 1        else:            j += 1    return reversed(ans)scores_count = int(input())scores = list(map(int, input().rstrip().split()))chacha_count = int(input())chacha = list(map(int, input().rstrip().split()))result = climbing(scores, chacha)print(result)`
`#include <stdio.h>#include <stdlib.h>int main() {    int n,i,j,t,scores_i,k;    scanf("%i", &n);    int *scores = malloc(sizeof(int) * n);    scanf("%i",&scores);    for (scores_i = 1,k=1; k < n;k++ )    {       scanf("%i",&t);       if(t !=scores[scores_i-1])       {           scores[scores_i]=t;            scores_i++;       }     }    n=scores_i;    int m,rank;    j=n-1;    scanf("%i", &m);    int *alice = malloc(sizeof(int) * m);    for (int alice_i = 0; alice_i < m; alice_i++) {       scanf("%i",&alice[alice_i]);    }    for(i=0;i<m;i++)    {        while(j>=0 && alice[i]>scores[j])            j--;        if(j==-1)            rank=1;        else if(alice[i] == scores[j])            rank=j+1;        else if(alice[i] < scores[j])            rank=j+2;        printf("%d\n",rank);    }    return 0;}` Question 5

Problem Statement:-   You will be given an array of integers and a target value. Determine the number of pairs of array elements that have a difference equal to a target value.

For example, given an array of [1, 2, 3, 4] and a target value of 1, we have three values meeting the condition: 2-1 = 1, 3-2 = 1, and 4-3 = 1.

Function Description

Write a function pairs. It must return an integer representing the number of element pairs having the required difference.

pairs has the following parameter(s):

• k: an integer, the target difference
• arr: an array of integers

Input Format

• The first line contains two space-separated integers n and k, the size of arr and the target value.
• The second line contains n space-separated integers of the array arr.

Sample Input

5 2

1 5 3 4 2

Sample Output

2

```import java.util.*;
public class Main
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int k = sc.nextInt();
int arr[] = new int [n];
for(int i=0;i<n;i++){
arr[i] = sc.nextInt();
}
int counter = 0;
Set<Integer> value = new HashSet<Integer>();
for(int i : arr){
}
for(int i : value){
if(value.contains(i + k)){
++counter;
}
}
System.out.println(counter);
}
}```
`def pairs(k, arr):    q = 0    n = len(arr)    arr = set(arr)    h = len(arr) - n    for i in arr:        if i+k in arr:            q+=1    return q+hnk = input().split()n = int(nk)k = int(nk)arr = list(map(int, input().rstrip().split()))result = pairs(k, arr)print(result)`
`#include <stdio.h> int countPairsWithDiffK(int arr[], int n, int k) {     int count = 0;     for (int i = 0; i < n; i++)     {                for (int j = i+1; j < n; j++)         {            if (arr[i] - arr[j] == k || arr[j] - arr[i] == k )             {                count++;            }        }     }     return count; } int main() {     int arr[] =  {1, 5, 3, 4, 2};     int n = sizeof(arr)/sizeof(arr);     int k = 3, result;    result = countPairsWithDiffK(arr, n, k);     printf("%d",result);    return 0; } `

Question 6

Problem Statement:- A jail has a number of prisoners and a number of treats to pass out to them. Their jailer decides the fairest way to divide the treats is to seat the prisoners around a circular table in sequentially numbered chairs. A chair number will be drawn from a hat. Beginning with the prisoner in that chair, one candy will be handed to each prisoner sequentially around the table until all have been distributed.

The jailer is playing a little joke, though. The last piece of candy looks like all the others, but it tastes awful. Determine the chair number occupied by the prisoner who will receive that candy.

For example, there are 4 prisoners and 6 pieces of candy. The prisoners arrange themselves in seats numbered 1 to 4 . Let’s suppose two are drawn from the hat. Prisoners receive candy at positions 2,3,4,1,2,3. The prisoner to be warned sits in chair number 3

Function Description

Write a function saveThePrisoner. It should return an integer representing the chair number of the prisoner to warn.

saveThePrisoner has the following parameter(s):

• n: an integer, the number of prisoners
• m: an integer, the number of sweets
• s: an integer, the chair number to begin passing out sweets from

Input Format

• The first line contains an integer t, denoting the number of test cases.
• The next t lines each contain 3 space-separated integers:
• – : n the number of prisoners
• – : m the number of sweets
• – : s the chair number to start passing out treats at

Output Format

• For each test case, print the chair number of the prisoner who receives the awful treat on a new line.

Sample Input

2
5 2 1
5 2 2

Sample Output

2
3

```import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int T = scan.nextInt();
int arr[] = new int [T];
int i = 0;
while (T-- > 0) {
int N = scan.nextInt();
int M = scan.nextInt();
int S = scan.nextInt();
int last = ((M - 1) + (S - 1)) % N + 1;
arr[i] = last;
i+=1;
}
scan.close();
for(int z=0;z<arr.length;z++) {
System.out.println(arr[z]);
}
}
}```
`def saveThePrisoner(n, m, s):    if (m + s - 1) % n == 0:        return n    else:        return (m+s-1) % nt = int(input())for t_itr in range(t):    nms = input().split()    n = int(nms)    m = int(nms)    s = int(nms)    result = saveThePrisoner(n, m, s)    print(result)`
`#include <stdio.h>int main(){    int t,j,i,count=0;    long int ncr;    long int result,diff;    scanf("%d",&t);    long int n[t];    long int m[t];    long int s[t];    for(i=0;i<t;i++)    {        scanf("%ld %ld %ld",&n[i],&m[i],&s[i]);    }    for(i=0;i<t;i++)    {        count=(n[i]-s[i])+1;        if(count>=m[i])        {            result=(s[i]+m[i])-1;            printf("%ld\n",result);        }        if(count<m[i])        {            diff=m[i]-count;            while(diff>n[i])            {                diff=diff-n[i];            }            printf("%ld\n",diff);        }    }    return 0;}` • Abhijeet

def saveThePrisoner(a,b,c):
sum=c+(b-1)
final=sum%a
if final==0:
print(a)
else:
print(final)
n=int(input(“Enter the number of Test Cases: “))
for i in range(n):
a,b,c=[int(i) for i in input(“Enter the number of prisoners, sweets and the beginning seat in respective order: “).split()]
saveThePrisoner(a,b,c) 0