Coding
Coding
the passenger at the security checkpoint. All the items have been
dumped into a huge box(array). Each item possessed a certain
amount of risk(0,1,2). Here is the risk severity of the item
representing an array[] of N number of integer values. The risk
here is to sort the item based on their level of risk values range
from 0 to 2.
Example 2:
Input:
10 ----- Value of N
[2,1,0,2,1,0,0,1,2,0] -> Element of arr[0] to arr[N-1], while input
each element is separated by new line
Output:
0 0 0 0 0 1 1 1 2 2 2 -> Element after sorting based on the risk
severity.
Constraints
0<N<=100
0<=arr[i]<=2
Code Solution:
#include<iostream>
using namespace std;
int main()
{
int n;
cin >> n;
int arr[n];
for(int i = 0; i < n; i++)
{
cin >> arr[i];
}
for(int i = 0; i < n; i++)
{
for(int j = i+1; j < n; j++)
{
if(arr[i] > arr[j])
{
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
for(int i = 0; i < n; i++)
{
cout << arr[i] << “ ”;
}
}
Q2) Given N gold wires, each wire has a length associated with it.
At a time, only two adjacent small wres assembled at the end of a
large wire and the cost of forming is the sum of their length. Find
the minimum cost when all wires are assembled to form a single
wire.
For Example: Suppose, Arr[] = {7, 6, 8, 6, 1, 1}
{7, 6, 8, 6, 1, 1} - {7, 6, 8, 6, 2}, cost = 2
{7, 6, 8, 6, 2} - {7, 6, 8, 8}, cost = 8
{7, 6, 8, 8} - {13, 8, 8}, cost =13
{13, 8, 8} - {13, 16}, cost = 16
{13, 16} - {29}, cost = 29
2 + 8 + 13 + 16 + 29 = 68
Hence, the minimum cost to assemble all gold wires is : 68
Constraints:
1 <= N <= 30
1<= Arr[] <= 100
Example 1:
Input:
Output:
68
Example 2:
Input:
Output:
34
Code Solution:
#include<bits/stdc++.h>
using namespace std;
struct MinHeap
{
unsigned size;
unsigned capacity;
int* harr;
};
struct MinHeap* createMinHeap(unsigned capacity)
{
struct MinHeap* minHeap = new MinHeap;
minHeap->size = 0;
minHeap->capacity = capacity;
minHeap->harr = new int[capacity];
return minHeap;
}
void swapMinHeapNode(int* a, int* b)
{
int temp = *a;
*a = *b;
*b = temp;
}
void minHeapify(struct MinHeap* minHeap, int idx)
{
int smallest = idx;
int left = 2 * idx + 1;
int right = 2 * idx + 2;
if (left < minHeap->size
&& minHeap->harr[left] < minHeap->harr[smallest])
smallest = left;
if (right < minHeap->size
&& minHeap->harr[right] < minHeap->harr[smallest])
smallest = right;
if (smallest != idx) {
swapMinHeapNode(&minHeap->harr[smallest],&minHeap->harr[idx]);
minHeapify(minHeap, smallest);
}
}
int isSizeOne(struct MinHeap* minHeap)
{
return (minHeap->size == 1);
}
int extractMin(struct MinHeap* minHeap)
{
int temp = minHeap->harr[0];
minHeap->harr[0] = minHeap->harr[minHeap->size - 1];
--minHeap->size;
minHeapify(minHeap, 0);
return temp;
}
void insertMinHeap(struct MinHeap* minHeap, int val)
{
++minHeap->size;
int i = minHeap->size - 1;
while (i && (val < minHeap->harr[(i - 1) / 2])) {
minHeap->harr[i] = minHeap->harr[(i - 1) / 2];
i = (i - 1) / 2;
}
minHeap->harr[i] = val;
}
void buildMinHeap(struct MinHeap* minHeap)
{
int n = minHeap->size - 1;
int i;
for (i = (n - 1) / 2; i >= 0; --i)
minHeapify(minHeap, i);
}
struct MinHeap* createAndBuildMinHeap(
int len[], int size)
{
struct MinHeap* minHeap = createMinHeap(size);
for (int i = 0; i < size; ++i)
minHeap->harr[i] = len[i];
minHeap->size = size;
buildMinHeap(minHeap);
return minHeap;
}
int minCost(int len[], int n)
{
int cost = 0;
struct MinHeap* minHeap = createAndBuildMinHeap(len, n);
while (!isSizeOne(minHeap)) {
int min = extractMin(minHeap);
int sec_min = extractMin(minHeap);
cost += (min + sec_min);
insertMinHeap(minHeap, min + sec_min);
}
return cost;
}
int main()
{
int n;
cin >> n;
int arr[n];
for(int i = 0; i < n; i++)
cin >> arr[i];
int size = sizeof(arr) / sizeof(arr[0]);
cout << minCost(arr, size);
return 0;
}
Example 1:
Input:
5 ---> Value of T
[7,0,5,1,3] ---> E[], element of E[0] to E[N-1], where input each
element is separated by new line
[1,2,1,3,4] -----> L[],element of L[0] to L[N-1], where input each
element is separated by new line
Output:
8 -----> Maximum number of guests on cruise at an instance.
Explanation:
Code Solution:
#include <iostream>
using namespace std;
int main()
{
int n, sum = 0,
max; cin >> n;
int a[n], b[n];
for(int i = 0; i < n; i++)
{
cin >> a[i];
}
for(int i = 0; i < n; i++)
{
cin >> b[i];
}
max = sum;
for(int i = 0; i < n; i++)
{
sum = sum + a[i] - b[i];
if(max < sum)
max = sum;
}
cout << max;
}
Input:
Explanation:
Code Solution:
#include <iostream>
using namespace std;
int main()
{
int n, count=0;
cin>>n;
int B[n];
for(int i=0; i<n; i++)
cin>>B[i];
for(int i=0; i<n; i++)
{
if(B[i]==0)
{
for(int k=i+1; k<n; k++)
{
if(B[k]==1)
count++;
}
}
}
cout<<count;
}
Example 1:
Input:
5244 -->Value of N
Output:
160 -->Price
Explanation:
From the input above:
Product of the digits: 5,1,4,4
5*2*4*4 = 160
Hence Output is 160
Code Solution:
#include <iostream>
using namespace std;
int main()
{
int n, rem, mul = 1;
cin >> n;
while(n!=0)
{
rem = n%10;
mul = mul * rem;
n = n/10;
}
cout << mul;
}
Example 1:
Input:
4523EF58G -> Value of STR1
Output:
452358 -> After removal of characters
‘EF’ and ‘G’
Example 2:
Input:
E12F35G58 -> Value of STR1
Output:
E12F3558 -> After removal of character ‘G’
Explanation:
The Input format for testing The candidate has to write the code
to accept 1 input(s). First input - Accept value for str1 which is a
string consisting of numbers and uppercase alphabets without
any spaces.
Constraints:
Str={(A,Z),(0-9)}
No spaces and special characters allowed.
Only uppercase alphabets in the input string
Code Solution:
#include<iostream>
#include<string.h>
using namespace std;
int main()
{
string T, F;
cin>>T;
int len = T.length();
int j=0;
for(int i=0; i<len; i++)
{
if((T[i]=='E'&&T[i+1]=='F') || (T[i]=='5'&&T[i+1]=='6'))
{
i++;
continue;
}
else if(T[i]=='G' || T[i]=='7')
{
continue;
}
else
{
F[j]=T[i];
j++;
}
}
for(int i=0; i<j; i++)
cout<<F[i];
}
Note:
Output:
9 → maximum number of equal squares or rectangles
Explanation:
Solution 2
Maximum number of squares/rectangles that can be obtained
with N=4 is 9(Solution 1)
Hence, output is 9
Example 2:
Input:
1 → Value of N(No. of teams)
Output:
2 → maximum number of equal squares or rectangles
Code Solution:
#include <bits/stdc++.h>
using namespace std;
int findMaximumPieces(int n)
{
int x = n / 2;
return ((x + 1) * (n - x + 1));
}
int main()
{
int n;
cin>>n;
cout <<findMaximumPieces(n);
return 0;
}
Q8) A family is about to break their piggy bank to use the money
for different purposes. The piggy bank here represents an array
(arr[]) consisting of N coins. The family has to split the coins of
piggy bank into smaller stack (sub-array) of coins such that the
sum of the difference between the maximum value and the
minimum value of the coins for all the stacks (sub-arrays) is
maximum.
Note: Each value of the array can be used only once that is only
in one subarray.
Constraints:
1 <= N <= 500
1 <=arr[i] <= 100
Example 1:
Input:
5 → Value of N
{8,1,7,9,2} → arr[] elements from arr[0] to arr [N-1],
Where each element is separated by new line.
Output:
14
Explanation:
Let us break the array elements into following subarrays:
1. (8,1) → Max:8 Min:1
2. (7,9,2) → Max:9 Min:2
So, the difference between the maximum and minimum
elements in each subarrays is
1. 8-1=7
2.9-2=7
So, the difference between the max and min elements in each
subarray is
1. 2-1 = 1
2. 5-0 = 5
The input format for testing The candidate has to write the code
to accept 2 inputs.
First input - Accept value for N(positive integer number)
Second input - Accept N number of values (arr[]), where each
value is separated by a new line.
Code solution:
#include<iostream>
using namespace std;
int main()
{
int n, sum = 0;
cin >> n;
int arr[n];
for(int i = 0; i < n; i++)
{
cin >> arr[i];
}
for(int i = 0; i < n; i++)
{
for(int j = i+1; j < n; j++)
{
if(arr[i] < arr[j])
{
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
Example 1:
Input
3 -> Value of N, represents size of Arr
2 -> Value of Arr[0]
2-> Value of Arr[1]
4-> Value of Arr[2]
Output
9
Explanation:
As two elements have the same value, max value for the one of
them needs to be incremented to 3.
He can set the array with 2+3+4=9
Example 2:
Input
2 -> Value of N, represents size of Arr
3 -> Value of Arr[0]
4-> Value of Arr[1]
5-> Value of Arr[2]
Output
Wrong Input
Explanation:
Here N=2, so we need to provide value of only two elements but
we are providing value of three elements so result is “Wrong
Input”
Code Solution:
#include<iostream>
using namespace std;
int main()
{
int n, sum = 0, flag = 0;
cin >> n;
int arr[n];
for(int i = 0; i < n; i++)
{
cin >> arr[i];
}
for(int i = 0; i < n; i++)
{
for(int j = i+1; j < n; j++)
{
if(arr[i] == arr[j])
{
arr[i] = arr[i]+1;
}
if(arr[j] < 0)
flag = 1;
}
}
for(int i = 0; i < n; i++)
{
sum = sum + arr[i];
}
if(flag == 1)
cout << "Wrong Input";
else
cout << sum;
}
Q10) Joseph is learning digital logic subject which will be for his
next semester. He usually tries to solve unit assignment
problems before the lecture. Today, he got one tricky question.
The problem statement is “A positive integer has been given as
an input. Convert decimal value to binary representation. Toggle
all bits of it after the most significant bit including the most
significant bit. Print the positive integer value after toggling all
bits”.
Constraints
1 <=N <=100
Example 1
Input:
10 ---> Integer
Output:
5 → result - Integer
Explanation:
Binary representation of 10 is 1010. After toggling the bits (1010),
will get 0101, which represents “5”.
Hence the output will print “5”.
Example 2
Input:
101 ---> Integer
Output:
Wrong input → result - String
Explanation:
Given integer “101” is out of range. Hence the output will print
“Wrong input”.
The input format for testing The candidate has to write the code
to accept one input(s).
1. First Input - First line contains an integer
Instructions
1. The system doesn’t allow any kind of hard-coded input value.
2. Written program code by the candidate will be verified against
all the inputs which are supplied from the system
Code Solution:
#include<iostream>
using namespace std;
void toggle(int &n)
{
int temp = 1;
while (temp <= n)
{
n = n ^ temp;
temp = temp << 1;
}
}
int main()
{
int n;
cin >> n;
if(n <= 100)
{
toggle(n);
cout << n;
}
else
cout << "Wrong Input";
return 0;
}
________________________________________________________
Output:
[3,4]
[3,4,-7,1,3,3]
[1,3,3]
[3,3,1]
Code :-
#include <iostream>
#include <vector>
#include <unordered_map>
using namespace std;
int main() {
vector<int> arr = {3, 4, -7, 1, 3, 3, 1, -4};
int N = arr.size();
int target = 7;
optimalApproach(arr, N, target);
return 0;
}
The test cases are generated so that the answer will be less than
or equal to 2=10^9
Code:-
#include <bits/stdc++.h>
using namespace std;
class Solution {
private:
int f(int i, int j, vector<vector<int>> &dp){
if(i == 0 && j == 0) return 1;
if(i < 0 || j < 0) return 0;
if(dp[i][j] != -1) return dp[i][j];
Q13) Given two Integer, find sum of cubes all numbers in the
range of n&m .
Input n= 4 ,m = 9
Output = 1989
Code:-
#include <iostream>
int main() {
int start, end;
cin >> start >> end;
findCubeSum(start, end);
return 0;
}
Input :
Apple[1.0,5]
Orange[10.0,5]
Apple[10.0,5]
Output:
Apple
105
35.0
Code:-
#include <iostream>
#include <string>
#include <map>
int main() {
int n;
cin >> n;
// Method 1
int maxCost = 0;
string maxCostItem = "";
double totalPrice = 0;
double avg = 0;
// Method 2
map<string, double> store;
maxCost = 0;
maxCostItem = "";
totalPrice = 0;
double total = 0;
for (auto& pair : store) {
total += pair.second;
}
avg = total / n;
cout << "\nTask 2 - Item: " << maxCostItem << "\nTotal price: " <<
fixed << total << "\nAverage Price: " << avg << endl;
return 0;
}
Input: 10
output: 550
Code:
#include <iostream>
int main() {
int n;
cin >> n;
int mSum = 0;
for (int i = 1; i <= n; i++) {
mSum += n * i;
}
cout << mSum << endl;
return 0;
}
Input -247163
K=3
Output -7776
Code:-
#include <iostream>
#include <vector>
#include <queue>
int main() {
vector<int> arr;
int num;
while (cin >> num) {
arr.push_back(num);
if (cin.get() == '\n') break;
}
int k;
cin >> k;
solve(arr, k);
optimalSolution(arr, k);
return 0;
}
Note,
fib(0)=1
fib(1) = 1
Input
n=5
Output : 7
Code:-
#include <iostream>
using namespace std;
int fibonacci(int n) {
int a = 0, b = 1, sum=1;
if (n < 0) {
cout << "Incorrect input" << endl;
return -1; // or any error code you prefer
} else if (n == 0) {
return a;
} else if (n == 1) {
return b;
} else {
for (int i = 2; i < n; ++i) {
int c = a + b;
sum += c;
a = b;
b = c;
}
return sum;
}
}
int main() {
int n;
cin >> n;
cout << fibonacci(n) << endl;
return 0;
}
Example 1:
Inputs arr[0]
Output: 1
Example 2:
Output: 3
Explanations :The possible subarrays are [1], [1], [2], [1, 1],
[1,2],[1,1,2]
Output 6
Code :
#include <iostream>
#include <vector>
#include <unordered_set>
using namespace std;
Q19) In a database there are N students, the fields of the table are
name,age ,grade,gender . Your task is to return the students
name who are greater than 20 years old & calculate the average
of grades using ascii values of female candidates .
Input : 3
AAA 21 A Female
BBB 21 B Male
CCC 24 C Female
Output -
AAA BBB CCC
66
Code:
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
vector<string> ans;
int totalFemale = 0, totalGrade = 0;
while(n--) {
string name, gender;
char grade;
int age;
cin >> name >> age >> grade >> gender;
if (gender == "Female") {
totalFemale++;
totalGrade += grade;
}
}
double avg = (double) (totalGrade / totalFemale);
cout << avg << endl;
Input- 6
[2 4 2 4 2 4]
Output - 2,4
Code-
#include <bits/stdc++.h>
using namespace std;
int main() {
int N;
cin >> N;
vector<int> array(N);
for (int i = 0; i < N; ++i) {
cin >> array[i];
}
findMajorityElement(array, N);
return 0;
}
Input -236
o/p- Number 236 is not divisible by 9
Input -162
Number 162 is divisible by 9.
Code:
#include <iostream>
int main() {
int number;
std::cin >> number;
if (number >= 100 && number <= 999) {
if (isDivisibleByNine(number)) {
std::cout << "Number " << number << " is divisible by 9" <<
std::endl;
} else {
std::cout << "Number " << number << " is not divisible by 9" <<
std::endl;
}
}
return 0;
}
Q22) we are given a lIst of numbers we need to return maximum
difference blw Smallest & Largest Number.
Ex , n= 7
[-3, -5, 1, 6, -7, 8, 11]
ans - 18
Code:
#include <bits/stdc++.h>
Example 1:
7L 7R 7L 8L 6R 7R 8R 6R
O/P: 3
Example 2:
7R 7L 8R 10R 10L
O/P: 2
Code:-
#include <iostream>
#include <unordered_map>
#include <string>
using namespace std;
int main() {
int N;
cin >> N;
unordered_map<string, int> store;
int pairs = 0;
for (auto& it : store) {
string key = it.first;
int val = it.second;
string opposite;
if (key[key.size() - 1] == 'L') {
opposite = key.substr(0, key.size() - 1) + 'R';
} else {
opposite = key.substr(0, key.size() - 1) + 'L';
}
if (store.find(opposite) != store.end()) {
pairs += min(val, store[opposite]);
}
}
return 0;
}
Q24) in a company there are employees and their efficiency is
given in array 'arr' (can be negative) you need to find the
maximum efficiency of 3 employees. The efficiency of 3
employees will be calculated by multiplying their individual
efficiencies from the given array.
Example 1:
[3 -2 -8 4 1]
O/P: 64
Code:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
int N;
cin >> N;
vector<int> arr(N);
for (int i = 0; i < N; ++i) {
cin >> arr[i];
}
sort(arr.begin(), arr.end());
// Max values
int emp1 = arr[N - 1];
int emp2 = arr[N - 2];
int emp3 = arr[N - 3];
// Negative case
int neg1 = arr[0];
int neg2 = arr[1];
cout << max(emp1 * emp2 * emp3, neg1 * neg2 * emp1) << endl;
return 0;
}
Example 1:
Output: 3
Example 2:
Output: 2
Code:
#include <iostream>
#include <unordered_map>
#include <vector>
#include <iostream>
void input_array_format() {
cout << "\nEnter elements in format [e1,e2,e3]: ";
string input;
getline(cin, input);
stringstream ss(input);
string token;
vector<string> arr;
while (getline(ss, token, ',')) {
arr.push_back(token);
}
findMajorityElement(arr, arr.size());
}
void input_space_separated() {
cout << "\nCase 2: Enter space-separated elements: ";
string input;
getline(cin, input);
stringstream ss(input);
int num;
vector<int> arr;
while (ss >> num) {
arr.push_back(num);
}
findMajorityElementInt(arr, arr.size());
}
int main() {
input_array_format();
input_space_separated();
return 0;
}
in-place so that objects of the same color are adjacent, with the
colors in the order red, white, and blue. We will use the integers
3, 6 and 7 to represent the color red, white, and blue,
respectively.
You must solve this problem without using the library's sort
function.
Example 1:
Output: [3 3 3 6 6 7 7]
Code:-
#include <iostream>
#include <vector>
int main() {
cout << "\nEnter space-separated elements: ";
vector<char> arr;
char c;
while (cin >> c) {
arr.push_back(c);
}
arr = sortBySwap(arr, arr.size());
for (char c : arr) {
cout << c << " ";
}
return 0;
}
Q27) Given an array of size N-1 with integers in the range of [1,N],
the task is to find the missing number from the first N integers.
Example 1:
Input:
[1 2 3 5]
Output: 4
Example 2:
Input:
[1 2 4]
Output: 3
Code:
#include <iostream>
#include <vector>
#include <sstream>
void input_space_separated() {
cout << "\nEnter space-separated elements: ";
int N;
cin >> N;
cin.ignore();
string input;
getline(cin, input);
stringstream ss(input);
int num;
vector<int> arr;
while (ss >> num) {
arr.push_back(num);
}
cout << "Missing number: " << getMissingNumIntFormat(arr, N) <<
endl;
}
void input_array_format() {
cout << "\nEnter elements in format e1,e2,e3: ";
string input;
int N; cin >> N;
cin.ignore();
getline(cin, input);
stringstream ss(input);
string token;
vector<string> arr;
while (getline(ss, token, ',')) {
arr.push_back(token);
}
cout << "Missing number: " << getMissingNumStrFormat(arr, N) <<
endl;
}
int main() {
input_space_separated();
input_array_format();
return 0;
}
Q28) Write a program that accepts two integers, nn and mm, and
prints all prime numbers between nn and mm (inclusive) such
that the sum of their digits is also a prime number.
Example 1:
Output: 23
Code:
#include <iostream>
bool calculateSum(int n) {
int sum = 0;
while (n > 0) {
int num = n % 10;
sum += num;
n /= 10;
}
return isPrime(sum);
}
int main() {
int n, m;
cin >> n >> m;
for (int i = n; i <= m; i++) {
if (isPrime(i) && calculateSum(i)) {
cout << i << endl;
}
}
return 0;
}
Q29) Write a program to take input of X and Y in a new line. Print
the number which is nearer the integer when divided by Y.
Example 1:
Input:
X=13
Y-3
Output: 12
Example 2:
Input:
X=13
Y=3
Output: 12
Code:
#include <iostream>
using namespace std;
int main() {
int x, y;
cin >> x;
cin >> y;
return 0;
}
• It must contain at least one special character from the set ('#',
'@'). • It must contain at least one uppercase letter and one
lowercase letter.
Example 1:
Input1: werV432@
Input2:2
Output: ygtX653#
Code:
#include <iostream>
using namespace std;
int main() {
string inputStr;
int value;
return 0;
}
Example 1:
Input:
N=4
[2 3 4 5]
Example 2:
Input:
N=2
[353]
Code:
#include <bits/stdc++.h>
void input_array_format1() {
int n; cin >> n;
cin.ignore();
string input;
getline(cin, input);
input.erase(remove(input.begin(), input.end(), '['), input.end()); //
Remove '['
input.erase(remove(input.begin(), input.end(), ']'), input.end()); //
Remove ']'
stringstream ss(input);
vector<string> arr1;
string element;
while (ss >> element) { // Read elements separated by space
arr1.push_back(element);
}
solve(arr1); // Print the result
}
int main() {
input_array_format1();
return 0;
}
Q32) Task 1- print the collatz sequence upto ending with 1 the
sequence should be in the following way
F(n)=n//2
F(n)=3*n+1
Task-2
For the given integer from 1,n it should calculate the sequence of
each k value l.e, 1<=k<=n Calculate the maximum length of
sequence list of each k value and return the maximum length of
the sequence list of the k value
Task-3
Input: 5
Output:
[5,16,8,4,2.1]
8,3
16,3
Input: 0
Output: Error!
Input: xyza
output: Error!
Input: 5.5
Output: Error!
Code:
#include <bits/stdc++.h>
using namespace std;
vector<int> generate_sequence(int n) {
vector<int> sequence;
sequence.push_back(n);
while (n != 1) {
if (n % 2 == 0)
n /= 2;
else
n = 3 * n + 1;
sequence.push_back(n);
}
return sequence;
}
int main() {
string n_str;
cin >> n_str;
if (is_positive_integer(n_str)) {
int n = stoi(n_str);
vector<int> sequence = generate_sequence(n);
auto max_val_k = max_sequence_value_and_k(n);
auto maxLen_kValue = max_length_of_sequence_and_k(n);
cout << "Sequence: [";
for (size_t i = 0; i < sequence.size(); ++i) {
cout << sequence[i];
if (i != sequence.size() - 1) {
cout << ", ";
}
}
cout << "]" << endl;
cout << maxLen_kValue.first << " " << maxLen_kValue.second
<< endl;
cout << max_val_k.first << " " << max_val_k.second << endl;
} else {
cout << "Error!" << endl;
}
return 0;
}
INPUT
OUTPUT
[123 , 371]
Code:
#include <iostream>
#include <vector>
#include <string>
#include <cmath>
int main() {
string input;
cout << "Enter numbers separated by commas: ";
getline(cin, input);
vector<string> arr;
size_t pos = 0;
while ((pos = input.find(',')) != string::npos) {
arr.push_back(input.substr(0, pos));
input.erase(0, pos + 1);
}
arr.push_back(input);
int flag = 0;
for (string num_str : arr) {
int num = stoi(num_str);
int n = num_str.length();
if (isArmStrongNumber(num, n)) {
cout << num << " ";
flag = 1;
}
}
if (flag == 0) {
cout << "No Armstrong numbers present";
}
return 0;
}
Input format:
Output:
Aditya: 110
Alice: 100
John: 90
Code:
#include <bits/stdc++.h>
int main() {
string s;
getline(cin, s);
vector<string> l;
string token;
istringstream iss(s);
while (iss >> token) {
l.push_back(token);
}
int k = stoi(l[0]);
int N = stoi(l[1]);
vector<pair<int, string>> arr;
for (int top = 0; top < k && top < arr.size(); top++) {
cout << arr[top].second << ": " << arr[top].first << endl;
}
return 0;
}
Q35) Print all unique combinations.
Input:
[3 0 1 2 3]
Code:
#include <iostream>
#include <sstream>
#include <vector>
int main() {
std::string input;
std::getline(std::cin, input);
std::istringstream iss(input);
int n;
iss >> n;
std::vector<int> arr;
int temp;
while (iss >> temp) {
arr.push_back(temp);
}
return 0;
}
Example 1:
Input: nums = [1 2 3 4]
Output: 4
Example 2:
Input: nums = [1 1 1 1 1]
Output: 0
Code:
#include <iostream>
#include <unordered_map>
#include <vector>
#include <sstream>
int main() {
string input;
getline(cin, input);
stringstream ss(input);
vector<int> nums;
int num;
while (ss >> num) {
nums.push_back(num);
}
unordered_map<int, int> d;
for (int i = 0; i < nums.size(); ++i) {
if (d.find(nums[i]) == d.end()) {
d[nums[i]] = 1;
} else {
d[nums[i]]++;
}
}
int ans = 0;
for (auto it: d) {
if (it.second == 1) {
ans += it.first;
}
}
cout << ans << endl;
return 0;
}
Q37) Given an integer array nums, find the subarray with the
largest sum, and return its sum.
Example 1:
Output: 6
Example 2:
Input: nums = [5 4 -1 7 8]
Output: 23
Explanation: The subarray [5,4,-1,7,8] has the largest sum 23
Code:
#include <iostream>
#include <vector>
#include <algorithm>
#include <sstream>
int main() {
string input;
getline(cin, input);
stringstream ss(input);
vector<int> nums;
int num;
while (ss >> num) {
nums.push_back(num);
}
int maxSum = nums[0];
int currentSum = 0;
for (int i = 0; i < nums.size(); ++i) {
currentSum += nums[i];
if (currentSum > maxSum) {
maxSum = max(currentSum, maxSum);
}
if (currentSum < 0) {
currentSum = 0;
}
}
cout << maxSum << endl;
return 0;
}
Q38) You are tasked with writing a program to calculate the total
shipping cost based on the weight of the package and the
distance it needs to travel. The shipping cost is determined by
the following criteria:
Example 1:
10 (w)
100 (D)
output: $30.00
Code:
#include <bits/stdc++.h>
int main() {
int weight, distance;
char output[50];
sprintf(output, "$%.2f", total_cost);
return 0;
}
k=3
Output: 6
Code:
#include <bits/stdc++.h>
using namespace std;
int main() {
string inputLine;
getline(cin, inputLine);
istringstream iss(inputLine);
vector<int> arr;
int number;
while (iss >> number) {
arr.push_back(number);
}
int k;
cin >> k;
return 0;
}
__________________________
Example 1:
Input
###*** → Value of S
Output
0 ---> number of * and # are equal
Example 2:
Input
###***# → Value of S
Output
-1 ---> number of # is more than *
Example 3:
Input
#*** → Value of S
Output
2 ---> number of * is more than #
Constraints
S = {*,#}
0<len(S)<=50
Code Solution:
#include<iostream>
using namespace std;
int main()
{
string str;
int count = 0, count1 = 0;
cin >> str;
for(int i = 0; str[i] != '\0'; i++)
{
if(str[i] == '#')
count++;
if(str[i] == '*')
count1++;
}
if(count == count1)
cout << "0";
else
cout << count1-count;
}
Solution in C++:
#include<bits/stdc++.h>
using namespace std;
vector<int> rotate(int nums[], int n, int k) {
if (k > n)
k = k % n;
vector<int> ans(n);
#include<bits/stdc++.h>
using namespace std;
int find(int n1, int n2) {
int count = 0;
for (int i = n1 ; i <= n2 ; i++) {
int num = i;
vector<bool> visited;
visited.assign(10, false);
while (num > 0) {
if (visited[num % 10] == true)
break;
visited[num % 10] = true;
num /= 10;
}
if (num == 0)
count++;
}
return count;
}
int main()
{
int n1 = 101, n2 = 200;
cout << find(n1, n2);
}
Note : The order of the numbers which are not the multiplier of 10
should remain unaltered ,and similarly the
order of the numbers which are the multiplier of 10 should
remain unaltered.
For example :
Suppose N = 9 and Arr[] = {10, 12, 5, 40, 30, 7, 5, 9, 10}
You have to push all the multiple of 10 at the end of the array
Arr[].
Hence the output is : 12 5 7 5 9 10 40 30 10
Solution in C++
#include<bits/stdc++.h>
using namespace std;
int main ()
{
int N;
cin>>N;
vector <int> Arr(N);
for(int i=0; i<N; i++)
cin>>Arr[i];
vector<int> A1, A2;
for(int i=0; i<N; i++)
{
if(Arr[i]%10==0)
A2.push_back(Arr[i]);
else A1.push_back(Arr[i]);
}
for(int i=0; i<A1.size(); i++)
Arr[i]=A1[i];
int k=A1.size();
for(int i=0; i<A2.size(); i++)
Arr[k++]=A2[i];
for(int i=0; i<N; i++)
cout<<Arr[i]<<" ";
return 0;
}
Output :
20 30 40 10
Solution in C++ :
#include<bits/stdc++.h>
using namespace std;
int main ()
{
int N;
cin>>N;
vector <int> Arr(N);
for(int i=0; i<N; i++)
cin>>Arr[i];
int K;
cin>>K;
queue <int> que;
for(int i=0; i<N; i++)
que.push(Arr[i]);
while(K--)
{
int x= que.front();
que.pop();
que.push(x);
}
while(!que.empty()){
cout<<que.front()<<" ";
que.pop();
}
return 0;
}
Q45) For hiring a car, a travel agency charges R1 rupees per hour
for the first N hours and then R2 rupees per hour.
Given the total time of travel in minutes in X.
The task is to find the total travelling cost in rupees.
Note : While converting minutes into hours, ceiling value should
be considered as the total number of hours.
For example : If the total travelling time is 90 minutes, i.e. 1.5
hours, it must be considered as 2 hours.
Example :
Input :
20 -- r1
4 -- n
40 -- r2
300 -- x
Output :
120
Explanation :
Solution in C++:
#include<bits/stdc++.h>
using namespace std;
int main () {
int r1;
cin >> r1;
int n;
cin >> n;
int r2;
cin >> r2;
int x;
cin >> x;
int total = 0;
int hours = ceil(x * 1.0 / 60);
if (n > hours) {
total += (hours * r1);
} else {
total += n * r1;
hours -= n;
total += hours * r2;
}
cout << total;
return 0;
}
Solution in C++:
#include<bits/stdc++.h>
using namespace std;
#define mod 1000000007
int dp[21][21][21][4];
int countPossibleWays(int a, int b, int c, int prev) {
if (dp[a][b][c][prev] != -1) {
return dp[a][b][c][prev];
}
else if (a == 0 && b == 0 && c == 0) {
dp[a][b][c][prev] = 1;
return dp[a][b][c][prev];
}
return 0;
}
Solution in C++:
#include<bits/stdc++.h>
using namespace std;
int main(){
string cstr;
cin>>cstr;
string res="";
for(int i=0; i<cstr.size(); i++){
if( 'A'<=cstr[i] and cstr[i]<='Z')
{
if(i!=0)
res += " ";
res += (cstr[i] + 32);
}
else res += cstr[i];
}
cout<<res;
return 0;
}
Instructions:
● Each employee can be paired only once.
● Each employee can only compete once
Example 1:
Input:
3---- Number of employees
Output:
1
Explanation:
N= 3 i.e, Odd Number
[1],[2],[3]: all employees are single.
Example 2:
Input:
4--- Numbers of employees
Output:
10
Explanation:
N= 4 i.e, Even Number
[1],[2],[3],[4]: all employees are single.
[1],[2,3],[4]: 3 and 4 are paired but 1 and 2 are single
[1,2],[3],[4]: 2 and 3 are paired but 1 and 4 are single
[1,3],[2],[4]: 1 and 3 are paired but 2 and 4 are single
[1,4],[2],[3]: 1 and 4 are paired but 2 and 3 are single
[2,4],[3],[1]: 2 and 4 are paired but 3 and 1 are single
[1,2], [3,4] : pairs of 1 and 2, 3 and 4
[1,3], [2,4] : pairs of 1 and 3, 2 and 4
[1,4], [2,3] : pairs of 1 and 4, 2 and 3
#include<bits/stdc++.h>
using namespace std;
int count_ways( int N){
if(N%2!=0)
return N;
int t[N+1];
for(int i=0; i<=N; i++)
{
if(i<=2)
t[i]=i;
else t[i] = t[i-1] + (i-1)*t[i-2];
}
return t[N];
}
int main(){
int N;
cin>>N;
int res = count_ways(N);
cout<<res;
return 0;
}
Code Solution: