TCS NQT Problem Statements

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 9

TCS NQT PROBLEM STATEMENTS:

Problem Statement 1

An automobile company manufactures both a two wheeler (TW) and a four wheeler (FW). A
company manager wants to make the production of both types of vehicle according to the
given data below:

 1st data, Total number of vehicle (two-wheeler + four-wheeler)=v


 2nd data, Total number of wheels = W

The task is to find how many two-wheelers as well as four-wheelers need to manufacture as
per the given data.
Example :

Input :
200 -> Value of V
540 -> Value of W

Output :
TW =130 FW=70

Explanation:
130+70 = 200 vehicles
(70*4)+(130*2)= 540 wheels

Constraints :

 2<=W
 W%2=0
 V<W

Print “INVALID INPUT” , if inputs did not meet the constraints.

The input format for testing


The candidate has to write the code to accept two positive numbers separated by a new line.
 First Input line – Accept value of V.
 Second Input line- Accept value for W.

The output format for testing

 Written program code should generate two outputs, each separated by a single space
character(see the example)
 Additional messages in the output will result in the failure of test case

Solution in C:

#include <stdio.h>
int main() {
int v, w, tw, fw;
// Accepting input from the user
scanf("%d", &v);
scanf("%d", &w);
// Checking constraints
if (w < 2 || w % 2 != 0 || v >= w) {
printf("INVALID INPUT");
} else {
// Calculating the number of two-wheelers and four-wheelers
fw = w / 4;
tw = v - fw;
// Printing the output
printf("TW=%d FW=%d", tw, fw);
}
return 0;
}
Solution in python:

# Accepting input from the user


v = int(input())
w = int(input())
# Checking constraints
if w < 2 or w % 2 != 0 or v >= w:
print("INVALID INPUT")
else:
# Calculating the number of two-wheelers and four-wheelers
fw = w // 4
tw = v - fw
# Printing the output
print(f"TW={tw} FW={fw}")

Control Statements:

Creatnx now wants to decorate his house by flower pots. He plans to buy exactly N
ones. He can only buy them from Triracle's shop. There are only two kind of
flower pots available in that shop. The shop is very strange. If you buy X flower
pots of kind 1 then you must pay A x X^2 and B x Y^2? if you buy Y flower pots
of kind 2. Please help Creatnx buys exactly N flower pots that minimizes money he
pays.
Input Format

The first line contains a integer T denoting the number of test cases.

Each of test case is described in a single line containing three space-separated


integers N, A, B.

Constraints

•1<T < 10^5

•1<N, A, B < 10^5

Output Format

For each test case, print a single line containing the answer.

Sample Input;

512

10 2 4

Sample Output:

17

134

Python Code:

# Number of test cases


T = int(input())

# Loop through each test case


for _ in range(T):
# Input for N, A, B
N, A, B = map(int, input().split())

# Calculate the minimum cost using a formula based approach


# Calculate the optimal value of x using the formula x = (N * B) / (A + B)
x = (N * B) // (A + B)
y=N-x

# Calculate the cost using the optimal values of x and y


min_cost = A * (x ** 2) + B * (y ** 2)

# Output the minimum cost for the current test case


print(min_cost)

# Number of test cases


T = int(input())
# Loop through each test case
for _ in range(T):
# Input for N, A, B
N, A, B = map(int, input().split())
# Calculate the minimum cost using the formula A*x^2 + B*y^2
# where x + y = N and x, y >= 0
min_cost = float('inf')
for x in range(N + 1):
y=N-x
cost = A * (x ** 2) + B * (y ** 2)
min_cost = min(min_cost, cost)
# Output the minimum cost for the current test case
print(min_cost)

C Code:

#include <stdio.h>
int main() {
int T; // Number of test cases
scanf("%d", &T);

// Loop through each test case


while (T--) {
int N, A, B;
// Input for N, A, B
scanf("%d %d %d", &N, &A, &B);

int min_cost = __INT_MAX__;


// Calculate the minimum cost using the formula A*x^2 + B*y^2
// where x + y = N and x, y >= 0
for (int x = 0; x <= N; x++) {
int y = N - x;
int cost = A * (x * x) + B * (y * y);
min_cost = (cost < min_cost) ? cost : min_cost;
}

// Output the minimum cost for the current test case


printf("%d\n", min_cost);
}

return 0;
}

Problem statement

Pooja would like to withdraw


X USfromanATM.ThecashmachinewillonlyacceptthetransactionifXi
samultipleof5,andPooja
′saccountbalancehasenoughcashtoperformthewithdrawaltransactio
n(includingbankcharges).Foreachsuccessfulwithdrawalthebankcha
rges0.50US. 0.50USfromanATM.Thecashmachinewillonlyacceptthet
ransactionifXisamultipleof5,andPooja
′saccountbalancehasenoughcashtoperformthewithdrawaltransactio
n(includingbankcharges).Foreachsuccessfulwithdrawalthebankcha
rges0.50US. Calculate Pooja's account balance after an attempted transaction.
Input Format

Positive integer 0 < X <= 2000 - the amount of cash which Pooja wishes to
withdraw.

Nonnegative number 0<= Y <= 2000 with two digits of precision - Pooja's initial
account balance.

Output Format

Output the account balance after the attempted transaction, given as a number with
two digits of precision. If there is not enough money in the account to complete the
transaction, output the current bank balance.

Sample Input:
30 120.00

Sample Output:

89.50

Python Code:
def main():
X = int(input("Enter the amount you wish to withdraw: "))
Y = float(input("Enter your initial account balance: "))
bank_charge = 0.50

if X % 5 == 0 and X + bank_charge <= Y:


Y -= (X + bank_charge)
print("Transaction successful! Account balance: {:.2f}".format(Y))
else:
print("Transaction failed! Insufficient funds or invalid amount.")
print("Account balance remains: {:.2f}".format(Y))

if __name__ == "__main__":
main()

C Code:

#include <stdio.h>

int main() {
int X;
float Y, bankCharge = 0.50;

printf("Enter the amount you wish to withdraw: ");


scanf("%d", &X);
printf("Enter your initial account balance: ");
scanf("%f", &Y);

if (X % 5 == 0 && X + bankCharge <= Y) {


Y -= (X + bankCharge);
printf("Transaction successful! Account balance: %.2f\n", Y);
} else {
printf("Transaction failed! Insufficient funds or invalid amount.\n");
printf("Account balance remains: %.2f\n", Y);
}

return 0;
}

You might also like