TCS NQT Coding Practice Questions
1. Sum of Integer Binary Logarithm
Problem: For every integer in the range L to R (inclusive), compute the integer binary
logarithm (largest k such that 2^k <= x). Find the sum.
Input:
LR
Constraints:
1 ≤ L ≤ R ≤ 10^9
Sample Input:
28
Sample Output:
13
Explanation:
2->1,3->1,4->2,5->2,6->2,7->2,8->3; Sum=13.
2. Sum of Absolute Differences
Problem: Given an unsorted array, sort it in ascending order. For every element in the
sorted array, compute the sum of absolute differences with every other element.
Input:
N
Array elements
Constraints:
1 ≤ N ≤ 2*10^5
Sample Input:
4
4321
Sample Output:
6446
Explanation:
Sorted array=[1,2,3,4]. Difference sums are [6,4,4,6].
3. Peak Element in an Array (TCS NQT)
Problem: A peak element is greater than or equal to its adjacent elements. Find the index of
any peak element.
Input:
N
Array elements
Sample Input:
6
1 3 20 4 1 0
Sample Output:
2
Explanation:
20 is a peak element.
4. Merge Two Sorted Arrays (Merge Sort Logic)
Problem: Merge two already sorted arrays into one sorted array using the merge procedure
of Merge Sort.
Input:
NM
Array1
Array2
Sample Input:
54
13579
2468
Sample Output:
123456789
TCS NQT CODING QUESTIONS
TCS NQT for Priority Institutes Batch 2027 IRC
Question 1: Longest Palindromic Substring
Problem Statement:
The organization is eager to introduce an innovative feature on its platform. This feature
would empower users to enter a specific string of text. In response, the system would
identify and return the most extended substring within that text that qualifies as a
palindrome.
A palindromic string is characterized by its ability to read identically from both front to back
and vice versa. The onus now lies on you to develop a computational strategy and program
that can efficiently realize this function.
Example 1:
Input:
babad
Output:
bab
Explanation:
Both "bab" and "aba" are valid palindromic substrings of the given input, but "bab" is
returned in this case
Constraints:
- `1 <= len(s) <= 1000`
- String `s` contains only lowercase English letters.
Input Format:
A single string `s`, which represents the text.
Output Format:
A string representing the longest palindromic substring. If there are multiple answers, return
any.
*Note:* You can compile and test multiple times, but only the last compilation will be
considered.
Question 2: Sophie's Fashion Giveaway with Discount Coupon
Problem Statement:
Meet Sophie, an avid shopper who loves finding deals and bargains at her favorite store.
She's out for a shopping spree, armed with a single discount coupon she received as part of
a loyalty program. In the store, Sophie spots N different fashion items she'd like to add to her
collection. Each item has its price tag `P[i]`, but here's the twist: Sophie can purchase each
item at a discounted price `D[i]` if she uses her coupon.
The store is running a unique campaign where customers are given entries into a fashion
giveaway. Specifically, the number of entries Sophie receives is determined by the *greatest
common divisor (GCD)* of the prices `P` of all the items she buys. For instance, if the prices
of the items in her cart are `[50, 100, 150]`, then Sophie would receive `gcd(50, 100, 150) =
50` entries into the giveaway.
Sophie's primary goal is to maximize her chances of winning the fashion giveaway by
obtaining as many entries as possible. However, she can only use her discount coupon on
*one item*.
Write a program to maximize her chances of winning the fashion giveaway with or without
using discount coupons.
Example 1:
Input:
2
4
12 24 18 36
6 8 12 9
3
10 15 20
5 10 5
Output:
3
10
Explanation:
For the first test case, using the discount coupon on the third item and exchanging the
original price of 18 with a discounted price of 12 is optimal. This makes the price list `[12, 24,
12, 36]` and GCD will be 12. There is no other way to get a larger GCD than 12.
Constraints:
- `1 ≤ T ≤ 1000`
- `1 ≤ N ≤ 10000`
- `1 ≤ D[i] ≤ P[i] ≤ 1000`
Input Format:
- The first input line contains an integer `T`, representing the number of test cases.
- Each test case consists of three lines of input.
- The first line of each test case contains an integer `N`, denoting the number of fashion
items Sophie wants to buy.
- The second line contains `N` space-separated integers representing the initial prices of
the fashion items.
- The third line contains `N` space-separated integers representing the discounted prices
of the fashion items if Sophie uses her coupon.
Output Format:
For each test case, output the maximum number of entries Sophie can obtain in the fashion
giveaway if she strategically uses her discount coupon.
TCS Problem Statements
1)Problem Statement
You are given an array of integers representing Product IDs.
Your task is to:
• Find the product ID that appears most frequently in the array.
• If multiple product IDs have the same highest frequency, return the smallest
product ID among them.
Input Format
• Integer N → number of elements in the array
• Array of N integers → product IDs
Output Format
• Print a single integer → product ID with the highest frequency
• If there is a tie, print the smallest product ID
Example
Input
1223341
Output
1
Explanation:
The frequencies are:
• 1 → 2 times
• 2 → 2 times
• 3 → 2 times
• 4 → 1 time
The highest frequency is 2, shared by 1, 2, and 3. Since there is a tie, the smallest product
ID, which is 1, is printed.
TCS Problem Statements
2)Question: Minimum Cost to Connect Servers
Problem Statement
You are given:
• N → number of servers
• An array installCost[] → cost to install each server
• A 2D matrix connectCost[][] → cost to connect any two servers
Your task is to:
• Connect all servers such that the total cost is minimized.
Cost includes:
1. Cost to install servers
2. Cost to connect servers
You may choose:
• To install servers individually, or
• Connect servers in a way that minimizes overall cost.
Input Format
• Integer N → number of servers
• Array of size N → installation cost of each server
• N × N matrix → connection cost between servers
Output Format
• Print a single integer → minimum total cost
Example
Input
3
546
013
102
TCS Problem Statements
320
Output
Explanation
Instead of installing all servers individually:
• Installation cost = 5 + 4 + 6 = 15
A better approach is:
• Install the cheapest server (cost = 4)
• Connect the remaining servers using the minimum-cost connections:
o 1→2=2
o 0→1=1
Total Cost = 4 + 2 + 1 = 6, which is the minimum possible.
3) Problem Statement
Given a sorted array and a key, perform Binary Search.
• If the key is found, print the key.
• Otherwise, print the element whose value is closest (minimum absolute difference)
to the key.
• If two elements are equally close, print the smaller element.
Input Format
• First line contains an integer N.
• Second line contains N sorted integers.
• Third line contains the integer key.
Output Format
TCS Problem Statements
• If the key exists, print the key.
• Otherwise, print the nearest element.
Constraints
• 1 ≤ N ≤ 10⁵
Example 1
Input
2 5 8 12 16
8
Output
8
Example 2
Input
2 5 8 12 16
10
Output
Explanation
• The key 10 is not present in the array.
• The closest elements are 8 and 12.
• Both are at the same distance (2) from 10.
• Since there is a tie, print the smaller element, which is 8.
TCS Problem Statements
4) Problem Statement
Given a positive integer N, print all of its prime factors in increasing order.
If a prime factor occurs multiple times, print it multiple times.
Input Format
• A single integer N.
Output Format
Print the prime factors of N separated by spaces.
Constraints
• 2 ≤ N ≤ 10⁹
Example 1
Input
60
Output
2235
Explanation
60 = 2 × 2 × 3 × 5
Example 2
Input
84
Output
2237
Explanation
84 = 2 × 2 × 3 × 7
TCS Problem Statements
5) Amazon Kth Best-Selling Product
Amazon is preparing for its annual shopping festival and wants to identify its top-
performing products.
Given the sales count of different products, find the Kth best-selling product.
The Kth best-selling product is the product whose sales rank is exactly K when all products
are sorted in descending order of sales.
To optimize memory and performance for large datasets, the solution must use a Min Heap
of size K.
Input Format
• First line contains two integers N and K.
• Second line contains N integers representing the sales count of each product.
Output Format
Print the Kth best-selling product's sales count.
Constraints
• 1 ≤ 𝐾 ≤ 𝑁 ≤ 105
• Sales count is a positive integer.
Example 1
Input
63
50 20 70 40 90 60
Output
60
Explanation
Sorted in descending order:
90 70 60 50 40 20
The 3rd best-selling product has 60 sales.
TCS Problem Statements
6) Time Conversion (12-Hour to 24-Hour Format)
Given a time in 12-hour AM/PM format, convert it to 24-hour (military) format.
Note
• 12:00:00 AM becomes 00:00:00
• 12:00:00 PM remains 12:00:00
Function Description
Complete the timeConversion function.
The function should return the given time in 24-hour format.
Function Signature
string timeConversion(string s)
Input Format
A single string s representing time in 12-hour format.
Format:
hh:mm:ssAM
or
hh:mm:ssPM
Output Format
Return the equivalent time in 24-hour format.
Constraints
• All input times are valid.
Sample Input 1
07:05:45PM
Sample Output 1
19:05:45
TCS Problem Statements
Explanation
• Hour = 7
• Since it is PM, add 12.
Approach
There are four cases:
Input Output
AM and hour = 12 Change hour to 00
AM and hour ≠ 12 Keep hour unchanged
PM and hour = 12 Keep hour unchanged
PM and hour ≠ 12 Add 12 to hour
7) Postfix Expression Evaluation Using Stack
Create a class that implements a Stack with the following methods:
• push(value: string)
• pop()
• evaluate()
The stack should be encapsulated, meaning the stack data should be private and can only be
accessed through these methods.
Use this class to evaluate a Postfix Expression.
It is guaranteed that the input expression is always valid.
Input Format
• First line contains an integer N, representing the number of tokens.
• Second line contains N space-separated tokens (numbers and operators).
Supported Operators
TCS Problem Statements
• +
• -
• *
• /
Output Format
Print the result of the postfix expression.
Example 1
Input
5
13 5 + 4 -
Output
14
Explanation
13 5 + = 18
18 4 - = 14
Example 2
Input
562+*4-
Output
36
Explanation
6+2=8
5 × 8 = 40
40 - 4 = 36
8)Problem 1: Gym Fee Calculation
TCS Problem Statements
8. Problem Statement
A gym offers subscription plans based on the duration of membership. The available plans
and their corresponding fees are given below:
Duration (months) Cost (₹)
1 2000
3 5000
6 9000
9 12000
12 15000
Your task is to determine the total gym fee based on the number of months selected.
If the entered duration does not match any of the available plans, print "Error".
______________
Input Format
A single integer M representing the number of months.
______________
Output Format
Print the corresponding gym fee.
If the input is invalid, print:
Error
______________
Constraints
• (1 <= M <= 12)
______________
Sample Input 1
Sample Output 1
5000
______________
Sample Input 2
TCS Problem Statements
Sample Output 2
9000
______________
Sample Input 3
10
Sample Output 3
Error
______________
9)Fraud Transactions Detection
You are given a list of transaction records.
Each transaction is provided in the following format:
Sender Receiver Amount Timestamp
A transaction is considered fraudulent if:
• Another transaction with the same Sender, Receiver, and Amount already exists.
• The difference between the two transaction timestamps is less than 60 seconds.
Your task is to identify and print all fraudulent transactions. If no fraudulent transactions are
found, print "No Fraud".
______________
Input Format
• The first line contains an integer N, representing the number of transactions.
• The next N lines each contain four values:
o Sender (String)
o Receiver (String)
o Amount (Integer)
o Timestamp (Integer)
______________
Output Format
TCS Problem Statements
• Print each fraudulent transaction in the same format as the input:
• Sender Receiver Amount Timestamp
• If there are no fraudulent transactions, print:
• No Fraud
______________
Constraints
• (1 \leq N \leq 10^5)
• Sender and Receiver are non-empty strings.
• Amount and Timestamp are positive integers.
______________
Sample Input 1
3
A B 100 1000
A B 100 1040
A B 200 1100
Sample Output 1
A B 100 1040
______________
Sample Input 2
4
X Y 500 100
X Y 500 180
A B 300 250
A B 300 400
Sample Output 2
No Fraud
______________
TCS Problem Statements
10)Parking Fee Calculation
A parking area charges fees based on the number of hours a vehicle is parked:
• For the first 2 hours, the charge is ₹100 per hour.
• For the next 3 hours (i.e., from hour 3 to hour 5), the charge is ₹50 per hour.
• For any time beyond 5 hours, the charge is ₹20 per hour.
Your task is to calculate the total parking fee based on the total number of hours the vehicle is
parked.
______________
Input Format
A single integer H representing the total number of hours the vehicle is parked.
______________
Output Format
Print a single integer representing the total parking fee.
______________
Constraints
• (0 \leq H \leq 10^5)
______________
Sample Input 1
Sample Output 1
370
______________
Sample Input 2
Sample Output 2
200
______________
Sample Input 3
4
TCS Problem Statements
Sample Output 3
300
______________
11)Hot Air Balloon – Maximum People
A hot air balloon has a maximum weight capacity of W kilograms.
You are given an array of integers representing the weights of N people:
w1, w2, w3, ..., wN
Your task is to determine the maximum number of people that can fit into the balloon such
that the total weight does not exceed W.
Input Format
• The first line contains an integer N, representing the number of people.
• The second line contains N space-separated integers representing the weights of the
people.
• The third line contains an integer W, representing the maximum weight capacity of
the balloon.
Output Format
Print a single integer representing the maximum number of people that can fit into the
balloon.
______________
Constraints
• (1 <= N <= 10^5)
• (1 <= w_i <= 10^5)
• (1 <= W <= 10^9)
______________
Sample Input 1
5
40 50 60 70 80
TCS Problem Statements
200
Sample Output 1
______________
Sample Input 2
30 20 10 40
100
Sample Output 2
Sample Input 3
6
50 60 70 80 90 100
150
Sample Output 3
12) Ticket Price Analysis
In a theatre, ticket prices are recorded for a show. Each ticket price lies in the range 30 to
100000 (inclusive).
You are given a list of ticket prices as input.
Your task is to process only the valid ticket prices (i.e., prices within the given range) and
perform the following operations:
1. Calculate the sum of all odd ticket prices.
2. Count the total number of odd ticket prices.
3. Calculate the mean (average) of the odd ticket prices.
If there are no odd ticket prices, print the mean as 0.
Input Format
A single line containing space-separated integers representing the ticket prices.
TCS Problem Statements
Output Format
Print three values:
• Sum of odd ticket prices
• Count of odd ticket prices
• Mean (average) of odd ticket prices
______________
Constraints
• 30 ≤ Ticket Price ≤ 100000
• Only valid ticket prices should be considered.
______________
Example
Input
55 60 70 45
Output
100
50
13) Sweet Seventeen
Given a maximum of four digits to the base 17(10 -> A, 11 -> B, 12 -> C, 16 -> G) as input,
output its decimal value.
Input: 23GF
OUTPUT : 10980
TCS Problem Statements
14) A Sober Walk
Our hoary culture had several great persons since time immemorial and king Vikramaditya’s
nava ratnas (nine gems) belongs to this ilk. They are named in the following shloka:
Among these, Varahamihira was an astrologer of eminence and his book Brihat Jataak is
recokened as the ultimate authority in astrology. He was once talking with Amarasimha,
another gem among the nava ratnas and the author of the Sanskrit thesaurus, Amarakosha.
Amarasimha wanted to know the final position of a person, who starts from the origin 0 0 and
travels per the following scheme.
• He first turns and travels 10 units of distance
• His second turn is upward for 20 units
• The third turn is to the left for 30 units
• The fourth turn is downward for 40 units
• The fifth turn is to the right(again) for 50 units …
And thus he travels, every time increasing the travel distance by 10 units.
Constraints: 2<=n<=1000
Input: 3
OUTPUT: -20 20
15) Word is the key
One programming language has the following keywords that cannot be used as identifiers:
break, case, continue, default, defer, else, for, func, goto, if, map, range, return, struct, type,
var
Write a program to find if the given word is a keyword or not
Input #1: defer
Output: defer is a keyword
Input #2: While
Output: While is not a keyword
TCS Problem Statements
16) A chocolate factory is packing chocolates into the packets. The chocolate packets here
represent an array of N number of integer values. The task is to find the empty packets(0) of
chocolate and push it to the end of the conveyor belt(array).
Example 1 :
N=8 and arr = [4,5,0,1,9,0,5,0]. There are 3 empty packets in the given set. These 3 empty
packets represented as O should be pushed towards the end of the array
Input : 8 – Value of N [4,5,0,1,9,0,5,0] – Element of arr[O] to arr[N-1],While input each
element is separated by newline
Output: 4 5 1 9 5 0 0 0
Example 2:
Input: 6 — Value of N. [6,0,1,8,0,2] – Element of arr[0] to arr[N-1], While input each
element is separated by newline
Output: 6 1 8 2 0 0
17) Joseph is learning digital logic subject which will be for his next semester. He usually
tries to solve unit assignment problems before the lecture. Today he got one tricky question.
The problem statement is “A positive integer has been given as an input. Convert decimal
value to binary representation. Toggle all bits of it after the most significant bit including the
most significant bit. Print the positive integer value after toggling all bits”.
Constrains- 1<=N<=100
Example 1:
Input : 10 -> Integer
Output : 5 -> result- Integer
Explanation: Binary representation of 10 is 1010. After toggling the bits(1010), will get
0101 which represents “5”. Hence output will print “5”.
TCS Problem Statements
18) Jack is always excited about sunday. It is favourite day, when he gets to play all day. And
goes to cycling with his friends. So every time when the months starts he counts the number
of sundays he will get to enjoy. Considering the month can start with any day, be it Sunday,
Monday…. Or so on.
Count the number of Sunday jack will get within n number of days.
Example 1: Input mon-> input String denoting the start of the month. 13 -> input integer
denoting the number of days from the start of the month.
Output : 2 -> number of days within 13 days.
Explanation: The month start with mon(Monday). So the upcoming sunday will arrive in
next 6 days. And then next Sunday in next 7 days and so on. Now total number of days are
[Link] means 6 days to first sunday and then remaining 7 days will end up in another sunday.
Total 2 sundays may fall within 13 days.
19) Airport security officials have confiscated several item of the passengers at the security
check point. All the items have been dumped into a huge box (array). Each item possesses a
certain amount of risk[0,1,2]. Here, the risk severity of the items represent an array[] of N
number of integer values. The task here is to sort the items based on their levels of risk in the
array. The risk values range from 0 to 2.
Example :
Input : 7 -> Value of N [1,0,2,0,1,0,2]-> Element of arr[0] to arr[N-1], while input each
element is separated by new line.
Output : 0 0 0 1 1 2 2 -> Element after sorting based on risk severity
Example 2:
input : 10 -> Value of N [2,1,0,2,1,0,0,1,2,0] -> Element of arr[0] to arr[N-1], while input
each element is separated by a new line.
Output : 0 0 0 0 1 1 1 2 2 2 ->Elements after sorting based on risk severity.
Explanation: In the above example, the input is an array of size N consisting of only 0’s, 1’s
and 2s. The output is a sorted array from 0 to 2 based on risk severity.
TCS Problem Statements
20) Given an integer array Arr of size N the task is to find the count of elements whose value
is greater than all of its prior elements.
Note : 1st element of the array should be considered in the count of the result.
For example, Arr[]={7,4,8,2,9} As 7 is the first element, it will consider in the result. 8 and 9
are also the elements that are greater than all of its previous elements. Since total of 3
elements is present in the array that meets the condition. Hence the output = 3.
Example 1: Input 5 -> Value of N, represents size of Arr 7-> Value of Arr[0] 4 -> Value of
Arr[1] 8-> Value of Arr[2] 2-> Value of Arr[3] 9-> Value of Arr[4]
Output : 3 Example 2: 5 -> Value of N, represents size of Arr 3 -> Value of Arr[0] 4 ->
Value of Arr[1] 5 -> Value of Arr[2] 8 -> Value of Arr[3] 9 -> Value of Arr[4]
Output : 5
Constraints: 1<=N<=20 1<=Arr[i]<=10000
21) A supermarket maintains a pricing format for all its products. A value N is printed on each
product. When the scanner reads the value N on the item, the product of all the digits in the
value N is the price of the item.
The task here is to design the software such that given the code of any item N the product
(multiplication) of all the digits of value should be computed(price).
Example 1:
Input : 5244 -> Value of N
Output : 160 -> Price
Explanation: From the input above Product of the digits 5,2,4,4 5*2*4*4= 160 Hence,
output is 160.
22) Ayush is working on a strange algorithm where he wants to convert a string from A
to B, both the strings of equal length N
Below are the rules which can be performed to convert a string
• String A and B are of equal length
• Both of them are in lower case
• Choose a subset X from the string A, between the index 1 and N.
• Let ‘s’ be the letter which alphabetically comes before all other letters in the subset. Let
TCS Problem Statements
‘s’ be called the ‘smallest element’ in the subset.
• Replace all the elements of subset with the letter ‘s’
Find the minimum number of moves which is required to perform the conversion. If it
is not possible to convert the string from A to b then return -1
Let us try to understand it with and examples
Suppose there are 2 strings
A = abcab
B = aabab
Operation 1:
Now we have chosen a subset S, let us say we have taken index 2,3,5 from A
Then the subset S becomes [bcb]
Next, we have to choose the smallest element , 6041 here, which is b here in b & c
Next, we have to replace all the other elements in subset with this element. So ‘b’
with replace everything in [bcb]. which becomes [bbb].
Now we will place all the respective elements back to their respective index. This will
update the original string as [abbab]
Operation 2:
Original string [abbab]
Now we have chosen a subset S, let say we have taken a index 1,2,4 from A
Then the subset become [aba]
Next, we have to choose the smallest element, which is here in a & b.
Next, we have to replace the smallest with all the other elements in subset. So ‘a’ will
replace everything in [aba]
Now we will place all the respective elements back to their respective index. This will
update the original string as [aabab]
This is exactly same as String B
Hence it is possible to convert string A to B, with 2 operations. So, the answer is 2.
Example 1:
Input:
2-> Input integer, N
de-> input string, A
cd-> Input string, B
Output:
-1
TCS Problem Statements
Explanation:
In the above example we can clearly see that there is an alphabet in A which is
completely different from B. hence it is not possible to convert A to B
So the answer is -1
Example 2:
Input:
4-> input integer, N
abab-> input string, A
abaa-> input string A
Output:
1 -> Output
Explanation:
Operation 1:
Now we have chosen a subset S, let say we have taken index 3,4 from A
Then the Subset S becomes [ab]
Next, we have to choose the smallest element, which is a here in a & b
Next, we have to replace the smallest with all the other elements in subset. So ‘a’ will
replace everything in [abl, which becomes [aa]
Now we will place all the respective elements back to their respective index. This will
update the original string as [abaa]
This is exactly same as String B
Hence it is possible to convert string A to B. with 1 operation. So, the answer is 1.
Constraints:
1. 1<=N<=1000
2. N integer
3. Only lower case letters of the English alphabet
4. Length of A,B = N
The input format for testing
1. First Input-Accept value of Integer, N.
2. Second Input-Accept value of string, A (Next Line)
3. Third Input-Accept value of string, B(Next Line)
TCS Problem Statements
The Output format for testing
1. The output is an integer as per above logic. (Check the output in Example 1, Example 21
2. Additional messages in output will cause the failure of test cases
Instructions:
1. System doesn’t allow any kind of hard coded input value/values.
2. Written program code by the candidate will be verified against the inputs which are
supplied from the system.
23) Jack is a sports teacher at St. Patrick’s School. He makes games not only to make
the student fit, but So smart.
So, he lined up all the N numb class. of students in his class.
At each position he has fixed a board with the Integer number printed on it. Each of
the numbers are unique and are in exactly the range of N. Let us say there are 10
students, then the boards will be printed with numbers from 1 to 10 in a random
order given by the sequence A[ ]
As a rule, all students wear a jersey with their numbers printed on it. So if there are
students, each will have a unique jersey number just like a football team.
Now, in the beginning, all the students will stand as per the increasing order of their
jersey numbers, from left to right.
The only difference will be their respective board number which is placed at their
respective location. The board location is fixed and cannot be changed. We can
consider the arrangement as below. Suppose there are students, and the board is
placed in the order of [2 3 1 5 4]
Board — 2, 3, 1, 5, 4
Student’s Jersey — 1, 2, 3, 4, 5
Now the game begins.
• After every beat of the drum, each student will have to move to that location (index),
where his board is pointing to. In the above case student with jersey #1 is standing with
board #2, so now he will have to move to location #2. Similarly, all the other students will
do.
So after first beat of the drum, the alignment will be:
Board — 2, 3, 1, 5, 4
This keeps going on and on, until all the students are back the way they were at the
beginning. So, after 6 beats of the drum, all the students will be aligned the same
way as before.
Given N and the order of board of the respective positions, find the number of beats
required to bring back the students to their original position.
TCS Problem Statements
So, for the above case the answer is 6
Example 1:
Input:
3 Input integer, N
{1, 2, 3}->Input integer. B[], board alignment.
Output:
1 -> Output
Explanation:
All the students will be standing as board positions;
Board — 1, 2, 3
Student’s Jersey –1, 2, 3
After first beat of the drum:
Jersey #1 will move to index 1.
Jersey #2 will move to index 2.
Jersey #3 will move to index 3.
Hence, they will be back on their own position in just 1 beat.
So, the answer is 1.
Example 2:
Input:
5-> Input integer, N
{2, 3, 1, 5, 4}-> Input integer, B[ ], board alignment.
Output:
6-> Output
Explanation:
All the students will be standing as below, with the board positions:
Board — 2, 3, 1, 5, 4
Student’s Jersey — 1, 2, 3, 4, 5
After Beat-1of the drum:
Jersey #1 has moved to index 2.
Jersey #2 has moved to index 3.
TCS Problem Statements
Jersey #3 has moved to index 1.
Jersey #4 has moved to index 5.
Jersey #5 has moved to index 4.
Board – 2, 3, 1, 5, 4
Student’ s Jersey — 3, 1, 2, 5, 4
After Beat-2 of the drum:
Jersey #3 has moved to index 2.
Jersey #1 has moved to index 3.
Jersey #2 has moved to index 1.
Jersey #5 has moved to index 5.
Jersey #4 has moved to index 4.
Board — 2, 3, 1, 5, 4
Student’s Jersey — 2, 3, 1, 4, 5
After Beat-3 of the drum:
Board — 2, 3, 1, 5, 4
Student’s Jersey — 1, 2, 3, 5, 4
After Beat-4 of the drum:
Board — 2, 3, 1, 5, 4
Student’s Jersey — 3, 1, 2, 4, 5
After Beat-5 of the drum:
Board — 2, 3, 1, 5, 4
Student’s Jersey — 2, 3, 1, 5, 4
After Beat-6 of the drum:
Board — 2, 3, 1, 5, 4
Student’s Jersey — 1, 2, 3, 4, 5
Hence, they will be back on their positions after 6 beats. So, the answer is 6.
Constraints:
• 1<=N<=100000
• 1 <=A[i] <= N
• All A[i] will be distinct numbers
• N and. Only Integers.
The input format for testing:
• First Input – Accept value of In1101
• Next ‘N’ Lines-Elements of sequence A[]