Coding questions
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.
Code- #include<iostream>
int sum;
sum=0;
sum += arr[j];
if(sum>=k)
break;
}
if(sum == k)
int main(){
int n, k;
cin>>n>>k;
int arr[n];
cin>>arr[i];
cout << "No subarray found with the sum " << k << endl;
else
Code-
#include<iostream>
#include<vector>
int main()
int n;
cin>>n;
vector<int>arr;
int num;
cin>>num;
arr.push_back(num);
}
vector<int> arr1;
int j=0;
if(arr[i] != 0){
arr1.push_back(arr[i]);
j++;
arr1.push_back(0);
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);
}
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;
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;
}
Code:
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n;
cin>>n;
vector<int> arr;
sort(arr.begin(), arr.end());
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;
Code:
#include<iostream>
using namespace std;
int main()
{
int n;
cin>>n;
while(n > 0)
{
int num =n % 10;
product*=num;
n/=10;
}
cout<<product;
}
int main()
{
string str;
cin>>str;
int n;
cin>>n;
set_no++;
cout<<max_set;
}
The president and prime minister of India will always sit next to
each other.
Code:
#include<iostream>
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;
Code:
#include<iostream>
int summation(int N)
{
int sum=0;
while(N > 0)
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;
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.
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;
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>
int main()
int n;
cin>>n;
int arr[n];
cin>>arr[i];
int count=0;
count++;
if(count != 0)
count++;
cout<<count;