TCS Practice
TCS Practice
Example 1:
5 — Value of N
{10, 20, 30, 40, 50} — Element of Arr[ ]
2 — Value of K
Output:
40 50 10 20 30
Example 2:
4 — Value of N
{10, 20, 30, 40} — Element of Arr[ ]
1 — Value of K
Output:
40 10 20 30
import java.util.*;
public class rotate {
static void reverse(int arr[], int start, int end) {
while (start < end) {
int temp = arr[start];
arr[start++] = arr[end];
arr[end--] = temp;
}
}
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
int N = in.nextInt();
int[] Arr = new int[N];
if (K > N) {
K = K % N;
}
reverse(Arr, 0, N - 1);
reverse(Arr, 0, K - 1);
reverse(Arr, K, N - 1);
Example 1:
Input:
11 — Value of n1
15 — Value of n2
Output:
4
Example 2:
Input:
101 — Value of n1
200 — Value of n2
Output:
72
import java.util.*;
public class checkrepeat {
public static Boolean repeat(int num) {
boolean arr[] = new boolean[10];
while (num > 0) {
if (arr[num % 10] == true) {
return false;
}
arr[num % 10] = true;
num = num / 10;
}
return true;
}
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
int n1 = in.nextInt();
int n2 = in.nextInt();
int count = 0;
for (int i = n1; i <= n2; i++) {
if (repeat(i) == true) {
count++;
}
}
System.out.print(count);
}
}
3. In this 3 Palindrome problem, given an input string word,
split the string into exactly 3 palindromic substrings.
Working from left to right, choose the smallest split for the
first substring that still allows the remaining word to be
split into 2 palindromes.
Similarly, choose the smallest second palindromic substring
that leaves a third palindromic substring.
If there is no way to split the word into exactly three
palindromic substrings, print “Impossible”.
Every character of the string needs to be consumed.
Constraints:
1 <= length of input string <= 1000
Input:
First line contains the input string consisting of characters
between [a-z].
Output:
Print 3 substrings, one on each line.
Time Limit:
1 second
Example 1:
Input:
nayannamantenet
Output:
nayan
naman
tenet
Example 2:
Input:
aaaaa
Output:
a
a
aaa
import java.util.*;
public class palindrome {
public static Boolean checkpal(String s) {
if (s.length() == 1) return true;
for (int i = 0; i < s.length() / 2; i++) {
if (s.charAt(i) != s.charAt(s.length() - i - 1))
return false;
}
return true;
}
if (flag == 1) System.out.println("Impossible");
}
}
4. In this Even-Odd problem, given a range [low, high] (both
inclusive), select K numbers from the range
(a number can be chosen multiple times) such that the sum of
those K numbers is even.
Calculate the number of all such permutations.
As this number can be large, print it modulo (1e9 + 7).
Constraints:
0 <= low <= high <= 10^9
K <= 10^6
Input:
First line contains two space-separated integers denoting low
and high respectively.
Second line contains a single integer K.
Output:
Print a single integer denoting the number of all such
permutations.
Time Limit:
1 second
Example 1:
Input:
4 5
3
Output:
4
Explanation:
There are 4 valid permutations: {4, 4, 4}, {4, 5, 5}, {5, 4,
5}, {5, 5, 4} — each summing to an even number.
Example 2:
Input:
1 10
2
Output:
50
Explanation:
There are 50 valid permutations like {1,1}, {1,3}, ...,
{10,10} which each sum to an even number.
import java.util.*;
public class permutation {
public static long mod = 1_000_000_007;
if (diff % 2 == 0) {
m = diff / 2;
n = m;
} else {
if (low % 2 == 0) {
m = (diff - 1) / 2;
n = m + 1;
} else {
m = (diff + 1) / 2;
n = m - 1;
}
}
Input:
First line contains the integer N — the number of packets.
Next N lines contain the elements of the array (one element
per line).
Output:
Print the modified array with all 0s pushed to the end.
Example 1:
Input:
8
4
5
0
1
9
0
5
0
Output:
4 5 1 9 5 0 0 0
Example 2:
Input:
6
6
0
1
8
0
2
Output:
6 1 8 2 0 0
import java.util.*;
public class chocolate {
public static void emptyshift(int arr[], int n) {
int count = 0, num = 0;
for (int i = 0; i < n; i++) {
if (arr[i] == 0) {
count++;
} else {
arr[num] = arr[i];
num++;
}
}
for (int i = n - count; i < n; i++) {
arr[i] = 0;
}
}
Constraints:
1 <= N <= 100
Input:
A single integer N.
Output:
The result as an integer after toggling all bits (including
MSB).
Example 1:
Input:
10
Output:
5
Explanation:
Binary of 10 is 1010. Toggling gives 0101, which is 5.
import java.util.*;
public class decitobin {
public static String bit(int n) {
String res = "";
while (n > 0) {
int r = n % 2;
if (r == 0) {
res = "1" + res;
} else {
res = "0" + res;
}
n = n / 2;
}
return res;
}
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
System.out.println(bit(n));
}
}
7. Problem Statement:
Jack is always excited about Sunday. It’s his favourite day
when he gets to play all day and go cycling with his friends.
Input:
- A string denoting the day on which the month starts (e.g.,
"mon", "tue", ..., "sun").
- An integer denoting the number of days in the month.
Output:
- An integer representing the number of Sundays that fall
within those days.
Constraints:
- The day string will be in lowercase (or mixed case), e.g.,
"mon", "Sun".
- 1 ≤ number of days ≤ 1000
Example 1:
Input:
mon
13
Output:
1
Explanation:
The month starts on Monday. The first Sunday comes after 6
days, then the next after another 7.
Within 13 days, there will be 1 Sundays.
import java.util.*;
public class sun {
public static int daynumber(String s, int n) {
String arr[] = {"mon", "tue", "wed", "thurs", "fri",
"sat", "sun"};
int count = 0, idx = 0;
return count;
}
Input:
First line: Integer N — number of items
Next N lines: Integer values representing risk levels (0, 1,
or 2)
Output:
Sorted array of risk levels (from lowest to highest)
Example 1:
Input:
7
1
0
2
0
1
0
2
Output:
0 0 0 1 1 2 2
Example 2:
Input:
10
2
1
0
2
1
0
0
1
2
0
Output:
0 0 0 0 1 1 1 2 2 2
Explanation:
The array is sorted by counting the frequency of 0s, 1s, and
2s, and then rebuilding the array in sorted order.
import java.util.*;
public class risk {
public static void riskcalc(int arr[], int n) {
int count0 = 0, count1 = 0, count2 = 0;
for (int i = 0; i < n; i++) {
if (arr[i] == 0) count0++;
else if (arr[i] == 1) count1++;
else count2++;
}
int idx = 0;
while (count0-- > 0) arr[idx++] = 0;
while (count1-- > 0) arr[idx++] = 1;
while (count2-- > 0) arr[idx++] = 2;
}
Input:
- First line: Integer N (size of array)
- Next N lines: Elements of array Arr[0] to Arr[N-1]
Output:
- An integer count of elements greater than all previous
elements.
Example 1:
Input:
5
7
4
8
2
9
Output:
3
Explanation:
Elements 7, 8, and 9 are greater than all their preceding
elements.
Example 2:
Input:
5
3
4
5
8
9
Output:
5
Constraints:
1 <= N <= 20
1 <= Arr[i] <= 10000
import java.util.*;
public class greaternum {
public static int greater(int arr[], int n) {
int max = arr[0], count = 1;
for (int i = 1; i < n; i++) {
if (arr[i] > max) {
max = arr[i];
count++;
}
}
return count;
}
Input:
- A single integer N (the printed code on the product)
Output:
- The price of the item (product of all digits in N)
Example 1:
Input:
5244
Output:
160
Explanation:
Digits of 5244 are 5, 2, 4, 4
Product = 5 × 2 × 4 × 4 = 160
import java.util.*;
public class pricecalc {
public static int price(int n) {
int res = 1;
while (n > 0) {
res *= (n % 10);
n /= 10;
}
return res;
}
Note:
- If the last group has fewer than `L` characters, it is still
considered a valid group.
- You must divide the string into groups of length `L` and
find the group with the highest count of 'a'.
Input:
- First line: a string of only 'a' and 'b' characters (1 ≤ N ≤
50)
- Second line: an integer L (1 ≤ L ≤ 10)
Output:
- A single integer indicating the maximum number of 'a'
curtains in any group.
Example 1:
Input:
bbbaaababa
3
Output:
3
Explanation:
Groups: [bbb], [aaa], [bab], [a]
Group with most 'a's is [aaa] → Output: 3
Example 2:
Input:
abbbaabbb
5
Output:
2
Explanation:
Groups: [abbba], [abbb]
Most 'a's in a group = 2 → Output: 2
import java.util.*;
public class countmax {
public static int maxaqua(String s, int L) {
int mx = 0, count = 0;
int n = s.length();
for (int i = 0; i < n; i++) {
if (i % L == 0) {
mx = Math.max(mx, count);
count = 0;
}
if (s.charAt(i) == 'a') {
count++;
}
}
mx = Math.max(mx, count);
return mx;
}
Constraints:
- 2 ≤ N ≤ 50
Input Format:
- A single integer N (total number of members including the
President and Prime Minister)
Output Format:
- A single integer representing the total number of valid
circular seating arrangements.
Example 1:
Input:
4
Output:
12
Explanation:
- President and PM must sit together → treat them as a pair →
so (N-1) members in circular arrangement.
- Arrangement = 2! × (N-1)!
- 2 × (4-1)! = 2 × 6 = 12
Example 2:
Input:
10
Output:
725760
Explanation:
- 2! × (10-1)! = 2 × 362880 = 725760
import java.util.*;
public class circulartable {
public static int arrangement(int N) {
int ans = 2; // 2! for swapping President and PM
for (int i = 1; i < N; i++) {
ans *= i; // (N - 1)!
}
return ans;
}
Note:
- If R is 0, output should be 0.
- You must reduce the sum to a single-digit number.
Input:
- First line: Integer N (0 < N ≤ 1000)
- Second line: Integer R (0 ≤ R ≤ 50)
Output:
- A single-digit integer
Example 1:
Input:
99
3
Output:
9
Explanation:
Sum of digits of 99 = 9 + 9 = 18
Repeat 3 times: 18 + 18 + 18 = 54
Reduce to single digit: 5 + 4 = 9
Example 2:
Input:
1234
2
Output:
2
Explanation:
Sum of digits of 1234 = 1 + 2 + 3 + 4 = 10
Repeat 2 times: 10 + 10 = 20
Reduce to single digit: 2 + 0 = 2
import java.util.*;
public class cisternways {
public static int sum_of_digits(int N) {
int sum = 0;
while (N > 0) {
sum += N % 10;
N /= 10;
}
return sum;
}
if (R == 0) {
System.out.println(0);
} else {
int sum = sum_of_digits(N) * R;
while (sum >= 10) {
sum = sum_of_digits(sum);
}
System.out.println(sum);
}
}
}
Example 1:
Input:
4
5
2
3
7
12
200
Output:
600
Explanation:
Date = 12 (even), so only vehicles ending in even digits are
allowed.
Violating vehicles: 5, 3, 7 → Total fine = 3 × 200 = 600
Example 2:
Input:
5
2
5
1
6
8
3
300
Output:
900
Explanation:
Date = 3 (odd), so only vehicles ending in odd digits are
allowed.
Violating vehicles: 2, 6, 8 → Total fine = 3 × 300 = 900
import java.util.*;
int D = in.nextInt();
int X = in.nextInt();
System.out.println(fine);
}
}
15. Problem Statement:
Alice and her friends are playing a game of verbal Kho-Kho.
Each of her N friends is seated on a chair, and Alice starts
by passing a paper with a digit to F[1].
Every friend passes the digit silently to the next.
If the final person (F[N]) writes the same digit as Alice’s
original, everyone gets a T-shirt.
Otherwise, Alice counts how many friends **misunderstood or
miscommunicated** the digit.
Input:
- First line: Integer N (number of friends)
- Second line: Array of N integers D (digits each friend
understood)
Output:
- An integer: number of friends who failed to enact or
understand the digit properly
Constraints:
- 1 ≤ N ≤ 1000
- 0 ≤ D[i] ≤ 9
Example 1:
Input:
3
4 4 4
Output:
0
Explanation:
Everyone passed the digit correctly.
Example 2:
Input:
5
1 2 3 2 2
Output:
4
Explanation:
Only the last two friends agreed on the digit. First 4 friends
either enacted or understood incorrectly.
import java.util.*;
public class KhoKho {
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
int N = in.nextInt();
int[] arr = new int[N];
Input:
- First line: Two integers N (number of horses) and K (reward
amount)
- Second line: N integers representing the betting price for
each horse
Output:
- An integer denoting the maximum length of a continuous
sequence of horses such that total price < K
Constraints:
- 2 ≤ N ≤ 10⁵
- 1 ≤ K ≤ 10⁹
- 1 ≤ A[i] ≤ 10⁹
Example 1:
Input:
10 100
30 40 50 20 20 10 90 10 10 10
Output:
3
Explanation:
Sequences like [50 20 20] and [10 10 10] have a sum < 100 and
length 3. No longer valid sequences exist.
Example 2:
Input:
10 100
10 90 80 20 90 60 40 60 70 75
Output:
1
Explanation:
No two or more consecutive prices sum to less than 100. So
maximum length is 1.
import java.util.*;
System.out.println(max);
}
}
17. Tina was given a piece of silk cloth. There are
already a few points or coordinates mentioned on it. She
is creating something which needs an exact square shape
cloth. Help Tina to find out how many minimum points she
can add, to cut the perfect square from the given cloth.
There are already a few points marked on one cloth, Tina
has to include the most number of points from the given
points and should try to include the minimum number of
points or coordinates from her side.
Find the minimum number of coordinates to achieve the perfect
square. Also, try to get as bigger cloth as possible.
Let us try to understand it with an example:
Let’s say there are 3 points given which means N = 3. These
points are:
0 0
2 2
3 3
We can have 2 additional coordinates:
(2,0) & (0,2)
OR
(3,0) & (0,3)
Tina doesn’t want to lose a good deal here, so better if she
would go with (3,0) & (0,3). Hence 2 additional coordinates
were required to cut a perfect square from that piece of
cloth.
Hence the answer is 2.
Example 1:
Input:
5
0 0
100 100
200 200
100 0
0 100
Output:
0
Explanation:
In the above scenario, we can already make a square from the
given coordinates:
P1: (0, 0)
P2: (100, 0)
P3: (0, 100)
P4: (100, 100)
Hence no need for any additional coordinates here. So the
answer is 0.
Example 2:
Input:
3
0 0
2 2
3 3
Output:
2
Explanation:
In the above scenario, we can have 2 additional coordinates:
(2,0) & (0,2) OR (3,0) & (0,3). Tina doesn’t want to lose a
good deal here so better if she would go with (3,0), (0,3).
Hence 2 additional coordinates were required to cut a perfect
square from that piece of cloth. Hence the answer is 2.
import java.util.*;
public class coordinate {
public static int pointcalc(int points[][], int n) {
Set<String> pointset = new HashSet<>();
for (int p[] : points) {
pointset.add(p[0] + "," + p[1]);
}
int minneeded = 4;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (i == j) continue;
int x1 = points[i][0], y1 = points[i][1];
int x2 = points[j][0], y2 = points[j][1];
int dx = x2 - x1;
int dy = y2 - y1;
int[][] rotations = {{-dy, dx}, {dy, -dx}};
for (int rot[] : rotations) {
int rx = rot[0];
int ry = rot[1];
int x3 = x1 + rx;
int y3 = y1 + ry;
int x4 = x2 + rx;
int y4 = y2 + ry;
int count = 2;
if (pointset.contains(x3 + "," + y3)) count++;
if (pointset.contains(x4 + "," + y4)) count++;
int needed = 4 - count;
minneeded = Math.min(minneeded, needed);
}
}
}
return minneeded;
}
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
int N = in.nextInt();
int[][] arr = new int[N][2];
for (int i = 0; i < N; i++) {
arr[i][0] = in.nextInt();
arr[i][1] = in.nextInt();
}
System.out.println(pointcalc(arr, N));
}
}
import java.util.*;
public class order {
public static int ordernum(int arr[], int B[], int n) {
int count = 0;
int[] original = arr.clone();
int[] current = arr.clone();
while (true) {
count++;
int[] next = new int[n];
for (int i = 0; i < n; i++) {
next[i] = current[B[i] - 1];
}
if (Arrays.equals(next, original)) {
break;
}
current = next;
}
return count;
}
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
int N = in.nextInt();
int[] arr = new int[N];
int[] B = new int[N];
for (int i = 0; i < N; i++) {
B[i] = in.nextInt();
arr[i] = i + 1;
}
System.out.println(ordernum(arr, B, N));
}
}