Tcs Asked Coding Question
Tcs Asked Coding Question
============================================================
TCS NQT 2027 | July Batch | Round 1
Question 1 – Search Key (Closest Element)
============================================================
QUESTION
--------
Given a sorted array and a key, perform Search.
INPUT
-----
• First line contains an integer N.
• Second line contains N sorted integers.
• Third line contains the integer key.
OUTPUT
------
• Print the key if found.
• Otherwise, print the nearest element.
------------------------------------------------------------
JAVA SOLUTION
------------------------------------------------------------
import [Link].*;
int n = [Link]();
int[] arr = new int[n];
break;
}
}
[Link](closest);
}
}
------------------------------------------------------------
EXPLANATION
------------------------------------------------------------
• Perform Binary Search on the sorted array.
• If the key is found, print it immediately.
• If not found, 'low' and 'high' indicate the two closest elements.
• Compare their absolute differences with the key.
• If both are equally close, print the smaller element (arr[high]).
QUESTION
--------
Given a positive integer N, print all of its prime factors in increasing order.
INPUT
-----
• A single integer N.
OUTPUT
------
• Print the prime factors of N separated by spaces.
------------------------------------------------------------
JAVA SOLUTION
------------------------------------------------------------
import [Link].*;
int n = [Link]();
while (n % i == 0) {
[Link](i + " ");
n /= i;
}
}
if (n > 1) {
[Link](n);
}
[Link]();
}
}
------------------------------------------------------------
EXPLANATION
------------------------------------------------------------
• Start checking factors from 2.
• While a number divides N completely, print it and divide N.
• Continue until i × i > N.
• If N is still greater than 1, it is the last prime factor.
============================================================
TCS NQT 2027 | July Batch | Round 1
Question 3 – Kth Best-Selling Product (Min Heap)
============================================================
QUESTION
--------
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.
INPUT
-----
• First line contains two integers N and K.
• Second line contains N integers representing the sales count of each product.
OUTPUT
------
• Print the Kth best-selling product's sales count.
CONSTRAINTS
-----------
• 1 ≤ K ≤ N ≤ 10⁵
• Sales count is a positive integer.
SAMPLE INPUT 1
--------------
63
50 20 70 40 90 60
SAMPLE OUTPUT 1
---------------
60
Explanation:
Descending Order → 90 70 60 50 40 20
The 3rd best-selling product is 60.
SAMPLE INPUT 2
--------------
52
15 25 10 40 30
SAMPLE OUTPUT 2
---------------
30
Explanation:
Descending Order → 40 30 25 15 10
The 2nd best-selling product is 30.
------------------------------------------------------------
JAVA SOLUTION
------------------------------------------------------------
import [Link].*;
int n = [Link]();
int k = [Link]();
QUESTION
--------
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
INPUT
-----
• A single string representing time in 12-hour format.
Format:
hh:mm:ssAM
or
hh:mm:ssPM
OUTPUT
------
• Return the equivalent time in 24-hour format.
CONSTRAINTS
-----------
• All input times are valid.
SAMPLE INPUT
------------
07:05:45PM
SAMPLE OUTPUT
-------------
19:05:45
------------------------------------------------------------
JAVA SOLUTION
------------------------------------------------------------
import [Link].*;
if ([Link]("AM")) {
if (hour == 12)
hour = 0;
} else {
if (hour != 12)
hour += 12;
}
[Link]("%02d%s%n", hour, [Link](2, 8));
[Link]();
}
}
------------------------------------------------------------
EXPLANATION
------------------------------------------------------------
============================================================
TCS NQT 2027 | July Batch | Round 1
Question 5 – Find the More Expensive Item
============================================================
QUESTION
--------
There are two items A and B with their respective prices.
Determine which item is more expensive.
Conditions:
• If either price is less than 0, print "Invalid input".
• If both prices are equal, print "Prices equal".
• Otherwise, print the higher price followed by "is more expensive".
INPUT
-----
• Two integers A and B representing the prices of the two items.
OUTPUT
------
• Print the required result based on the given conditions.
SAMPLE INPUT 1
--------------
10 15
SAMPLE OUTPUT 1
---------------
15 is more expensive
SAMPLE INPUT 2
--------------
10 10
SAMPLE OUTPUT 2
---------------
Prices equal
SAMPLE INPUT 3
--------------
-5 20
SAMPLE OUTPUT 3
---------------
Invalid input
------------------------------------------------------------
JAVA SOLUTION
------------------------------------------------------------
import [Link].*;
if (a < 0 || b < 0) {
[Link]("Invalid input");
}
else if (a == b) {
[Link]("Prices equal");
}
else {
[Link]([Link](a, b) + " is more expensive");
}
[Link]();
}
}
------------------------------------------------------------
EXPLANATION
------------------------------------------------------------
============================================================
TCS NQT 2027 | July Batch | Round 1
Question 6 – Postfix Expression Evaluation (Using Stack)
============================================================
QUESTION
--------
Create a Stack class with the following methods:
• push(value)
• pop()
• evaluate()
The stack should be encapsulated (private data) and used to evaluate a valid Postfix Expression.
Supported Operators:
•+
•-
•*
•/
INPUT
-----
• First line contains an integer N (number of tokens).
• Second line contains N space-separated tokens (operands/operators).
OUTPUT
------
• Print the evaluated result of the postfix expression.
SAMPLE INPUT
------------
5
13 5 + 4 -
SAMPLE OUTPUT
-------------
14
Explanation:
13 + 5 = 18
18 - 4 = 14
------------------------------------------------------------
JAVA SOLUTION
------------------------------------------------------------
import [Link].*;
int n = [Link]();
if ([Link]("+") || [Link]("-") ||
[Link]("*") || [Link]("/")) {
int b = [Link]();
int a = [Link]();
switch (token) {
case "+":
[Link](a + b);
break;
case "-":
[Link](a - b);
break;
case "*":
[Link](a * b);
break;
case "/":
[Link](a / b);
break;
}
} else {
[Link]([Link](token));
}
}
[Link]([Link]());
[Link]();
}
}EXPLANATION
------------------------------------------------------------
============================================================
============================================================
TCS NQT 2027 | July Batch | Round 1
Question 7 – Largest Rectangle in Histogram
============================================================
QUESTION
--------
Given an array heights[] where each element represents the height of a histogram bar and the width
of every bar is 1, find the largest rectangular area that can be formed inside the histogram.
INPUT
-----
• First line contains an integer N.
• Second line contains N integers representing the heights of the histogram bars.
OUTPUT
------
• Print the largest rectangular area.
SAMPLE INPUT
------------
6
215623
SAMPLE OUTPUT
-------------
10
Explanation:
The largest rectangle is formed using bars with heights 5 and 6.
Area = 5 × 2 = 10.
------------------------------------------------------------
JAVA SOLUTION
------------------------------------------------------------
import [Link].*;
int n = [Link]();
int[] heights = new int[n];
int maxArea = 0;
int width = j - i + 1;
int area = minHeight * width;
[Link](maxArea);
}
}
------------------------------------------------------------
EXPLANATION
------------------------------------------------------------
============================================================
TCS NQT 2027 | Java Program
Frequency of Characters in a String (Using Array)
============================================================
QUESTION
--------
Given a string, count the frequency of each character using an array.
INPUT
-----
A single string.
OUTPUT
------
Print each character along with its frequency.
SAMPLE INPUT
------------
programming
SAMPLE OUTPUT
-------------
p:1
r:2
o:1
g:2
a:1
m:2
i:1
n:1
------------------------------------------------------------
JAVA SOLUTION
------------------------------------------------------------
import [Link].*;
char ch = [Link](i);
if (freq[i] > 0) {
[Link]((char)(i + 'a') + " : " + freq[i]);
}
}
[Link]();
}
}
------------------------------------------------------------
EXPLANATION
------------------------------------------------------------
============================================================
============================================================
TCS NQT 2027 | July Batch | Round 1 Question 9 – Arithmetic Progression (Nth Term & Sum of N
Terms)
============================================================
QUESTION
An Arithmetic Progression (AP) is a sequence in which the difference between consecutive terms is
constant.
Given the first term (A), the common difference (D), and the number of terms (N):
• Find the Nth term of the Arithmetic Progression. • Find the sum of the first N terms.
INPUT
• First line contains the first term A. • Second line contains the common difference D. • Third line
contains the number of terms N.
OUTPUT
• Print the Nth term. • Print the sum of the first N terms.
SAMPLE INPUT
235
SAMPLE OUTPUT
Nth Term : 14 Sum : 40
Explanation: AP = 2, 5, 8, 11, 14
Nth Term = 14
Sum = 2 + 5 + 8 + 11 + 14 = 40
JAVA SOLUTION
import [Link].*;
public class Main {
public static void main(String[] args) {
int a = [Link]();
int d = [Link]();
int n = [Link]();
int nthTerm = a + (n - 1) * d;
[Link]();
}
EXPLANATION
Nth Term Formula: Nth Term = A + (N - 1) × D
Sum Formula: Sum = N × [2A + (N - 1) × D] / 2
• Read A, D and N. • Calculate the Nth term using the AP formula. • Calculate the sum of the first N
terms using the sum formula. • Print both results.
Time Complexity : O(1)
Space Complexity : O(1)
============================================================
============================================================
TCS NQT 2027 | Java Program | Question 10
Fibonacci Series & Sum of First N Terms
============================================================
QUESTION
--------
Given an integer N, generate the first N terms of the Fibonacci series and print their sum.
OUTPUT
------
• Print the Fibonacci series.
• Print the sum of the first N terms.
SAMPLE INPUT
------------
7
SAMPLE OUTPUT
-------------
Fibonacci Series : 0 1 1 2 3 5 8
Sum : 20
------------------------------------------------------------
JAVA SOLUTION
------------------------------------------------------------
import [Link].*;
int n = [Link]();
int a = 0, b = 1;
int sum = 0;
if (n >= 1) {
[Link](a + " ");
sum += a;
}
if (n >= 2) {
[Link](b + " ");
sum += b;
}
int c = a + b;
a = b;
b = c;
}
[Link]();
[Link]("Sum : " + sum);
[Link]();
}
}
------------------------------------------------------------
EXPLANATION
------------------------------------------------------------
• Initialize the first two Fibonacci numbers as 0 and 1.
• Print 0 and 1 separately and add them to the sum.
• From the 3rd term onwards, calculate:
c=a+b
• Print c, update the sum, then update:
a=b
b=c
• Repeat until N terms are generated.
============================================================
============================================================
TCS NQT 2027 | Java Program | Question 11
Integer to Roman Numeral Conversion
============================================================
QUESTION
--------
Given an integer, convert it into its equivalent Roman Numeral
Roman Symbols:
I=1
V=5
X = 10
L = 50
C = 100
D = 500
M = 1000
1513-1000=513-500=13-10=3-1=2-1=1
M D X 1 1 1
Num=1513->MDX111
14->X
INPUT
-----
• A single integer N.
OUTPUT
------
• Print the corresponding Roman Numeral.
CONSTRAINTS
-----------
• 1 ≤ N ≤ 3999
SAMPLE INPUT
------------
1994
SAMPLE OUTPUT
-------------
MCMXCIV
------------------------------------------------------------
JAVA SOLUTION
------------------------------------------------------------
import [Link].*;
[Link](roman[i]);
num -= values[i];
}
}
[Link](result);
[Link]();
}
}
------------------------------------------------------------
EXPLANATION
------------------------------------------------------------
============================================================
============================================================
TCS NQT 2027 | July Batch | Round 1
Question 12 – Kth Highest Salaried Person
============================================================
QUESTION
--------
Given the names of employees and their corresponding salaries, sort them in ascending order of
salary and print the sorted list.
INPUT
-----
• First line contains an integer N (number of employees).
• Second line contains N employee names.
• Third line contains N employee salaries.
• Fourth line contains an integer K.
OUTPUT
------
• Print the employees in ascending order of salary.
• Print the Kth highest salaried person's name.
SAMPLE INPUT
------------
4
A B C D
0 1 2 3
2000 3000 1500 4000
4000 3000 2000 1500
[Link](list, [Link]());
3
SAMPLE OUTPUT
-------------
Ascending Order
1500 -c
2000 - a
3000 – b
4000- d
k=n-k
[Link](index) wrong
------------------------------------------------------------
JAVA SOLUTION
------------------------------------------------------------
import [Link].*;
[Link](list, [Link]());
[Link]([Link](kthMark));
}
}
------------------------------------------------------------
EXPLANATION
------------------------------------------------------------
============================================================
============================================================
TCS NQT 2027 | July Batch | Round 1
Question 13 – First Unique Element in an Array
============================================================
QUESTION
--------
Given an array of integers, return the first unique element using a HashMap.
The array contains exactly one element that appears only once, while every other element appears
exactly twice.
INPUT
-----
• First line contains an integer N (size of the array).
• Second line contains N integers.
OUTPUT
------
• Print the first element that appears only once.
SAMPLE INPUT
------------
7
2354532
SAMPLE OUTPUT
-------------
4
Explanation:
Frequency of elements:
2 → 2 times
3 → 2 times
5 → 2 times
4 → 1 time
------------------------------------------------------------
JAVA SOLUTION
------------------------------------------------------------
import [Link].*;
public class Main {
int n = [Link]();
arr[i] = [Link]();
if ([Link](arr[i]) == 1) {
[Link](arr[i]);
break;
}
}
[Link]();
}
}
------------------------------------------------------------
EXPLANATION
------------------------------------------------------------
============================================================
============================================================
TCS NQT 2027 | July Batch | Round 1
Question 14 – Print All Indices of the Target Element
============================================================
QUESTION
--------
Given a sorted array and a target element, print all the indices at which the target element is
present.
INPUT
-----
• First line contains an integer N (size of the array).
• Second line contains N sorted integers.
• Third line contains the target element.
OUTPUT
------
• Print all the indices where the target element is present.
SAMPLE INPUT
------------
8
12223445
SAMPLE OUTPUT
-------------
123
Explanation:
The target element 2 occurs at indices 1, 2 and 3.
------------------------------------------------------------
JAVA SOLUTION
------------------------------------------------------------
import [Link].*;
int n = [Link]();
if (arr[i] == target) {
[Link](i + " ");
}
}
[Link]();
}
}
------------------------------------------------------------
EXPLANATION
------------------------------------------------------------
============================================================
============================================================
TCS NQT 2027 | July Batch | Round 1
Question 16 – Least Even Sum of Two Prime Numbers
============================================================
QUESTION
--------
Given an array of integers, find the least possible sum of any two prime numbers present in the array
such that their sum is even.
INPUT
-----
• First line contains an integer N (size of the array).
• Second line contains N integers.
OUTPUT
------
• Print the least even sum of two prime numbers.
SAMPLE INPUT
------------
6
7 5 2 11 8 3
SAMPLE OUTPUT
-------------
8
Explanation:
Prime Numbers = 7, 5, 2, 11, 3
------------------------------------------------------------
JAVA SOLUTION
------------------------------------------------------------
import [Link].*;
if (n < 2)
return false;
if (n % i == 0)
return false;
}
return true;
}
int n = [Link]();
int sol = 0;
if (!isPrime(arr[i]))
continue;
if (!isPrime(arr[j]))
continue;
if (minSum == Integer.MAX_VALUE)
[Link]("Not Possible");
else
[Link](minSum);
[Link]();
}
}
------------------------------------------------------------
EXPLANATION
------------------------------------------------------------
============================================================
TCS NQT 2027 | July Batch | Round 1
Question 17 – Highest Scoring Student & Average Marks
============================================================
QUESTION
--------
Given two arrays:
Print the name of the student who scored the highest marks and the average marks of all students.
Output Format:
[Highest Scoring Student : Average Marks]
INPUT
-----
• First line contains an integer N (number of students).
• Second line contains N student names.
• Third line contains N student marks.
OUTPUT
------
• Print the highest scoring student's name and the average marks in the format:
[StudentName : AverageMarks]
SAMPLE INPUT
------------
3
SAMPLE OUTPUT
-------------
[krish : 200]
Explanation:
Highest Marks = 300 (krish)
------------------------------------------------------------
JAVA SOLUTION
------------------------------------------------------------
import [Link].*;
int n = [Link]();
int sum = 0;
int max = Integer.MIN_VALUE;
int index = 0;
sum += marks[i];
[Link]();
}
}
------------------------------------------------------------
EXPLANATION
------------------------------------------------------------
============================================================
============================================================
TCS NQT 2027 | July Batch | Round 1
Question 18 – First Non-Repeating Character
============================================================
QUESTION
--------
Given a string, find the first character that does not repeat.
INPUT
-----
• A single string.
OUTPUT
------
• Print the first non-repeating character.
• If no such character exists, print -1.
SAMPLE INPUT 1
--------------
aabbcdde
SAMPLE OUTPUT 1
---------------
c
SAMPLE INPUT 2
--------------
aabbcc
SAMPLE OUTPUT 2
---------------
-1
------------------------------------------------------------
JAVA SOLUTION
------------------------------------------------------------
import [Link].*;
char ch = [Link](i);
char ch = [Link](i);
if ([Link](ch) == 1) {
[Link](ch);
return;
}
}
[Link](-1);
[Link]();
}
}
------------------------------------------------------------
EXPLANATION
------------------------------------------------------------
============================================================
============================================================
TCS NQT 2027 | July Batch | Round 1
Question 19 – Equilibrium Index of an Array
============================================================
QUESTION
--------
Given an array, find an index such that the sum of elements on its left is equal to the sum of
elements on its right.
If no such index exists, print -1.
INPUT
-----
• First line contains an integer N (size of the array).
• Second line contains N integers.
OUTPUT
------
• Print the equilibrium index.
• If no such index exists, print -1.
SAMPLE INPUT 1
--------------
5
13522
Total sum=13
SAMPLE OUTPUT 1
---------------
2
Explanation:
Left Sum = 1 + 3 = 4
Right Sum = 2 + 2 = 4
SAMPLE INPUT 2
--------------
4
1234
SAMPLE OUTPUT 2
---------------
-1
------------------------------------------------------------
JAVA SOLUTION
------------------------------------------------------------
import [Link].*;
int totalSum = 0;
int leftSum = 0;
totalSum -= arr[i];
if (leftSum == totalSum) {
[Link](i);
return;
}
leftSum += arr[i];
}
[Link](-1);
[Link]();
}
}
------------------------------------------------------------
EXPLANATION
------------------------------------------------------------
============================================================
TCS NQT 2027 | July Batch | Round 1
Question 20 – Two Sum (Brute Force)
============================================================
QUESTION
--------
Given a sorted array and a target sum, find two numbers such that they add up to the target.
INPUT
-----
• First line contains an integer N (size of the array).
• Second line contains N sorted integers.
• Third line contains the target sum.
OUTPUT
------
• Print the indices of the two numbers whose sum equals the target.
• If no such pair exists, print -1.
SAMPLE INPUT 1
--------------
5
2 7 11 15 18
SAMPLE OUTPUT 1
---------------
01
Explanation:
arr[0] + arr[1] = 2 + 7 = 9
SAMPLE INPUT 2
--------------
5
12345
20
SAMPLE OUTPUT 2
---------------
-1
------------------------------------------------------------
JAVA SOLUTION
------------------------------------------------------------
import [Link].*;
int n = [Link]();
if (found)
break;
}
if (!found)
[Link](-1);
[Link]();
}
}
------------------------------------------------------------
EXPLANATION
------------------------------------------------------------
============================================================
TCS NQT 2027 | July Batch | Round 1
Question 21 – Transpose of a Matrix
============================================================
QUESTION
--------
Given an N × M matrix, compute its transpose.
INPUT
-----
• First line contains two integers N and M.
• Next N lines contain M integers each.
OUTPUT
------
• Print the transpose of the matrix.
SAMPLE INPUT
------------
23
123
456
SAMPLE OUTPUT
-------------
14
25
36
Explanation:
Original Matrix:
123
456
Transpose:
14
25
36
------------------------------------------------------------
JAVA SOLUTION
------------------------------------------------------------
import [Link].*;
int n = [Link]();
int m = [Link]();
arr[i][j] = [Link]();
}
}
[Link]();
}
[Link]();
}
}
------------------------------------------------------------
EXPLANATION
------------------------------------------------------------
============================================================
TCS NQT 2027 | July Batch | Round 1
Question 23 – Majority Element
============================================================
QUESTION
--------
Given an array of size N, find the element that appears more than N/2 times.
INPUT
-----
• First line contains an integer N (size of the array).
• Second line contains N integers.
OUTPUT
------
• Print the majority element.
• If no majority element exists, print -1.
SAMPLE INPUT 1
--------------
7
2212322
SAMPLE OUTPUT 1
---------------
2
Explanation:
2 appears 5 times.
N/2 = 3
SAMPLE INPUT 2
--------------
5
12345
SAMPLE OUTPUT 2
---------------
-1
------------------------------------------------------------
JAVA SOLUTION
------------------------------------------------------------
import [Link].*;
int n = [Link]();
int count = 0;
if (arr[i] == arr[j]) {
count++;
}
}
if (count > n / 2) {
[Link](arr[i]);
found = true;
break;
}
}
if (!found) {
[Link](-1);
}
[Link]();
}
}
------------------------------------------------------------
EXPLANATION
------------------------------------------------------------
============================================================
TCS NQT 2027 | July Batch | Round 1
Question 24 – Replace Elements with Their Rank
============================================================
QUESTION
Given an array of integers, replace each element with its position (rank) in the sorted list of unique
elements.
• The smallest unique element has rank 1.
• The next smallest unique element has rank 2, and so on.
• If an element appears more than once, assign the same rank to each occurrence.
INPUT
• First line contains an integer N (size of the array).
• Second line contains N integers.
OUTPUT
• Print the rank of each element.
SAMPLE INPUT
5
23 6 4 6 1
SAMPLE OUTPUT
43231
Explanation:
Original Array : 23 6 4 6 1
Ranks:
1 →1
4 →2
6 →3
23 → 4
Hence,
23 6 4 6 1
43231
------------------------------------------------------------
JAVA SOLUTION
------------------------------------------------------------
import [Link].*;
int n = [Link]();
arr[i] = [Link]();
temp[i] = arr[i];
}
int rank = 1;
if () {
[Link](temp[i], rank);
rank++;
}
}
------------------------------------------------------------
EXPLANATION
------------------------------------------------------------
============================================================
TCS NQT 2027 | July Batch | Round 1
Question 25 – Find the Duplicate and Missing Number
============================================================
QUESTION
--------
An array of size N contains numbers from 0 to N-1.
INPUT
-----
• First line contains an integer N.
• Second line contains N space-separated integers.
OUTPUT
------
• Print the duplicate number.
• Print the missing number.
SAMPLE INPUT
------------
5
012345
012244
Index=-1;
For(int i=0;i<n-1;i++)
{
If(arr[i]==arr[i+1]
Index=arr[i]
Print (duplicate=arr[i]
missing =arr[i]+1
}
Index=2
Index+1→ missing number
Explanation:
Numbers should be:
01234
Hence,
Duplicate = 2
Missing = 3
------------------------------------------------------------
JAVA SOLUTION
------------------------------------------------------------
import [Link].*;
int n = [Link]();
arr[i] = [Link]();
freq[arr[i]]++;
}
int duplicate = -1;
int missing = -1;
if (freq[i] == 2) {
duplicate = i;
}
if (freq[i] == 0) {
missing = i;
}
}
[Link]();
}
}
------------------------------------------------------------
EXPLANATION
------------------------------------------------------------
============================================================
TCS NQT 2027 | July Batch | Round 1
Question 26 – +2, -1 String Encoding
============================================================
QUESTION
--------
Given a string, perform +2, -1 encoding based on the character index.
Rules:
Special Cases:
•Z→B
•z→b
•A→Z
•a→z
•9→1
•0→9
INPUT
-----
• A single string.
Char ch=’a’
Ch+=1
Print(ch)
→b
Char ch=’a’
Ch++;
Print(ch)
→b
Char ch=’a’
Ch=ch+1;
Print(ch)
→ERROR
OUTPUT
------
• Print the encoded string.
SAMPLE INPUT
------------
AbcZ0
For(int i=0;i<[Link]();i++)
{
If(i%2==0)
{
(char)[Link](i)+2
a - 97
Z - 90
A - 65
z - 122
SAMPLE OUTPUT
-------------
CaeY2
Explanation:
0 A +2 C
1 b -1 a
2 c +2 e
3 Z -1 Y
4 0 +2 2
------------------------------------------------------------
JAVA SOLUTION
------------------------------------------------------------
import [Link].*;
char ch = [Link](i);
if (i % 2 == 0) {
// Even Index : +2
if (ch == 'Z')
ch = 'B';
else if (ch == 'z')
ch = 'b';
else if (ch == '9')
ch = '1';
else
ch += 2;
} else {
// Odd Index : -1
if (ch == 'A')
ch = 'Z';
else if (ch == 'a')
ch = 'z';
else if (ch == '0')
ch = '9';
else
ch -= 1;
}
result += ch;
}
[Link](result);
[Link]();
}
}
------------------------------------------------------------
EXPLANATION
------------------------------------------------------------
============================================================
TCS NQT 2027 | July Batch | Round 1
Question 27 – Remove Duplicates and Reverse the Array
============================================================
QUESTION
--------
Given a sequence of integers:
INPUT
-----
• First line contains an integer N.
• Second line contains N integers.
OUTPUT
------
• Print the reversed array after removing duplicates.
SAMPLE INPUT
------------
6
141324
SAMPLE OUTPUT
-------------
1432
2341
Explanation:
Original Array:
141324
After Reversing:
2341
------------------------------------------------------------
JAVA SOLUTION
------------------------------------------------------------
import [Link].*;
int n = [Link]();
int size = 0;
// Remove Duplicates
for (int i = 0; i < n; i++) {
if (arr[i] == unique[j]) {
found = true;
break;
}
}
if (!found) {
unique[size] = arr[i];
size++;
}
}
[Link]();
}
}
------------------------------------------------------------
EXPLANATION
------------------------------------------------------------