0% found this document useful (0 votes)
7 views

Coding questions

The document contains multiple coding problems and their respective solutions in C++. Each problem involves various algorithmic challenges such as finding subarrays with a specific sum, sorting items based on risk levels, counting Sundays in a given number of days, and calculating fines for traffic violations based on vehicle registration numbers. The solutions provided include detailed code implementations for each problem.

Uploaded by

lakra lakra
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Coding questions

The document contains multiple coding problems and their respective solutions in C++. Each problem involves various algorithmic challenges such as finding subarrays with a specific sum, sorting items based on risk levels, counting Sundays in a given number of days, and calculating fines for traffic violations based on vehicle registration numbers. The solutions provided include detailed code implementations for each problem.

Uploaded by

lakra lakra
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 17

Coding questions

Given several rooms (N), and several gold coins in N rooms. You have to
find room numbers from where you will start and from where you will exit.
If there is more than one solution possible, then you have to provide a
solution with a smaller starting room number.
You can assume that room numbers will start from 1 and end at N.

Hint: Find a continuous subsequence whose sum will be exactly equal to


K.

Code- #include<iostream>

using namespace std;

pair<int, int> sum_array( int arr[], int n, int k)

int sum;

int i=0, j=0;

for(i=0; i<n; i++)

sum=0;

for(j=i; j<n; j++){

sum += arr[j];

if(sum>=k)

break;

}
if(sum == k)

return make_pair(i+1, j+1);

return make_pair(-1, -1);

int main(){

int n, k;

cin>>n>>k;

int arr[n];

for(int i=0; i<n; i++)

cin>>arr[i];

pair<int, int> result = sum_array(arr, n, k);

if (result.first == -1 && result.second == -1)

cout << "No subarray found with the sum " << k << endl;

else

cout <<result.first << " " << result.second << endl;


return 0;

A chocolate factory is packing chocolates into the packets. The chocolate


packets here represent an array of N number of integer values. The task
is to find the empty packets(0) of chocolate and push it to the end of the
conveyor belt(array).

Code-

#include<iostream>

#include<vector>

using namespace std;

int main()

int n;

cin>>n;

vector<int>arr;

for(int i=0; i<n; i++)

int num;

cin>>num;

arr.push_back(num);

}
vector<int> arr1;

int j=0;

for(int i=0; i<n; i++)

if(arr[i] != 0){

arr1.push_back(arr[i]);

j++;

for(int i = j ; i < n; i++){

arr1.push_back(0);

for(int i=0; i<n; i++)

cout<<arr1[i]<<" ";

}
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”.

Code:
#include <bits/stdc++.h>
using namespace std;

int toggle_element(int n)
{
//decimalto binary
int num=n;
int m=0;
vector<int> arr;

while(num > 0)
{
if(num % 2 == 0)
arr.push_back(0);
else
arr.push_back(1);
num= num/2;
m++;
}

//binary to decimal
int sum = 0;
for(int i=0; i<m; i++)
{
int number = arr[m - 1 -i];
if(number == 1)
sum+=pow(2,i);
}

return sum;
}
int main(){

int n;
cin>>n;

cout<<toggle_element(n);
}

Jack is always excited about sunday. It is favourite day, when he gets to


play all day. And goes to cycling with his friends.

So every time when the months starts he counts the number of sundays
he will get to enjoy. Considering the month can start with any day, be it
Sunday, Monday…. Or so on.

Count the number of Sunday jack will get within n number of days.

Code:
#include<bits/stdc++.h>
using namespace std;

int count_of_sundays(string str, int n)


{
string days[]={"sun","mon","tue","wed","thu","fri","sat"};
int num, count=0;
for(int i=0; i<7; i++)
{
if(str.compare(days[i]) == 0){
num = 7 - i;

while(num<=n)
{
count++;
num += 7;

}
break;
}
}

return count;
}

int main()
{
string str;
cin>>str;

int n;
cin>>n;

cout<<count_of_sundays(str, n);

return 0;
}

Airport security officials have confiscated several item of the


passengers at the security check point. All the items have been
dumped into a huge box (array). Each item possesses a certain
amount of risk[0,1,2]. Here, the risk severity of the items
represent an array[] of N number of integer values. The task
here is to sort the items based on their levels of risk in the
array. The risk values range from 0 to 2.

Code:
#include<bits/stdc++.h>
using namespace std;

int main()
{
int n;
cin>>n;

vector<int> arr;

for(int i=0; i<n; i++)


{
int num;
cin>>num;
arr.push_back(num);
}

sort(arr.begin(), arr.end());

for(int i=0; i<n; i++)


{
cout<<arr[i]<<" ";
}

Given an integer array Arr of size N the task is to find the count of
elements whose value is greater than all of its prior elements.

Note : 1st element of the array should be considered in the count of the
result.

Code:
#include<bits/stdc++.h>
using namespace std;

int main()
{
int n;
cin>>n;

int arr[n];
for(int i=0; i<n; i++)
{
cin>>arr[i];
}

vector<int> arr1;
int m=0;

for(int i=n - 1 ;i>=0; i--)


{
int flag=0;
for(int j= i - 1; j>=0; j--)
{
if(arr[j]>=arr[i])
{
flag=1;
break;
}
}
if(flag == 0)
{
arr1.push_back(arr[i]);
m++;
}
}
cout<<m<<endl;

A supermarket maintains a pricing format for all its products. A


value N is printed on each product. When the scanner reads the
value N on the item, the product of all the digits in the value N
is the price of the item. The task here is to design the software
such that given the code of any item N the product
(multiplication) of all the digits of value should be
computed(price).

Code:
#include<iostream>
using namespace std;

int main()
{
int n;
cin>>n;

int product =1;

while(n > 0)
{
int num =n % 10;
product*=num;
n/=10;
}
cout<<product;
}

A furnishing company is manufacturing a new collection of


curtains. The curtains are of two colors aqua(a) and black (b).
The curtains color is represented as a string(str) consisting of
a’s and b’s of length N. Then, they are packed (substring) into L
number of curtains in each box. The box with the maximum
number of ‘aqua’ (a) color curtains is labeled. The task here is
to find the number of ‘aqua’ color curtains in the labeled box .
Code:
#include<bits/stdc++.h>
using namespace std;

int main()
{
string str;
cin>>str;

int n;
cin>>n;

int set_no = 0, prev_count = 0;


int curr_count = 0;
int max_set;

for(int i=0; i<str.length(); i=i+n)


{
for(int j=i; j<i + n; j++)
{
if(str[j] == 'a'){
curr_count++;
}
}

set_no++;

if(curr_count > prev_count){


prev_count = curr_count;
curr_count = 0;
max_set=set_no;
}
}

cout<<max_set;
}

An international round table conference will be held in india.


Presidents from all over the world representing their respective
countries will be attending the conference. The task is to find
the possible number of ways(P) to make the N members sit
around the circular table such that.

The president and prime minister of India will always sit next to
each other.

Code:

#include<iostream>

using namespace std;

int fact(int n)

if(n == 1)

return 1;

else

return n*fact(n-1);

}
int main()

int n;

cin>>n;

cout<<fact(n - 1) * 2;

An intelligence agency has received reports about some threats. The


reports consist of numbers in a mysterious method. There is a number “N”
and another number “R”. Those numbers are studied thoroughly and it is
concluded that all digits of the number ‘N’ are summed up and this action
is performed ‘R’ number of times. The resultant is also a single digit that is
yet to be deciphered. The task here is to find the single-digit sum of the
given number ‘N’ by repeating the action ‘R’ number of times.

If the value of ‘R’ is 0, print the output as ‘0’.

Code:

#include<iostream>

using namespace std;

int summation(int N)

{
int sum=0;

while(N > 0)

int num = N % 10;

sum+=num;

N=N/10;

return sum;

int main()

int N;

cin>>N;

int R;

cin>>R;

int sum=summation(N);

sum=sum * R;
int output= summation(sum);

cout<<output;

Particulate matters are the biggest contributors to Delhi pollution. The


main reason behind the increase in the concentration of PMs include
vehicle emission by applying Odd Even concept for all types of vehicles.
The vehicles with the odd last digit in the registration number will be
allowed on roads on odd dates and those with even last digit will on even
dates.

Given an integer array a[], contains the last digit of the registration
number of N vehicles traveling on date D(a positive integer). The task is to
calculate the total fine collected by the traffic police department from the
vehicles violating the rules.

Note : For violating the rule, vehicles would be fined as X Rs.

Code:
#include<iostream>
using namespace std;

int main()
{
int N;
cin>>N;

int arr[N];
for(int i=0; i<N; i++)
cin>>arr[i];

int D;
cin>>D;

int x;
cin>>x;

int count =0;


int fine;

if(D % 2 == 0){
for(int i=0; i<N; i++)
{
if(arr[i] % 2 != 0)
count++;
}

fine = count * x;
}
else{
for(int i=0; i<N; i++)
{
if(arr[i] % 2 == 0)
count++;
}

fine = count * x;
}

cout<<fine;
}
---------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------
Advanced coding

Alice and her friends are playing a game of verbal Kho-Kho. Alice is acting
as a mediator, and the rest of the N friends are seated on N chairs, one
each………..
Given N number of friends and digit array D, denoting the digit understood
by each friend F. finds out how many of Alice’s friends have not enacted
well OR did not understand the enactment by the previous friend
correctly.

Code:

#include<bits/stdc++.h>

using namespace std;

int main()

int n;

cin>>n;

int arr[n];

for(int i=0; i<n; i++)

cin>>arr[i];

int count=0;

for(int i=1; i<n; i++)

if(arr[i] != arr[i - 1])

count++;

if(count != 0)

count++;
cout<<count;

You might also like