0% found this document useful (0 votes)
14 views10 pages

15th March Shift 1 PDF

The document contains two programming problems: one about making three integers equal through specific operations, and another about calculating the sum of integers between two indices. Each problem includes input and output specifications, constraints, and example cases. Additionally, there are code implementations in C++, Python, and Java for both problems.

Uploaded by

62 Viraj Shelke
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views10 pages

15th March Shift 1 PDF

The document contains two programming problems: one about making three integers equal through specific operations, and another about calculating the sum of integers between two indices. Each problem includes input and output specifications, constraints, and example cases. Additionally, there are code implementations in C++, Python, and Java for both problems.

Uploaded by

62 Viraj Shelke
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

TCS NQT 2025

15th march shift 1


Ques 5: Problem Statement: Making Triplets Equal
You are given a triplet of integers (a,b,c)(a, b, c). You can perform the following operation any
number of times:
1. Select any two numbers from the triplet.
2. Add 1 to both selected numbers.
3. Subtract 1 from the remaining number.
Your task is to determine whether it is possible to make all three numbers equal using the
given operations.
Input:
A single integer TT representing the number of test cases.
Each test case consists of three integers a,b,ca, b, c.
Output:
For each test case, print "YES" if it is possible to make all three numbers equal; otherwise,
print "NO".
Constraints:

Example:
Input:
3
123
444
337
Output:
-1
0
2
C++ #include <iostream>
#include <algorithm>
using namespace std;

int min_steps_to_equal(int P, int Q, int R) {


int arr[3] = {P, Q, R};
sort(arr, arr + 3);

if (arr[0] == arr[1] && arr[1] == arr[2])


return 0;

int steps = 0;
while (true) {
arr[0] += 1;
arr[1] += 1;
arr[2] -= 1;
steps++;
sort(arr, arr + 3);

if (arr[0] == arr[1] && arr[1] == arr[2])


return steps;

if ((arr[0] == arr[1] && arr[1] + 1 == arr[2]) ||


(arr[1] == arr[2] && arr[0] + 1 == arr[1])) {
return -1;
}
}
}

int main() {
int T;
cin >> T; // Number of test cases
while (T--) {
int P, Q, R;
cin >> P >> Q >> R;
cout << min_steps_to_equal(P, Q, R) << endl;
}
return 0;
}
PYTHON def min_steps_to_equal(P, Q, R):
arr = [P, Q, R]
arr.sort()

if arr[0] == arr[1] == arr[2]:


return 0

steps = 0
while True:
arr[0] += 1
arr[1] += 1
arr[2] -= 1
steps += 1
arr.sort()

if arr[0] == arr[1] == arr[2]:


return steps

if (arr[0] == arr[1] and arr[1] + 1 == arr[2]) or \


(arr[1] == arr[2] and arr[0] + 1 == arr[1]):
return -1

# Input handling
T = int(input()) # Number of test cases
for _ in range(T):
P, Q, R = map(int, input().split())
print(min_steps_to_equal(P, Q, R))
JAVA import java.util.Arrays;
import java.util.Scanner;

public class Main {


public static int minStepsToEqual(int P, int Q, int R) {
int[] arr = {P, Q, R};
Arrays.sort(arr);

if (arr[0] == arr[1] && arr[1] == arr[2]) {


return 0;
}

int steps = 0;
while (true) {
arr[0] += 1;
arr[1] += 1;
arr[2] -= 1;
steps++;
Arrays.sort(arr);

if (arr[0] == arr[1] && arr[1] == arr[2]) {


return steps;
}

if ((arr[0] == arr[1] && arr[1] + 1 == arr[2]) ||


(arr[1] == arr[2] && arr[0] + 1 == arr[1])) {
return -1;
}
}
}

public static void main(String[] args) {


Scanner sc = new Scanner(System.in);
int T = sc.nextInt(); // Number of test cases

while (T-- > 0) {


int P = sc.nextInt();
int Q = sc.nextInt();
int R = sc.nextInt();
System.out.println(minStepsToEqual(P, Q, R));
}
sc.close();
}
}
Ques 2:
Problem Statement: Range Sum Query
You are given two integers i and j, where 0≤i<j≤9999. Your task is to compute the sum of all
integers from index i to j, inclusive.
Input Format:
A single integer T representing the number of queries.
Each of the next T lines contains two integers i and j (0≤i<j≤9999)
Output Format:
For each query, print a single integer representing the sum of numbers from i to j.
Constraints:

Example Input:
3
03
26
10 1001
Example Output:
6 20 invalid input i&j i <=j<10000
Explanation:
1. Sum from 0 to 3: 0+1+2+3=6
2. Sum from 2 to 6: 2+3+4+5+6=20
3. Sum from 10 to 20: 10+11+⋯+20=165
MASSIVE SUCCESS RATE
"Transform Your Interview Opportunity into an Offer Letter and Make Your Parents
Proud!"
In-depth Technical Mock
Crack coding challenges with real experts.
HR & Managerial Prep
Master behavioral questions and impress TCS
Interviewer.
Full Interview Simulation
Ace both technical and HR in one session.
Resume Review
Identify and fix weaknesses for a standout CV.
Personalized Feedback & Expert Guidance
Tailored improvement tips to boost success.

www.primecoding.in
WWW.PRIMECODING.IN
JAVA import java.util.ArrayList;
import java.util.Scanner;
import java.util.Scanner;

public class Main {


class MyClass{
public static int rangeSum(int i, int j) {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in); return (j * (j + 1) / 2) - (i * (i - 1) / 2);
ArrayList<Integer> arr = new ArrayList<>(); }
String str = sc.nextLine();
boolean flag = false; public static void main(String[] args) {
StringBuilder sb = new StringBuilder(); Scanner sc = new Scanner(System.in);
for(int i=0; i<str.length(); i++) {
int T = sc.nextInt();
if(str.charAt(i) == '-') {
System.out.print("Invalid, out of range"); sc.nextLine();
flag = true;
break; for (int t = 0; t < T; t++) {
} String userInput = sc.nextLine().trim();
if(Character.isDigit(str.charAt(i))) { String[] values = userInput.split(" ");
sb.append(str.charAt(i));
} else if(sb.length() > 0) {
int temp = Integer.parseInt(sb.toString());
if (values.length < 2) {
if(temp < 0 || temp > 10000) { System.out.println("Invalid input i&j i <= j < 10000");
System.out.print("Invalid, out of range"); continue;
flag = true; }
break;
}
try {
arr.add(temp);
int i = Integer.parseInt(values[0]);
sb.setLength(0);
} int j = Integer.parseInt(values[1]);
}
if(sb.length()>0) { if (i >= j || i < 0 || j >= 10000) {
arr.add(Integer.parseInt(sb.toString())); System.out.println("Invalid input i&j i <= j < 10000");
} } else {
System.out.print(rangeSum(i, j) + " ");
if(!flag) {
if(arr.size() < 2) {
}
System.out.print("Invalid more than one input needed"); } catch (NumberFormatException e) {
} else { System.out.println("Invalid input i&j i <= j < 10000");
for(int i=0; i<arr.size(); i+=2) { }
int ans = 0; }
for(int j=arr.get(i); j<arr.get(i+1); j++) {
ans+=j;
sc.close();
}
System.out.print(ans + " "); }
} }
}
}
}
}
C++
#include <iostream>
#include <sstream>
using namespace std;

int rangeSum(int i, int j) {


return (j * (j + 1) / 2) - (i * (i - 1) / 2);
}

int main() {
int T;
cin >> T;
cin.ignore();

for (int t = 0; t < T; ++t) {


string userInput;
getline(cin, userInput);
stringstream ss(userInput);

int i, j;
if (!(ss >> i >> j)) {
cout << "Invalid input i&j i <= j < 10000" << endl;
continue;
}

if (i >= j || i < 0 || j >= 10000) {


cout << "Invalid input i&j i <= j < 10000" << endl;
} else {
cout << rangeSum(i, j) << " ";
}
}

return 0;
}
PYTHON
def range_sum(i, j):
return (j * (j + 1) // 2) - (i * (i - 1) // 2)

T = int(input())

for _ in range(T):
user_input = input()
values = user_input.split()
if len(values) < 2:
print("Invalid input i&j i <= j < 10000")
continue

i, j = map(int, values)

if i >= j or i < 0 or j >= 10000:


print("invalid input i&j i <= j < 10000")
else:
print(range_sum(i, j), end = " ")

You might also like