Tcs NQT 2025 03-04-05 October Coding
Tcs NQT 2025 03-04-05 October Coding
Q1. Find the Total minutes of exercise done and it’s average for a week
Input:
Solution –
C++ code
#include <bits/stdc++.h>
Int main() {
Int duration, sum = 0;
Java –
Import java.util.Scanner;
Duration = scanner.nextInt();
Sum += duration;
Scanner.close();
}
The time complexity is O(n) due to the loop running n times, while the space complexity
is O(1) as only constant memory is used.
Q2. (print the total number of palindrome between the given range m and
n,0<=m,n<=1000)
Input: 0 20
Output: 11
Reason: 0,1,2,3,4,5,6,7,8,9,11
Solution –
C++
#include <bits/stdc++.h>
Using namespace std;
Bool is_palindrome(int n) {
Int original = n, reversed = 0;
While (n > 0) {
N /= 10;
}
Return original == reversed;
Int main() {
Int m, n, count = 0;
Return 0;
Java code –
Import java.util.Scanner;
While (n > 0) {
Int m = scanner.nextInt();
Int n = scanner.nextInt();
Int count = 0;
If (isPalindrome(i)) {
Count++;
Scanner.close();
}
}
The time complexity is O(k * log n), where k is the range size and log n is the number of
digits in the largest number, while the space complexity is O(1) since no additional data
structures are used.
Input: 28
Output: True (28 is a perfect number)
Solution:
C++ Code-
#include <iostream>
bool isPerfect(int n) {
if (n <= 1) return false;
int sum = 1;
sum += i;
if (i!= n/i) {
sum += n/i;
}}
}
return sum == n;
}
int main() {
int n;
cin >> n;
if (isPerfect(n)) {
cout << "True" << endl; }
else {
return 0;
JAVA code –
import java.util.Scanner;
int sum = 1;
sum += i;
if (i != n / i) {
sum += n / i;
} }
return sum == n;
}
public static void main(String[] args) {
int n = scanner.nextInt();
System.out.println(isPerfect(n));
}
Question:
Given a space-separated string of words, write a function to count the frequency of each
word in the string. The output should display each unique word followed by its
frequency, with the words in the order of their first appearance. The output should
capitalize the first letter of each word.
Example:
Input:
Output:
Solution :
C++ Code –
#include <bits/stdc++.h>
stringstream ss(input_string);
string word;
word[0] = toupper(word[0]);
if (frequency.find(word) == frequency.end()) {
words_in_order.push_back(word);
frequency[word]++;
int main() {
string input_string;
getline(cin, input_string);
count_word_frequencies(input_string);
return 0;
Java Code –
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
if (word.chars().allMatch(Character::isLetter)) {
countWordFrequencies(inputString);
scanner.close();
}
QUESTION:
You are giving an integer like 123 Return true if sum of the digits is multiple of 3
INPUT:
123
OUTPUT:
true/True
Solution:
C++ Code –
#include <bits/stdc++.h>
int main() {
int n;
cin >> n;
int sum = 0;
while (n > 0) {
sum += digit;
n /= 10;
}
if (sum % 3 == 0)
else
cout << "False" << endl;
return 0;
Java Code –
import java.util.Scanner;
int sum = 0;
while (n > 0) {
sum += digit;
QUESTION:
Given an integer array nums and an integer k, return the number of pairs (i, j) where i < j
such that nums[i] - nums[j]] == k.
INPUT
12211
OUTPUT
Solution:
C++ Code –
#include <bits/stdc++.h>
return store;
if (store.find(num + k) != store.end()) {
}
}
return ans;
int main() {
vector<int> arr;
string line;
getline(cin, line);
stringstream ss(line);
int ele;
arr.push_back(ele);
int k;
cin >> k;
return 0;
Java Code –
import java.util.*;
return store;
int ans = 0;
if (store.containsKey(num + k)) {
return ans;
arr.add(lineScanner.nextInt());
lineScanner.close();
scanner.close();