Problem statement 1
A new automobile company manufactures both two-wheeler (TW) and four-wheeler (FW). Unfortunately, there is no monitoring system placed to count the number of two-wheelers and the number of four-wheelers separately but there is a system that counts the total number of vehicles produced per day. There is also a system that counts the number of tyre manufactured a day. Given the number of vehicles manufactured and the number of wheels consumed, print the number of two-wheelers and the number of four-wheelers the company has produced on the given day.
Sample input:
- First line: A single, non-negative integer value V that denotes the number of vehicles manufactured the given day.
- Second line: A single, non-negative integer value W that denotes the number of tyres consumed to manufacture V vehicles.
Sample output:
- First line: Number of two-wheelers.
- Second line: Number of four-wheelers.
Constraints:
2 <= W
W % 2=0
V < W
- Print “INVALID INPUT”, if inputs did not meet the constraints.
Example
- Input :
200
540
- Output :
130
70
- Explanation:
- 130+70 = 200 vehicles
- (704)+(1302)= 540 wheels
Program in C
#include <stdio.h>
int main() {
int vehicles, wheels;
scanf("%d%d", &vehicles, &wheels);
if (wheels & 1 || wheels < 2 || wheels <= vehicles) {
printf("INVALID INPUT");
return 0;
}
int wheels_cars = vehicles * 4; // Number of expected wheels if all given vehicles are to be cars.
int surplus_wheels = wheels_cars - wheels; // If all the vehicles were to be cars,
// number of extra wheels that would be required more than given wheels.
int no_of_bikes = surplus_wheels / 2;
int no_of_cars = vehicles - no_of_bikes;
printf("%d\n%d", no_of_bikes, no_of_cars);
return 0;
}
Program in C++
#include <iostream>
int main() {
int vehicles, wheels;
std::cin >> vehicles >> wheels;
if (wheels & 1 || wheels < 2 || wheels <= vehicles) {
std::cout << "INVALID INPUT";
return 0;
}
int wheels_cars = vehicles * 4; // Number of expected wheels if all given vehicles are to be cars.
int surplus_wheels = wheels_cars - wheels; // If all the vehicles were to be cars,
// number of extra wheels that would be required more than given wheels.
int no_of_bikes = surplus_wheels / 2;
int no_of_cars = vehicles - no_of_bikes;
std::cout << no_of_bikes << std::endl << no_of_cars;
return 0;
}
Program in Java
import java.util.Scanner;
class Main {
public static void main (String args[]) {
Scanner in = new Scanner(System.in);
int vehicles = in.nextInt();
int wheels = in.nextInt();
in.close();
if (wheels % 2 == 1 || wheels < 2 || wheels <= vehicles) {
System.out.print("INVALID INPUT");
return;
}
int wheels_cars = vehicles * 4; // Number of expected wheels if all given vehicles are to be cars.
int surplus_wheels = wheels_cars - wheels; // If all the vehicles were to be cars,
// number of extra wheels that would be required more than given wheels.
int no_of_bikes = surplus_wheels / 2;
int no_of_cars = vehicles - no_of_bikes;
System.out.print(no_of_bikes + "\n" + no_of_cars);
}
}
Program in Python
if __name__ == "__main__":
vehicles = int(input())
wheels = int(input())
if (wheels & 1 == 1 or wheels < 2 or wheels <= vehicles):
print("INVALID INPUT")
exit(0)
wheels_cars = vehicles * 4; # Number of expected wheels if all given vehicles are to be cars.
surplus_wheels = wheels_cars - wheels; # If all the vehicles were to be cars,
# number of extra wheels that would be required more than given wheels.
no_of_bikes = surplus_wheels // 2
no_of_cars = vehicles - no_of_bikes
print(int(no_of_bikes))
print(int(no_of_cars), end='')
Problem statement 2
A chocolate factory is packing chocolates into the packets. Each packet can contain 1 or more chocolates. Recently the customer service has been getting complaints from the customers that the factory is dispatching empty chocolate packets. Your job is to identify the empty packets and push them to the end of the conveyer belt. You are given an array of N integer values denoting the number of chocolates in the packets in the same order as they are arriving on the conveyer belt. The order of the rest of the packets should be preserved.
Input format:
- First line: A non-negative integer N denoting the number of packets on the conveyer belt
- Second line: N space-separated integers denoting the number of chocolates present in each of those packets.
Output format:
- First line: N space-separated integers in the order described in the problem statement.
Constraints:
1 <= N <= 10^5
Examples
- Input – 1:
7
4 5 0 1 9 0 5 0
- Output – 1:
4 5 1 9 5 0 0 0
- Input – 2:
7
4 5 0 1 0 0 5
- Output – 2:
4 5 1 5 0 0 0
Program in C
#include <stdio.h>
#include <string.h>
int main() {
unsigned int n, non_zero_size = 0;
scanf("%d", &n);
int *array = (int *) calloc (n, sizeof(int));
memset(array, 0, n);
for (unsigned int i = 0, j = 0 ; i < n; i++) {
scanf("%d", &j);
if (j != 0)
array[non_zero_size++] = j;
}
for (unsigned int i = 0 ; i < n; i++)
printf("%d ", array[i]);
return 0;
}
Program in C without using the array
#include <stdio.h>
int main() {
unsigned int n, zeroes = 0;
scanf("%d", &n);
for (unsigned int i = 0, j = 0 ; i < n; i++) {
scanf("%d", &j);
if (j != 0)
printf("%d ", j);
else
zeroes++;
}
for (unsigned int i = 0 ; i < zeroes; i++)
printf("0 ");
return 0;
}
Program in C++
#include <iostream>
int main() {
unsigned int n{0}, non_zero_size{0};
std::cin >> n;
int array[n] = {0};
for (unsigned int i = 0, j = 0 ; i < n; i++) {
std::cin >> j;
if (j != 0)
array[non_zero_size++] = j;
}
for (unsigned int i = 0 ; i < n; i++)
std::cout << array[i] << " ";
return 0;
}
Program in Java
import java.util.Scanner;
class Main {
public static void main (String args[]) {
Scanner in = new Scanner(System.in);
int N = in.nextInt();
int zero_count = 0, inp;
for (int i = 0 ; i < N ; i++) {
inp = in.nextInt();
if (inp == 0)
zero_count++;
else
System.out.print(inp + " ");
}
in.close();
for (int i = 0 ; i < zero_count ; i++)
System.out.print("0 ");
}
}
Program in Python
if __name__ == "__main__":
N = int(input())
zero_count = 0
for i in input().split():
if i == '0':
zero_count += 1
else:
print(i, end = ' ')
for i in range(zero_count):
print('0', end = ' ')
Pingback: TCS NQT PREVIOUS YEAR PAPERS - GRAD JOB OPENINGS