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

Document 10

The document describes a problem where a physical trainer is arranging students in a row for physical training based on their heights. There are N students with different heights. The trainer wants to insert students with average heights between two students with odd heights standing consecutively. A program is needed to output the desired sequence after inserting average height students in the proper places. Sample inputs and outputs are provided to demonstrate the expected output format.

Uploaded by

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

Document 10

The document describes a problem where a physical trainer is arranging students in a row for physical training based on their heights. There are N students with different heights. The trainer wants to insert students with average heights between two students with odd heights standing consecutively. A program is needed to output the desired sequence after inserting average height students in the proper places. Sample inputs and outputs are provided to demonstrate the expected output format.

Uploaded by

Dipesh Raj
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 207

DAY 1

1 Consider a physical trainer is arranging the students for physical training in a school. There are
N students standing in a row for physical training. He wants to insert a student with average
height between the two students who are having odd height standing in consecutive. Write a
program for a physical trainer so that he can get the desired sequence of students.
Example: if the 5 students are having values: 3 6 5 9 7 then 5 and 9 are the students with odd
height so the student with height 7 (average of 5 and 9) should be inserted between 5 and 9.
Similarly, 9 and 7 are the 2 students with odd heights so student with height 8 should be
inserted between 9 and 7.
Output: 3 6 5 7 9 8 7

Input Format
The first line will be containing one Integer representing a number of students N. The second
line will contain N integers representing the heights of the students.
Constraints
N>2 && N<20

Output Format
The desired sequence of students after inserting students having the average height of two
students with odd height standing in consecutive in between them.
Sample Input 0
10
4 7 9 8 5 7 6 5 2 4

Sample Output 0
4 7 8 9 8 5 6 7 6 5 2 4

Sample Input 1
1

Sample Output 1
Invalid Input

Sol:-
#include <cmath>

#include <cstdio>

#include <vector>

#include <iostream>

#include <algorithm>

using namespace std;

int main() {

/* Enter your code here. Read input from STDIN. Print output to STDOUT */

int n;

cin>>n;

if(n<2){

cout<<"Invalid Input";

else{

int arr[n];

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

cin>>arr[i];

if(arr[i]>=100000){

cout<<"Invalid Input"<<endl;

return 0;

cout<<arr[0]<<" ";

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

if(arr[i]%2==1 && arr[i-1]%2==1){


cout<<(arr[i-1]+arr[i])/2<<" "<<arr[i]<<" ";

else{

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

return 0;

2) To insert the element to specific location. The specific location will be calculate based on
given following condition 1. if the newly inserted element is less than the size of array then the
location/ index of element is size_of_array mod(%) element 2. else the location/ index of
element is element mod(%) size_of_array 3. After inserting the new element number of
element should be increase by one.

Input Format
Sample 1: Line 1: Enter the size of Array : 10
Line 2: How many element do you want in insert in to array :5
Line 3: 11 22 33 44 55

Line 4: Enter the number you want to insert : 78


Line 5(Output): 11 22 33 44 55 0 0 78 0 0
Sample 2: Line 1: Enter the size of Array : 15
Line 2: How many element do you want in insert in to array :3

Line 3: 12 23 34
Line 4: Enter the number you want to insert : 71
Line 5(Output): 12 23 34 0 0 0 0 0 0 0 71 0 0 0 0
Sample 3: Line 1: Enter the size of Array : 5
Line 2: How many element do you want in insert in to array :5

Line 3: 11 22 33 44 55
Line 4: Enter the number you want to insert : 8
Line 5(Output):: Insertion is not possible because Array Overflow.. !!
Constraints

The array size maximum: 50


Length of Array: positive
Output Format
Prints all the numbers after inserting the number at specified location in the list

Sample Input 0
10
3
5 6 2
9

Sample Output 0
9 5 6 2 0 0 0 0 0 0

Explanation 0
Enter the size of Array : 10 How many element do you want in insert in to array :3 4 7 8 4 7 8 0
0 0 0 0 0 0 Enter the number you want to insert : 9 The Element 9 inserted at the location : 1 9 4
78000000
----------------------------------------------------------------------------------------------------------------------------- -------------
#include<bits/stdc++.h>

using namespace std;

int main(){

int n;cin>>n;

vector<int> arr(n,-1);

int x;cin>>x;

for(int i=0;i<x;i++){

int y;cin>>y;

// cout<<y<<" ";

}// cout<<endl;

cin>>x;

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

int y;cin>>y;

cout<<y<<" ";

}cout<<endl;

}
Day 2
3) Today is such a beautiful evening after heavy rainfall and kids decided to play in
playground, then at the same moment they are deligthed to see the colors of Rainbow in the
sky. They observe seven colors like Violet, Indigo,Blue,Green,Yellow,Orange,Red i.e
'V','I','B','G','Y','O','R'.Now your task is to remove a color which is mentioned above as input and
display the remaining colors of Rainbow, if the color which you want to delete that is not comes
under VIBGYOR then display message as "Color not available".
Input Format
User will enter the character which is to be deleted. Example:- V

Constraints
Character entered by user must be in uppercase
Output Format
It will print remaining colors of VIBGYOR Example:-IBGYOR But if color is Not available then it
will print message as shown bellow:- Color not available

Sample Input 0
V

Sample Output 0
IBGYOR
Sol:-
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
void array_delete(int loc,char a[])
{
for(int i=loc;i<7;i++)
{
a[i]=a[i+1];
}

int main() {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
char arr[7]={'V','I','B','G','Y','O','R'};
char t;
int flag=0;
cin>>t;
for(int i=0;i<7;i++)
{
if(t==arr[i])
{
array_delete(i, arr);
flag=1;
}
}
if(flag==1)
{
for(int i=0;i<6;i++)
cout<<arr[i];
}else
cout<<"Color not available";

return 0;
}

4
Given an array a[] of M elements, the task is to write a program to search a given element X in
a[].

Input Format
you have to take two input lines:
first line takes the array element
second line takes the value of x

Sample Input 1
10 20 80 30 60 50 110 100 130 170
110

Sample Output 1
7

Sample Input 2
10 20 80 30 60 50 110 100 130 170
175
Sample Output 1
-1

Constraints
Array size should be between 10-50 elements
Output Format
The program prints the position of the element found else it prints -1
Sample Input 0
10 20 80 30 60 50 110 100 130 170
110

Sample Output 0
7

#include <iostream>

using namespace std;

int main()
{
int a[10],x,k=0;
//cout<<"Enter elements into the array :";
for(int i=0; i<10;i++){
cin>>a[i];
}
//cout<<"Eneter the element you need to search : ";
cin>>x;
for(int i=0; i<10;i++){
if(a[i]==x){
cout<<(i+1);
k=1;
exit(0);
}
}
if(k==0)
cout<<"-1";
return 0;
}

Day 3
5 Consider Aman is visiting Nehru Zoo. She has seen there are N elephants standing in a row.
She wants to remove the elephants having the same height standing in consecutive.Write a
program for Aman so that she can get the desired sequence of elephants.
Input Format
The first line will be containing one Integer representing a number of elephants N.
The second line will contain N integers representing the heights of the elephants.
Constraints
N>2 && N<30
Output Format
The desired sequence of elephants after removing elephants having the same height standing
in consecutive.
Sample Input 0
12
4 7 9 9 8 5 7 7 6 5 5 5

Sample Output 0
4 7 9 8 5 7 6 5

Sample Input 1
1

Sample Output 1
Invalid Input

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

int main()
{
int n;
cin >> n;
if(n>2 && n<30)
{
int a[n]={0};
int b[n]={0};
for (int i = 0; i < n; i++)
{
cin >> a[i];
}
int j=0;
if(a[0]!=0)
{
b[j++]=a[0];
}
else
{
cout << "Invalid Input";
return 0;
}
for(int i = 1; i < n; i++)
{
if(a[i]!=0)
{
if(a[i]!=a[i-1])
{
b[j++]=a[i];
}
}
else
{
cout << "Invalid Input";
return 0;
}
}
for(int i = 0; i < j; i++)
{
cout << b[i] << " ";
}
}
else
{
cout<<"Invalid Input";
}
return 0;
}

6 Dr. Priyanka is teaching a course code CSE205 to N number of students. She has conducted
CA-1 and recorded the result into an array where array index represents roll-no of students and
value represents the marks. Now find out the topper of a class. If multiple students are toppers
then you have to print the largest roll number from the topper list.
Input Format
Enter number of students
Enter marks

Constraints
n<10 && n>1
Note that the roll numbers of students begin with 0.
Output Format

It should print the position index of the element having highest value
Sample Input 0
5
55 75 89 99 98

Sample Output 0
3

#include <iostream>
#include <climits>
using namespace std;

int main() {
int N;
cin>>N;
int arr[N];
for (int i=0;i<N;i++)
{
cin>>arr[i];
}
int max=INT_MIN;
int index=-1;
for(int i=0;i<N;i++)
{
if(arr[i]>=max)
{
max=arr[i];
index=i;
}
}
cout<< index;
return 0;
}
Day 4
7
Alice went for shopping and bought 8 goods costing different prices. At
the time of billing she realises that she did not have enough money.
Now, she have decided to remove the item costing maximum amount
and replace it with another item whose price is same as the item
costing minimum amount. Help Alice and display final prices on the
screen.
Note: if more than one element cost maximum price, replace the first
item.
Example 1: Input: 250 1000 50 20 10 100 200 25 Output: 250 10 50 20
10 100 200 25
Example 2: Input: 2500 2500 50 20 10 100 200 25 Output: 10 2500 50
20 10 100 200 25
Input Format
Input Format:
Sample Input: 250 1000 50 20 10 100 200 25
Constraints
8 items prices are required
Output Format
Prints the final prices of different items after replacing
Sample Output: 10 2500 50 20 10 100 200 25
Sample Input 0
250 250 250 250 250 250 250 250

Sample Output 0
250 250 250 250 250 250 250 250

#include<iostream>
using namespace std;

int main(){
int arr[8];

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


int min=arr[0],max=arr[0], max_key=0, min_key=0;
for(int i=0; i<8; i++){
if(min>arr[i]){
min=arr[i];
min_key=i;
}
else if(max<arr[i]){
max=arr[i];
max_key=i;
}
}

// cout<<"---------"<<min<<" "<<max<<endl;
// cout<<"---------"<<min_key<<" "<<max_key<<endl;
for(int i=0; i<8; i++){
if(i==max_key){
arr[i]=min;
}
// else if(i==max_key) arr[i]=max;
}

for(auto x: arr) cout<<x<<" ";


}
8
A task is assigned to some surveyors to store temparature in an array
daily for 8 days.
An array for storing temperature is used by the surveyor but they forget
to insert one element at some positions. Index 0 1 2 3 4 5 6 7 Temp. 30
40 35 25 20 10 23
If they insert 15 at index 4 then the array looks like:
Index 0 1 2 3 4 5 6 7
Temparature 30 40 35 25 15 20 10 23
So, you decided to create a function to insert the temperature at any
given position in an array so, that if insertion is required then they can
insert it.
Input Format
You have to take three line inputs where
first line contains an array of temperature separated by space
second line input contains the position index where we want to insert
element(where index position starts from 0)
Third line input contains the element you want to insert.

Constraints
Temparature values must be an integer values. Capacity of an array to
store is according to 8 days temparature data only.
Output Format
Prints all the temperatures given in an array after inserting the
temparature at given index position. Output is comma separated values
where there is no comma after last element. If the position given is not
in the array index range then display an error message "Invalid
Position".
Sample Input 0
30 40 35 25 20 10 23
3
15

Sample Output 0
30,40,35,15,25,20,10,23

Sample Input 1
5 15 20 23 34 45 45
-1
5

Sample Output 1
Invalid Position
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

int main(){
int arr[8];
for(int i=0; i<7; i++) cin>>arr[i];

int index,key;
cin>>index;
if(index<0 || index>7){
cout<<"Invalid Position";
return 0;
}
cin>>key;

for(int i=7; i>index; i--){


arr[i]=arr[i-1];
}
arr[index]=key;

for(int i=0; i<8; i++){


if(i==7){
cout<<arr[i];
break;
}
cout<<arr[i]<<",";
}
}
Day 5
9
To insert the element to a specific location. The specific location will be
calculate based on given following condition 1. find the sum of newly
inserted element, the result of the sum is the location of the newly
inserted element. 2. If the sum is greater than the size of the array then
repeat the condition one again. 3. After inserting the new element, the
number of elements should be increased by one.
Input Format
Sample 1: Line 1 : Enter the size of Array : 9
Line 2 : How number of element to insert in to array :5
Line 3 : Enter 5 number of element :
Line 4 : 12 34 87 32 11
Line 5 : 12 34 87 32 11 0 0 0 0
Line 6 : Enter the number to insert in to array: 65
Line 7 : The Element 65 inserted at the location : 2
Line 8 : 12 65 34 87 32 11 0 0 0
Sample 2:
Line 1 : Enter the size of Array : 9
Line 2 : How number of element to insert in to array :2
Line 3 : Enter 2 number of element :
Line 4 : 12 23
Line 5 : 12 23 0 0 0 0 0 0 0
Line 6 : Enter the number to insert in to array: 44
Line 7 : The Element 44 inserted at the location : 8
Line 8 : 12 23 0 0 0 0 0 44 0
Sample 3 Line 1 : Enter the size of Array : 3
Line 2 : How number of element to insert in to array :3
Line 3 : Enter 3 number of element :
Line 4 : 12 34 65
Line 5 : 12 34 65
Line 6 : Enter the number to insert in to array: 55
Line 7 : Insertion is not possible becouse Array Overflow.. !!
Line 8 : 12 34 65
Constraints
The length of array (size) should be 0 =< L < 100
Output Format
The list of array element should be display in forwarding order before
insertion and after insertion operation.
Sample Input 0
9
2
12 23
44

Sample Output 0
12 23 0 0 0 0 0
44 0

Explanation 0
Enter the size of Array : 9 How number of element to insert in to array
:2 Enter 2 number of element : 12 23 12 23 0 0 0 0 0 0 0 Enter the
number to insert in to array: 44 The Element 44 inserted at the location
: 8 12 23 0 0 0 0 0 44 0

#include<iostream>
using namespace std;
int getSum(int n,int size)
{
int sum = 0;
while (n != 0) {
sum = sum + n % 10;
n = n / 10;
}
if(size<sum){
sum = getSum(sum,size);
}
return sum;
}
int main(){

int size, n,i,elem;


cin>>size>>n;
int arr[size+1];
for(i=0;i<size;i++){
if(i<n){cin>>arr[i];}
else{arr[i]=0;}
}
cin>>elem;
if(size>n){
int sum = getSum(elem,size);

for(i=size; i>=sum; i--)


{
arr[i] = arr[i-1];
}
arr[i] = elem;
}
else{
cout<<"Insertion is not possible becouse Array Overflow..
!!"<<endl;
}

for(i=0;i<size;i++){
cout<<arr[i];
if(i!=size-1){
int spc = 8-to_string(arr[i]).length();
for(int k=0;k<spc;k++){
cout<<" ";
}
}
}
}

10
Akash got an interesting assignment. He got K balls,
every ball has its diameter inscribed. Write a
programming solution, to help akash to find the largest
ball. He is supposed to solve assignment N times.
Input Format
• 1st line contain N(nos. of problem)
• 2nd line contain space separated diameter from K
balls
Constraints
• K=6
• N>0 & N<5
Output Format
Return diameter of highest ball. For Every assignment,
output should be on a new line.
Sample Input 0
2
4 1 8 34 23 18
34 23 45 11 7 55

Sample Output 0
34
55

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

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

for(int i=0;i<6;i++){
if(arr[i]>max){
max=arr[i];

}
}
cout<<max<<endl;
}
return 0;
}

Day 6
11
lewis is working on a game designing
project called as Ball Blast and he is
facing issue in implementing the logic for
the same. There are N number of balls
having a number written on each ball. All
the balls are arranged in a row and as per
the rules of the game, if there are 3
balls in a row having ODD number on them
then the first ball of the set will be
removed. The same process is to be
repeated for all the balls from left to
right. At the end of the game, the numbers
on the remaining balls is to be printed.
Example:
If there are 7 balls with numbers: 3, 8,
5, 7, 1, 4, 5 Then: 5, 7, 1 are the 3
balls in a row with odd numbers so, 5
should be removed. 3, 8, 7, 1, 4, 5 No
other triplet of odd numbers.
But, If there are 7 balls with numbers: 3,
8, 5, 7, 1, 5, 4 Then: 3, 8, 5, 7, 1, 5, 4
==> 3, 8, 7, 1, 5, 4 ==> 3, 8, 1, 5, 4
Sample Input 1: 5 4 3 1 3 6
Sample Output 1: 4 1 3 6
Sample Input 2: 6 7 4 5 3 1 4
Sample Output 2: 7 4 3 1 4
Sample Input 3: 4 2 5 3 4
Sample Output 3: 2 5 3 4
Input Format
First line will be the number of Balls N.
Second Line will be the numbers written on
Balls.
Sample Input 1: 5 4 3 1 3 6
Constraints
N > 0 where N is the number of Balls
Output Format
Output should be the numbers on the
Remaining Balls after the Blast
Sample Output 1: 4 1 3 6
Sample Input 0
5
4 1 3 7 8

Sample Output 0
4 3 7 8

#include<iostream>

using namespace std;

int main() {
int nOfBalls;
cin >> nOfBalls;
int* balls = new int(nOfBalls);
for (int x = 0; x < nOfBalls; x++) {
cin >> balls[x];
}
for (int i = 0; i < nOfBalls-2; i++) {
if (balls[i] % 2 != 0 &&
balls[i+1] % 2 != 0 &&
balls[i+2] % 2 != 0) {

for (int j = i; j < nOfBalls-


1; j++) {
balls[j] = balls[j+1];
}
nOfBalls--;
i--;
}
}

for (int j = 0; j < nOfBalls; j++) {


cout << balls[j] << " ";
}
return 0;
}

12
You are given two integers representing
the size of two different sized arrays.
Take the elements of the arrays from the
user in the non-increasing order. If the
elements are not in sorted order in any
array, display the message “Incorrect
Array Elements”. Merge the elements of the
given arrays in sorted order and display
them. The size of the array should be
greater than 0 and less than equal to 20.
If array will not be in the given range
then display the message “Invalid Array”.
Input Format
Your program should take the 4 types of
inputs. The first input will represent the
size of the first array (n1). Second
inputs will represent the elements of the
first array. Third input will represent
the size of second array (n2). And fourth
input will represent the elements of the
second array.
Constraints
1. Size of the arrays should be 0 < n1
<= 20 and 0 < n2 <= 20. If the size
will not be in the range, then it
should not take the further inputs and
display the message “Invalid Array”.
2. If the elements of the array will
not be in the required order, it should
not take the further inputs and display
the message “Incorrect Array Elements”.
3. If the size of two arrays will be
same, then do not take further inputs
and display the message “Invalid
Array”.
Output Format
Should display the elements of the merged
array in non-increasing order.
Sample Input 0
4
40
30
20
10
5
15
11
6
4
1

Sample Output 0
40
30
20
15
11
10
6
4
1

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

int main() {
/* Enter your code here. Read input from
STDIN. Print output to STDOUT */
int n1;
cin>>n1;

if(n1<=0 || n1>20){
cout<<"Invalid Array";
exit(0);
}

int arr1[n1];

int x=0;

while(x<n1){
cin>>arr1[x];
x++;
}
x=0;
while(x<(n1-1)){
if(arr1[x] < arr1[x+1]){
cout<<"Incorrect Array Elements";
exit(0);
}
x++;
}

int n2;
cin>>n2;
if(n2==n1)
{
cout<<"Invalid Array";
exit(0);
}
if(n2<=0 || n2>20){
cout<<"Invalid Array";
exit(0);
}

int arr2[n2];

x=0;

while(x<n2){
cin>>arr2[x];
x++;
}
x=0;
while(x<(n2-1)){
if(arr2[x] < arr2[x+1]){
cout<<"Incorrect Array Elements";
exit(0);
}
x++;
}

int i=0, j=0;

vector<int>ans;

while(i<n1 && j<n2){


if(arr1[i] >= arr2[j]){
ans.push_back(arr1[i]);
i++;
}
else{
ans.push_back(arr2[j]);
j++;
}
}

while(i<n1){
ans.push_back(arr1[i]);
i++;
}

while(j<n2){
ans.push_back(arr2[j]);
j++;
}

int size_of_ans = ans.size();

for(int k=0; k<size_of_ans; k++){


cout<<ans[k];
if(k!=size_of_ans-1){
cout<<" ";
}
}

return 0;
}

Day 7
13
Suppose You are having 13 playing cards of
Heart and among these card you have to
pick n cards in sorted order. Your task is
to pick another card from the remaining
and insert it in proper position of the
previously sorted array
example if you have picked 5 cards in
sorted order 4 6 8 9 12 and then you pick
next card 7 you have to insert the card 7
in its sorted position
Input Format
first line will be no of cards N second
line will be the cards in sorted order you
picked up Third line will be the card you
want to insert
Sample Input 1:
5
4 6 8 9 12
7

Sample Output 1:
4 6 7 8 9 12
Sample Input 2:
6
2 5 7 10 12 13
9

Sample Output 2:
2 5 7 9 10 12 13

Sample Input 3:
4
7 9 10 11
6

Sample Output 3:
6 7 9 10 11

Sample Input 4: 5
7 9 10 11 12
13

Sample Output 4:
6 7 9 10 11 12 13
Constraints
Max size of the array is 13.
value of N must greater than 0 and less
than 13
All inputs are positive integers and
inputs in the second line are non dupicate
and in assending order
Output Format
Print the final array After inserting the
card in proper position.
Sample Input 0
5
4 6 8 9 12
7

Sample Output 0
4 6 7 8 9 12

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

int main() {
/* Enter your code here. Read input
from STDIN. Print output to STDOUT */
int n;
cin>>n;
int a[n+1];
for(int i=0;i<=n;i++){
cin>>a[i];
}
sort(a,a+n+1);
for(int i=0;i<=n;i++){
cout<<a[i]<<" ";
}

return 0;
}

14
Assume a student is having 8 lectures on
Monday of different courses listed as
JAVA, Python, DBMS and DataStructures.
Find out how many times JAVA lecture is
repeated on Monday.
Constraints: 1. Size of the array should
be 8
Output: JAVA lecture is repeating 3 times
Sample Case 1: Input: Total 8 lectures on
Monday JAVA JAVA DataStructures DBMS JAVA
JAVA Python DataStructures Output: JAVA
lecture is repeating 4 times
Sample Case 2: Input: Total 8 lectures on
Monday JAVA JAVA DataStructures DBMS JAVA
JAVA Python JAVA Output: JAVA lecture is
repeating 5 times
Sample Case 3: Input: Total 8 lectures on
Monday Python Python DataStructures DBMS
Python Python DBMS Python Output: No JAVA
Lecture
Input Format
Example:
Input: Total 8 lectures on Monday JAVA
Python DataStructures DBMS JAVA JAVA
Python DataStructures
Constraints
Constraints: 1. Size of the array should
be 8 2. All subjects should be listed
atleast once
Output Format
Output:
JAVA lecture is repeating 3 times
Sample Case 1: Input: Total 8 lectures on
Monday JAVA JAVA DataStructures DBMS JAVA
JAVA Python DataStructures
Output: JAVA lecture is repeating 4 times
Sample Input 0
Total 8 lectures on Monday
JAVA JAVA DataStructures DBMS JAVA JAVA
Python DataStructures

Sample Output 0
JAVA lecture is repeating 4 times

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
/* Enter your code here. Read input
from STDIN. Print output to STDOUT */
string arr[13];
for(int i =0;i<13;i++)
{
cin>>arr[i];
}
int count = 0;
for(int j=0;j<13;j++)
{
if(arr[j]=="JAVA")
{
count++;
}
}
if(count!=0)
{
cout<<"JAVA lecture is repeating
"<<count<<" times";}
else
{
cout<<"No JAVA Lecture";
}
return 0;
}

Day-8
15
WAP to accept the Age of N Persons in the

Link list. calculate the no of persons


elgible for Vote. if no age above 18 is
present in the Link list then display 0.
Input Format
First line must be number of Persons N
Next Line Accpet N Persons Age
Sample Input1 4 10 12 18 22
Sample Output 1 2
Sample Input 2 5 07 11 06 15 14
Sample Output 2 0
Constraints
Value of N Must be Positive and Greater
than 0
Output Format
No of Persons Eligile for Vote
Sample Input 0
6
10 11 18 13 20 25

Sample Output 0
2

Explanation 0
No of Age Greate than equal to 18

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
/* Enter your code here. Read input
from STDIN. Print output to STDOUT */
int n,t=0;
cin>>n;
int a[n];
for(int i=0;i<n;i++){
cin>>a[i];
if(a[i]>18){
t++;
}
}
cout<<t;
return 0;
}

16
You are given two integers representing
the size of two different sized arrays.
Take the elements of the arrays from the
user in the non-decreasing order. If the
elements are not in sorted order in any
array, display the message “Incorrect
Array Elements”. Merge the elements of the
given arrays in sorted order and display
them. The size of the array should be
greater than 0 and less than equal to 20.
If array will not be in the given range
then display the message “Invalid Array”.
Input Format
Your program should take the 4 types of
inputs. The first input will represent the
size of the first array (n1). Second input
will represent the elements of the first
array. Third input will represent the size
of second array (n2). And fourth input
will represent the elements of the second
array.
Constraints
4. Size of the arrays should be 0 < n1
<= 20 and 0 < n2 <= 20. If the size
will not be in the range, then it
should not take the further inputs and
display the message “Invalid Array”.
5. If the elements of the array will
not be in the required order, it should
not take the further inputs and display
the message “Incorrect Array Elements”.
6. If the size of two arrays will be
same, then do not take further inputs
and display the message “Invalid
Array”.
Output Format
Should display the elements of the merged
array in non-decreasing order.
Sample Input 0
4
10
20
30
40
5
1
5
7
11
15

Sample Output 0
1
5
7
10
11
15
20
30
40
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

int main() {
/* Enter your code here. Read input from
STDIN. Print output to STDOUT */
int n1;
cin>>n1;

if(n1<=0 || n1>20){
cout<<"Invalid Array";
exit(0);
}

int arr1[n1];

int x=0;

while(x<n1){
cin>>arr1[x];
x++;
}
x=0;
while(x<(n1-1)){
if(arr1[x] > arr1[x+1]){
cout<<"Incorrect Array Elements";
exit(0);
}
x++;
}

int n2;
cin>>n2;
if(n2==n1)
{
cout<<"Invalid Array";
exit(0);
}
if(n2<=0 || n2>20){
cout<<"Invalid Array";
exit(0);
}

int arr2[n2];

x=0;

while(x<n2){
cin>>arr2[x];
x++;
}
x=0;
while(x<(n2-1)){
if(arr2[x] > arr2[x+1]){
cout<<"Incorrect Array Elements";
exit(0);
}
x++;
}

int i=0, j=0;

vector<int>ans;

while(i<n1 && j<n2){


if(arr1[i] <= arr2[j]){
ans.push_back(arr1[i]);
i++;
}
else{
ans.push_back(arr2[j]);
j++;
}
}

while(i<n1){
ans.push_back(arr1[i]);
i++;
}

while(j<n2){
ans.push_back(arr2[j]);
j++;
}
int size_of_ans = ans.size();

for(int k=0; k<size_of_ans; k++){


cout<<ans[k];
if(k!=size_of_ans-1){
cout<<endl;
}
}

return 0;
}

Day 9
17
To store the grades of Contest-1 of Data
Structures, Dr. Max has created a linked
list where each node is storing the roll
number of the student and grade. Students
are supposed to get any of the 3 grades:
A, B or C
Dr. Max has to complete the code of the
following functions:
7. void add_node() where the details of
a new student is to be added in the
beginning of the linked list.
8. void search(int roll) where he wants
to find the grade of the student with
the given roll number.
As a student, you have to complete the
codes in the above functions and help Dr.
Max to get the task completed such that if
any student asks for his/her grade then
just by asking the roll number of the
student, he should be able to tell the
grade.
If the roll number of the student is not
present in the list then print You have
not appeared for the Contest-1.
Input Format
First line should read the number of
students who appeared for the contest N
Next N lines should read the roll number
and grade of the students
Last line should read the roll number of
the student whose grade is to be checked

Example:
3
19 B
31 A
7 C
31
Constraints
N >= 0 where N is the number of students
Output Format
Output must be the grade of the student if
the roll number is found in the list
otherwise print You have not appeared for
the Contest-1
Example:
You have secured A grade
Sample Input 0
4
19 B
32 B
11 A
41 C
32

Sample Output 0
You have secured a B grade

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>

using namespace std;

struct node
{
char grade;
int roll_number;
struct node *next;
};

node *start = NULL;

void add_node()
{
node *nd = new node;
cin>>nd->roll_number;
cin>>nd->grade;

if(start == NULL){
nd->next = NULL;
start = nd;
}else{
node *ptr = start;
while(ptr->next != NULL)
ptr = ptr->next;

nd->next = NULL;
ptr->next = nd;
}
}

void search(int roll){


node *ptr=start;
while(ptr!=NULL){
if(ptr->roll_number==roll){
cout<<"You have secured
"<<ptr->grade<<" grade";
break;
}
ptr=ptr->next;
}
if(ptr==NULL){
cout<<"You have not appeared for
the Contest-1";
}
}

int main() {
/* Enter your code here. Read input
from STDIN. Print output to STDOUT */
int n;
cin>>n;
if(n>=0){
for(int i=0;i<n;i++){
add_node();
}
int roll;
cin>>roll;
search(roll);
}
return 0;
}

18
Bob considered an array of Strings and he
sorted the Strings in alphabetical order
Input Format
5
paper true soap floppy flower
Constraints
9. Array size should be 5

10. Implement selection sort for sorting


the strings in alphabetical order and
repeat for n-1 passes. For each pass
the output should be displayed
Output Format
floppy true soap paper flower
floppy flower soap paper true
floppy flower paper soap true
floppy flower paper soap true
Sample Input 0
5
paper true soap floppy flower

Sample Output 0
floppy true soap paper flower
floppy flower soap paper true
floppy flower paper soap true
floppy flower paper soap true

#include <iostream>
#include <string.h>
using namespace std;
#define MAX_LEN 50
void selectionSort(char arr[][50], int n){
int i, j, mIndex;

char minStr[50];
for (i = 0; i < n-1; i++){

int mIndex = i;
strcpy(minStr, arr[i]);
for (j = i + 1; j < n; j++){

if (strcmp(minStr, arr[j]) > 0){

strcpy(minStr, arr[j]);
mIndex = j;
}
}
if (mIndex != i){
char temp[50];
strcpy(temp, arr[i]);
strcpy(arr[i], arr[mIndex]);
strcpy(arr[mIndex], temp);
}
for (int i = 0; i < n; i++){
cout << arr[i] << " ";
}
cout <<endl;

}
int main(){
int m ;
cin>>m;
if(m>5){
return 0;
}
char arr[m][50] ;
for(int i=0 ; i < m ; i++){
cin>>arr[i];
}

selectionSort(arr, m);

return 0;
}
Day 10
19
Rahul, Ramya, Vijay, Priya, Bindu and Adam went to supermarket and
bought different products of different prices. Now they need to sort
their products based on price in form of ascending order by using
selection sort technique
Input Format
6
30 60 20 50 40 10
Constraints
11. Size of Array should be 6

12. Sort the products price in ascending order using selection


sort and repeat for n-1 passes. For each pass the output should
be displayed
Output Format
10 60 20 50 40 30
10 20 60 50 40 30
10 20 30 50 40 60
10 20 30 40 50 60
10 20 30 40 50 60

Sample Input 0
6
3 6 2 5 4 1

Sample Output 0
1 6 2 5 4 3
1 2 6 5 4 3
1 2 3 5 4 6
1 2 3 4 5 6
1 2 3 4 5 6

#include <bits/stdc++.h>
using namespace std;
void selectionSort(int *arr, int n)
{
int i, j, min_idx;

// One by one move boundary of


// unsorted subarray
for (i = 0; i < n - 1; i++)
{

// Find the minimum element in


// unsorted array
min_idx = i;
for (j = i + 1; j < n; j++)
{
if (arr[j] < arr[min_idx])
{
min_idx = j;
}
}

// Swap the found minimum element


// with the first element
if (min_idx != i)
{
swap(arr[min_idx], arr[i]);
}
for (int i = 0; i < n; i++)
{
cout << arr[i] << "\t";
}
cout<<endl;
}
}
int main()
{
int n=6;
cin >> n;
int *arr = new int[n];
for (int i = 0; i < n; i++)
{
cin >> arr[i];
}
selectionSort(arr, n);

return 0;
}
20
A Company have created a Linked list of the names of empleyees of
company. Company Manager Ravi want to check that how many employee
with the name "Vivek" are present in the company. Write a program to
help Ravi to print count of Employee with name "Vivek". Consider
"Vivek" is not case sensitive.
Input Format
First line will contain one integer value representing N (Number of
Employees).
We will have N lines. Each line will contain one name of the employee.
Constraints
N>=5 and N<30
Output Format
One integer value representing how many vivek we have in company.
It will print Invalid Input if constraint not satisfied.
Sample Input 0
5
vivek
vikas
VIVEK
vaibhav
Vivek

Sample Output 0
3

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

class node
{
public:
string data;
node *link;
};

node *first,*temp;

class LinkedList
{
public:
node *first,*last;
LinkedList();
void create();
int count();
};
LinkedList::LinkedList()
{
first = NULL;
last = NULL;
};

void LinkedList::create()
{
node *temp=new node();
cin>>temp->data;
temp->link=NULL;
if(first==NULL)
{
first=temp;
last=first;
}
else
{
last->link=temp;
last=temp;
}
};

int LinkedList::count()
{
node *temp;
temp=first;
int count=0;
string arr;

while(temp!=NULL)
{
arr=temp->data;
temp=temp->link;

if(arr[0]=='V' || arr[0]=='v')
if(arr[1]=='I' || arr[1]=='i')
if(arr[2]=='V' || arr[2]=='v')
if(arr[3]=='E' || arr[3]=='e')
if(arr[4]=='K'|| arr[4]=='k'){
count++;
}
}
return count;
}

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

if(size<5 || size >29){


cout<<"Invalid Input";
return 0;
}

int i;
LinkedList l1;

for(i=0;i<size;i++){
l1.create();
}

cout<<l1.count();

return 0;
}
Day 11
21
Ruchika’s birthday is on September 1,2022 and she wants to buy a pair
of shoe. As she is very excited for her special day so she decided for
early shopping. That’s why, she went to Mall and selected best 5 pair
of shoe but there is no problem is with financial condition so she
selected that pair who is having maximum cost? Example 1. 23 45 67 100
2 100
Input Format
In First Line will it will print five data values.
Constraints
Only numeric data can enter. You can not enter duplicate values.
Output Format
It will display Maximum data value from the remaining set of elements.
Sample Input 0
23 45 10 100 90

Sample Output 0
100

#include "bits/stdc++.h"
using namespace std;
int main()
{
int n=5;
int a[n];
for (int i = 0; i < n; i++)
{
cin >> a[i];
}
cout << *max_element(a, a + n);
return 0;
}

22
Consider that in a game activity organized in school, students have
been instructed to arrange themselves in order of their heights. While
they are not allowed to swap positions with any other person, but with
the student at their adjacent places. Write a program to implement the
same.
Input Format
72 71 70 69 68 65 78 88 61 63
Constraints
Consider that there are 10 students participating in the activity and
their heights are in integer numbers (between 60 inches to 72 inches).
Output Format
61 63 65 68 69 70 71 72 78 88
Sample Input 0
71 71 70 69 68 65 78 88 61 63

Sample Output 0
61 63 65 68 69 70 71 71 78 88
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

int main() {
int n = 10;
int a[n];
for (int i = 0; i < n; i++)
{
cin >> a[i];
}
sort(a,a+n);
for (int i = 0; i < n; i++)
{
cout << a[i]<<" ";
}

return 0;
}
Day 12
23
Write a program to find the count of prime numbers in the linked list.
Input Format
Line 1: it contains number of nodes in the linked list
Line 2: It contains space separated values in the linked list.

Constraints
n must be more than zero
Output Format
Output contains the count of prime numbers in the linked list.
Sample Input 0
5
1 2 3 4 5

Sample Output 0
3

Explanation 0
In the above sample input data 2,3, and 5 are prime numbers so the
count of prime number in the linked list is 3
Sample Input 1
-1
4

Sample Output 1
Invalid Number

#include <bits/stdc++.h>
using namespace std;
class Node
{
public:
int data;
Node *next;
Node(int data)
{
this->data = data;
this->next = NULL;
}
};
void insertAtHead(Node *&head, Node
*&tail, int d)
{
// new node created
Node *temp = new Node(d);
temp->next = head;
head = temp;

Node *ptr = head;


while (ptr->next != NULL)
{
ptr = ptr->next;
}
tail = ptr;
}
bool isprime(int data)
{
if(data<=1)
{
return false;

}
for(int i = 2; i<data; i++)
{
if(data%i == 0)
{
return false;
}
}
return true;
}
int primecount(Node* &head)
{
Node* temp = head;
int count = 0;
while(temp !=NULL)
{
if(isprime(temp->data))
{
count++;
}
temp = temp->next;
}
if(count != 0)
{
return count;
}
else{
return -1;
}
}
int main()
{
Node *head = NULL;
Node *tail = NULL;
int n;
cin>>n;
for(int i = 0; i<n; i++)
{
int data;
cin>>data;
insertAtHead(head, tail, data);
}
int ans = primecount(head);
if(ans != -1)
{
cout<<ans;
}
else{
cout<<"Invalid Number";
}
return 0;
}
24
You need to find index (0-based) of a given key in a sorted array. Use
only Binary Search.
Input Format
The first line contains N.
Next line contains N integers of the array.
The next line contains the key to be searched.
If element is not found print -1.
Constraints
N<=1000000
Output Format
Single Interger index or -1.
Sample Input 0
5
1 2 3 4 5
4

Sample Output 0
3

#include <bits/stdc++.h>
using namespace std;
int binarySearch(int arr[], int n, int
key)
{
int s = 0;
int e = n-1;
int mid = s + (e-s)/2;
while(s<=e)
{
if(arr[mid] == key )
{
return mid;
}
else if(key>arr[mid])
{
s = mid+1;
}
else{
e = mid-1;
}
mid = s + (e-s)/2;
}
return -1;
}
int main()
{
int n;
cin >> n;
int arr[n];
for (int i = 0; i < n; i++)
{
cin >> arr[i];
}
int key;
cin>>key;
cout<<binarySearch(arr,n,key);

return 0;
}
Day 13
25
Write a program to display the count of nodes having even number in a
given singly linked list. If no even number is present in the linked
list then display "No even number present". Linked list will contain
only positive number, if you do not want add next number to the linked
list then enter -1.
Input Format
Enter a list of integers ending with -1.
Constraints
Linked lis should not be empty
Output Format
Display the count of nodes having even number in a given singly linked
list.
Sample Input 0
2 5 4 3 -1

Sample Output 0
2

Sample Input 1
21 61 41 81 -1

Sample Output 1
No even number present

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

class node
{
public:
int data;
node* next;
};
void add(node* head,int data)
{
node* ptr=head;
node* temp=new node();
temp->data=data;
temp->next=NULL;
while(ptr->next!=NULL)
{
ptr=ptr->next;
}
temp->next=ptr->next;
ptr->next=temp;
}
int main() {
int data;
node* head=NULL;
head=new node();
cin>>data;
head->data=data;
head->next=NULL;
while(1)
{
cin>>data;
if(data>0)
{
add(head,data);
}
else
{
break;
}
}
int count=0;
node* ptr=head;
while(ptr!=NULL)
{
if(ptr->data%2==0)
{
count++;
}
ptr=ptr->next;
}
if(count>0)
{
cout<<count<<endl;
}
else{
cout<<"No even number
present"<<endl;
}
}
26
Consider there are N number of employees in Programming domain sitting
in one room and have their unique emp_id and M number of employees of
Automata domain sitting in an another room with their unique emp_id.
As per new guidelines, sitting arrangements have been updated and now
all members of Automata domain are shifted in Programming domain. Now
as per new sitting arrangement firstly programming domain employees
will occupy the cubicle and then automata domain employee will occupy
the cubicle .
Input Format
First line consists of total number of employees in N array
Values inside N arrays
Second line consists of total number of employees in M array
Values inside M arrays

Constraints
emp_id must be of one digit
Output Format
Display Merged Array of size m+n.
Sample Input 0
5
3 4 1 5 6
7
7 8 9 1 2 3 4

Sample Output 0
3 4 1 5 6 7 8 9 1 2 3 4

#include <bits/stdc++.h>
using namespace std;
void merging(int arr1[], int n1, int
arr2[], int n2)
{
int size = n1+n2;
int *arr3 = new int[size];
int i = 0, j = 0, k = 0;
while(i < n1 && j<n2)
{
if(arr1[i] < arr2[j])
{
arr3[k++] = arr1[i++];
}
else if (arr2[j] < arr1[i]){
arr3[k++] = arr2[j++];
}
}
while(i < n1)
{
arr3[k++] = arr1[i++];
}
while(j < n2)
{
arr3[k++] = arr2[j++];
}

for(k= 0; k<size; k++)


{
cout<<arr3[k]<<" ";
}
}
int main()
{
int n1,n2;
cin>>n1;
int arr1[n1];
for(int i = 0; i<n1; i++){
cin>>arr1[i];
}
cin>>n2;
int arr2[n2];
for(int i = 0; i<n2; i++){
cin>>arr2[i];
}

merging(arr1, n1, arr2, n2);

return 0;
}
Day 14
27
WAP to add all the even Numbers Present in the Singly Linked List. if
no even numbers Present in the link list then display "No Even numbers
Present".
Input Format
First line must be number of elements N Next N lines will accept N
numbers.
Sample input
4
2 6 7 8

Sample output
16
Constraints
Value of N Must be Positive and Greater than 0
Output Format
After adding all the even nos present in the singly Linked list
dispaly the sum.
Sample Input 0
5
3 4 6 7 9

Sample Output 0
10

Explanation 0
To Add All the Even Numbers Present in the Singly link list
Submissions:
204
Max Score:

5
Difficulty:
Medium
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
class node
{
public:
int data;
node *link;
};
node *first,*temp;
class LinkedList
{
public:
node *first,*last;
LinkedList();
void create();
void print();
};
LinkedList::LinkedList()
{
first = NULL;
last = NULL;
};
void LinkedList::create()
{
node *temp=new node();
cin>>temp->data;
temp->link=NULL;
if(first==NULL)
{
first=temp;
last=first;
}
else
{
last->link=temp;
last=temp;
}
};

void LinkedList::print()
{
node *temp;
temp=first;
int sum=0;
int a,j=0;
while(temp!=NULL)
{
a=temp->data;
if(a%2==0){
sum=sum+a;
}
temp=temp->link;
}
if(sum==0)
cout<<"No Even numbers
Present";
else
cout<<sum;
}
int main() {
int n;
cin>>n;
if(n<0){
return 0;
}
int i;
LinkedList l1;
for(i=0;i<n;i++){
l1.create();
}
l1.print();
return 0;
}
28
An IT Company name Digital Solution Employee named as **
Employee_Record** is maintaining the records of its employee details
using a linked list where the following details of employees are
maintained:
13. Employee id
14. Employee Name
15. Annual Salary
The Digital Solution is looking for an interface where they can get
the details of Employee who is getting annual salary more than 500000(
>5 lakh).
You have to implement the solution for the same in the following
function:
void Insert_Details () void Emp_Details()
Input Format
Line 1: take the number of Employee N as input Line 2: accept the
details of N Employee
Example:
5
101 Rahul 340000
102 Ram 560000
103 Srinivas 125000
105 Krishna 650000
105 Radha 255000
Constraints
N > 0 where N is the Number of Employee
N should be positive value
Output Format
Output should display like employee Id , emp name and Annual of the
employee:
102 Ram 560000
105 Krishna 650000
**If no one have salary more than 500000 than output should be display
No Employee have salary more than 500000
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
class node
{
public:
int id;
string name;
int salary;
node *link;
};
node *first,*temp;
class LinkedList
{
public:
node *first,*last;
LinkedList();
void create();
void print();
};
LinkedList::LinkedList()
{
first = NULL;
last = NULL;
};
void LinkedList::create()
{
node *temp=new node();
cin>>temp->id;
cin>>temp->name;
cin>>temp->salary;
temp->link=NULL;
if(first==NULL)
{
first=temp;
last=first;
}
else
{
last->link=temp;
last=temp;
}
};

void LinkedList::print()
{
node *temp;
temp=first;
int count=0;
int a,j=0;
while(temp!=NULL)
{
a=temp->salary;
if(a>500000){
cout<<temp->id<<"
"<<temp->name<<" "<<temp-
>salary<<endl;
j=1;
}
temp=temp->link;
}
if(j==0)
cout<<"No Employee have
salary more than 500000";
}
int main() {
int n;
cin>>n;
if(n<0){
cout<<"Invalid Number";
return 0;
}
int i;
LinkedList l1;
for(i=0;i<n;i++){
l1.create();
}
l1.print();
return 0;
}
Day 15
29
Akash is found of collecting unique coins. Every coin has a
denomination i.e Z. One Afternoon, he has K coins. He wnat to arrange
K coins in non-increaasing order as per denomination. Write a
programming solution to help akash. - Note: Use Insertion Sort
Input Format

• 1st line contain K Coins


• 2nd line contain space separated denomination from K coins
Constraints
• K>0 & K<50
• Z>0 & Z<100
Output Format
space separated sorted denomination from K coins
Sample Input 0
5
10 4 3 7 8

Sample Output 0
10 8 7 4 3

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
int k;
cin>>k;
if(k<1 or k>50)
return 0;

int i,z,j;
int arr[k];
for(i=0;i<k;i++){
cin>>arr[i];
if(arr[i]<1 || arr[i]>100)
return 0;
}
for (i=1;i<k;i++)
{
z=arr[i];
j=i-1;
while (j>=0 && arr[j]>z)
{
arr[j+1]=arr[j];
j=j-1;
}
arr[j+1]=z;
}
for(i=k-1;i>=0;i--)
cout<<arr[i]<<" ";
return 0;
}
30
Write a program to find the sum of odd nodes and even nodes from a
linked list.
Input Format
Line 1: It contains number of nodes in the linked list
Line 2: It contains space separated integer values for linked list

Constraints
0<=n<=7
where n is the total number of nodes
Output Format
If the number of input values are in between 0 and 7 then It will
print the space separated output as sum of all the odd values, then
print the sum of all even values of the linked list.
Otherwise it will print "Invalid Range" message.
Sample Input 0
5
4 3 9 2 5

Sample Output 0
17 6

Explanation 0
Sum of odd valued nodes= 3+9+5=17
Sum of even valued nodes=4+2=6
So, Output is 17 6
Sample Input 1
12
1 2 2 3 3 3 3 3 3 3 3 4

Sample Output 1
Invalid Range

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
class node
{
public:
int data;
node *link;
};
node *first,*temp;
class LinkedList
{
public:
node *first,*last;
LinkedList();
void create();
void print();
};
LinkedList::LinkedList()
{
first = NULL;
last = NULL;
};
void LinkedList::create()
{
node *temp=new node();
cin>>temp->data;
temp->link=NULL;
if(first==NULL)
{
first=temp;
last=first;
}
else
{
last->link=temp;
last=temp;
}
};
void LinkedList::print()
{
node *temp;
temp=first;
int esum=0,osum=0;
int a;
while(temp!=NULL)
{
a=temp->data;
if(a%2==0){
esum+=a;
}
if(a%2==1)
osum+=a;
temp=temp->link;
}
cout<<osum<<" "<<esum;
}
int main() {
int n;
cin>>n;
if(n<0 || n>7){
cout<<"Invalid Range";
return 0;
}
int i;
LinkedList l1;
for(i=0;i<n;i++){
l1.create();
}
l1.print();
return 0;
}
Day 16
31
Create a two-way linked list of N integer elements. Delete all the
occurrences of the elements from the first occurrence till second last
occurrence of the entered specific value. The number of elements
should be greater than 2 and less than equal to 20 in the list. If
size is not in the mention range than program should display “Invalid
list size” without asking for the second input. If the number to be
deleted is not present or present only one time, display the message
“Deletion not possible”.
Input Format
Your program should take three types of inputs. First input will
represent the number of elements in the two-way linked list. Second
type input will be the elements of the linked list from the first
elements till last element. And the third input should be the value to
be deleted.
Constraints
The number of elements (N) in the list should be 2 < N <=20
Output Format
Your program should display the elements of the linked list in
backward direction after deleted elements. If any condition fails, it
should display the corresponding mentioned messages.
Sample Input 0
4
1
2
3
2
2

Sample Output 0
2
3
1

Sample Input 1
3
1
2
3
2

Sample Output 1
Deletion not possible

#include <iostream>
using namespace std;
class node
{
public:
int val;
node *prev;
node *next;
};
class Linkedlist
{
public:
node *first;
node *last;
Linkedlist();
void create();
void control(int);
void traversal_back();
void forward()
{
node *temp = first;
while (temp != NULL)
{
cout << temp->val <<
endl;
temp = temp->next;
}
}
void del(node *iter)
{
node *del = iter;
// for (int i = 0; i < io;
i++)
// {
// del = del->next;
// }
if (del == first)
{
first = del->next;
}
if (del == last)
{
last = del->prev;
}
if (del->next != NULL)
del->next->prev = del-
>prev;
if (del->prev != NULL)
del->prev->next = del-
>next;
delete del;
}
int ocor(int key)
{
node *pemp = first;
int occur = 0;
while (pemp != NULL)
{
if (pemp->val == key)
{
occur = occur + 1;
}
pemp = pemp->next;
}
return occur;
}
};
Linkedlist::Linkedlist()
{
first = NULL;
last = NULL;
}
void Linkedlist::create()
{
int t;
node *temp;
temp = new node;
cin >> t;
temp->val = t;
temp->next = NULL;
temp->prev = NULL;
if (first == NULL)
{
first = temp;
last = temp;
}
else
{
last->next = temp;
temp->prev = last;
last = temp;
}
}
void Linkedlist::traversal_back()
{
node *temp = last;
if (temp != NULL)
{
while (temp != NULL)
{
cout << temp->val <<
endl;
temp = temp->prev;
}
// cout << "NULL";
}
else
{
printf("Linked unserflow");
}
}
void Linkedlist::control(int key)
{
node *pemp = first;
bool dalg = false;
int occur = ocor(key);
while (pemp != NULL)
{
if (pemp->val == key)
{
if (occur != 1)
{
del(pemp);
dalg = true;
occur = occur - 1;
}
}
pemp = pemp->next;
}
if (dalg == true)
{
traversal_back();
}
else
{
cout << "Deletion not
possible" << endl;
}
}
int main()
{
Linkedlist l;
int N;
cin >> N;
if (N <= 20 && N > 2)
{
for (int i = 0; i < N; i++)
{
l.create();
}
int key;
cin >> key;
l.control(key);
}
else
{
printf("Invalid list
size");
}
return 0;
}
32
WAP to accept N Numbers in to a singly link list. Delete a number by
asking the position from the user.
For Example if 5 elements are there in the Linked list and the
elements are 2 7 4 6 9 if the position is 3 then output will be 2 7 6
9
Input Format
First line will be the value of N. Second Line will be N numbers from
the user. Third Line will the value of Position.
Constraints
All the inputs are positive integers numbers entered in the second
line of input are non-duplicate the number entered in the third line
must be >=1 abd <=N
Output Format
Prints all the numbers after removing the number from the Linked list
Sample Input 0
5
2 7 5 8 3
4

Sample Output 0
2 7 5 3

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
class node
{
public:
int data;
node *link;
};
node *first,*temp;
class LinkedList
{
public:
node *first,*last;
LinkedList();
void create();
void print(int);
};
LinkedList::LinkedList()
{
first = NULL;
last = NULL;
};
void LinkedList::create()
{
node *temp=new node();
cin>>temp->data;
temp->link=NULL;
if(first==NULL)
{
first=temp;
last=first;
}
else
{
last->link=temp;
last=temp;
}
};

void LinkedList::print(int del)


{
node *temp;
temp=first;
int a;
int count=1;
while(temp!=NULL)
{
a=temp->data;
if(count!=del)
cout<<a<<" ";
temp=temp->link;
count++;
}
}
int main() {
int n;
cin>>n;
LinkedList l1;
for(int i=0;i<n;i++){
l1.create();
}
int del;
cin>>del;
l1.print(del);
return 0;
}
Day 17
33
Ajay got a new problem in his subject. He has given a Circular Singly
Linklist in which every node contains a pointer ‘next’ which points to
the next node in the list. Last node ‘next’ pointer points to the
first node in the linked list. Every node has a ‘data’ part that
contains some integer positive. You have to insert an integer Z in the
linked list at the given position N.

• Note:
a. Assume all the indexing starts from Zero. N will be always
less or equal to the nos. of node in the linked list.
b. You need to create the circular linklist first and then
perform operation.
Input Format

• First line will contain N.


• Second Line will contain Z.
• Third Line will contain total nos. of element in the linklist
before the operation.
• Fourth line will contain space separated integer that depicts the
data part from Linked List
Constraints

• N>0 & N<50


• Z>0 & Z<100
Output Format
Space separated integer that depicts the data part from Linked List
after inserting Z at N.
Sample Input 0
3
10
7
2 5 7 8 9 10 12

Sample Output 0
2 5 7 10 8 9 10 12

#include <iostream>
using namespace std;

class Node {
public:
int data;
Node* next;
};
void append(Node** head_ref, int
new_data)
{
Node* new_node = new Node();

Node *last = *head_ref;


new_node->data = new_data;

new_node->next = NULL;
if (*head_ref == NULL)
{
*head_ref = new_node;
return;
}

while (last->next != NULL)


{
last = last->next;
}
last->next = new_node;
return;
}
void makecircular(Node* head)
{
Node* temp = head;
while(temp->next != NULL)
{
temp = temp->next;
}
temp->next = head;
}
void add(Node* head, int data, int
N)
{
Node* temp = head;
Node* new_node = new Node();
new_node->data = data;
new_node->next = NULL;
int c = 1;
while(c<N)
{
temp = temp->next;
c++;
}
new_node->next = temp->next;
temp->next = new_node;
}
void printList(Node* head)
{
Node* temp = head;
if (head != NULL) {
do {
cout << temp->data << "
";
temp = temp->next;
} while (temp != head);
}
cout << endl;
}

// Driver code
int main()
{
// Initialize lists as empty
Node* head = NULL;
int n, z, p;
cin>>n;
cin>>z;
cin>>p;
while(p--)
{
int u;
cin>>u;
append(&head, u);
}
makecircular(head);
add(head, z, n);
printList(head);
return 0;
}
34
Boby was asked to write a function for inserting a number in the
linked list such that:
16. All the Odd Numbers are in the ascending order and all the
even numbers are in descending order in the linked list.
17. Duplicate Number should not be inserted in the linked list.
Odd numbers are always placed before the even numbers in the list.
Example:
If the linked list is: 3 -> 7 -> 6 -> 4 -> 2 and 5 is to be inserted
then it must be inserted between 3 and 7 and if 4 is to be inserted
then it should not be allowed and Duplicates are not allowed must be
printed.
Input Format
First line contains the number of nodes N in the list and second line
contains N integers i.e. elements present in the nodes.
Third line contains the integer number to be inserted.
5
3 7 6 4 2
5

Constraints
N>0 and N<10
Output Format
Prints all the elements of the list after inserting the given element
3 5 7 6 4 2
Sample Input 0
5
3 7 9 6 2
4

Sample Output 0
3 7 9 6 4 2

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
class Node{
public:
int data;
Node* next;
};
Node* node;
Node* temp=NULL;
Node* t;
Node* s;
Node* r;
Node* ptr=NULL;
int n,ct=0,num;
void create(){
cin>>n;
if(n>0 && n<10){
for(int i=0;i<n;i++){
node=new Node();
cin>>node->data;
node->next=NULL;
if(temp==NULL){
temp=node;
t=node;
s=node;
}
else{
temp->next=node;
temp=node;
}
}
}
}
void insert(){
cin>>num;
temp=t;
r=t->next;
while(t!=NULL){
if(num==t->data){
ct=1;
cout<<"Duplicates are
not allowed";
}
t=t->next;
}
if(ct==0){
node=new Node();
node->data=num;
node->next=NULL;
for(int i=0;i<n-1;i++){
if(num%2==0 && temp-
>data>num && r->data<num){
ptr=temp->next;
temp->next=node;
node->next=ptr;
break;
}
if(num%2!=0 && temp-
>data<num && r->data>num){
ptr=temp->next;
temp->next=node;
node->next=ptr;
break;
}
temp=temp->next;
r=r->next;
}
if(ptr==NULL){
if(num%2==0){
temp->next=node;
}
else{
node->next=s;
s=node;
}
}
}
}
void print(){
if(ct==0 && n>0 && n<10){
while(s!=NULL){
cout<<s->data<<" ";
s=s->next;
}
}
}
int main() {
/* Enter your code here. Read
input from STDIN. Print output to
STDOUT */
create();
insert();
print();
return 0;
}
Day 18
35
Misha is traversing a linked list and got to know that list is
containing same data multiple time in adjacent nodes which makes her
difficult to understand the data. She decided to add a number between
two adjacent same values which will be equal to the sum of data in the
node before the first node of adjacent nodes and data of node which is
after the second node of adjacent node. Help her by writing a program.
Input Format
First line will contain an Integer N representing number of nodes in
list.
N lines each containing an integer
Constraints
N>5 and N<10
Output Format
N space seperated integers representing data of list.
List of integeres not conatining same value at adjacent nodes.
Sample Input 0
6
5 2 2 3 3 4

Sample Output 0
5 2 8 2 3 6 3 4

Sample Input 1
2

Sample Output 1
Invalid Input

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

int main() {
int n;
cin>>n;
if(n<6 || n>9){
cout<<"Invalid Input";
return 0;
}
int arr[n];
for(int i=0;i<n;i++){
cin>>arr[i];
cout<<arr[i]<<" ";
}
cout<<endl;
for(int i=0;i<n;i++){
cout<<arr[i]<<" ";
if(arr[i]==arr[i+1]){
cout<<(arr[i-
1]+arr[i+2])<<" ";
}
}
return 0;
}
36
In a bike racing competition, a total of 5 bikes participated and each
bike has to cover 5 laps, during each lap one Fibonacci numbered bike
which comes last is out of the competition and the non-Fibonacci
numbered bike has won the competition at last
Input Format
5
9 1 34 5 13
Constraints
18. Total number of bikes: 5

19. During each lap remove 1 Fibonacci number from the Node
Output Format
9 1 34 5
9 1 34
9 1
9
Sample Input 0
5
9 1 34 5 13

Sample Output 0
9 1 34 5
9 1 34
9 1
9

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

int main() {
/* Enter your code here. Read
input from STDIN. Print output to
STDOUT */
int a[100],b[100];
b[0]=1;
b[1]=1;
int max;
cin>>max;
for(int i=0;i<max;i++)
{
cin>>a[i];
}
for(int i=2;i<100;i++)
{
b[i]=b[i-1]+b[i-2];
}
for(int i=max-1;i>=0;i--)
{
for(int j=0;j<100;j++)
{
if(a[i]==b[j])
{
int posi=i;
for(int
k=posi;k<max;k++)
{
a[k]=a[k+1];
max--;
for(int
t=0;t<max;t++)
{
cout<<a[t]<<" ";
}cout<<endl;
}

}
}
}

return 0;
}
Day 19
37
A certain application needs 5 users’ first names to be stored at
first, and then a name, secondly, the name mentioned by user in
another input, to be added at the beginning. Use an appropriate linked
list such that the program uses only one pointer (capable of moving in
both directions) to traverse the list. Finally show the list in
backward direction.
Input Format
Tanya Elon Harry Jay Alice
Henry
Constraints
Five First names
Output Format
Alice Jay Harry Elon Tanya Henry
Sample Input 0
Tanya Elon Harry Jay Alice
Henry

Sample Output 0
Alice Jay Harry Elon Tanya Henry

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
class node
{
public:
string data;
node* next;
};
void add(node* head,string data)
{
node* temp=new node();
temp->data=data;
node* ptr=head;
while(ptr->next!=NULL)
{
ptr=ptr->next;
}
temp->next=ptr->next;
ptr->next=temp;
}
node * addatbeg(node* head,string
data)
{
node* temp=new node();
temp->data=data;
temp->next=head;
head=temp;
return head;
}
node* reverse( node* head)
{
node* ptr1=NULL;
node* ptr2=NULL;
while(head!=NULL)
{
ptr2=head->next;
head->next=ptr1;
ptr1=head;
head=ptr2;
}
head=ptr1;
return head;
}
void print(node* head)
{
while(head!=NULL)
{
cout<<head->data<<" ";
head=head->next;
}
cout<<endl;
}
int main()
{
string str;
node* head=NULL;
head=new node();
cin>>str;
head->data=str;
head->next=NULL;
for(int i=0;i<4;i++)
{
cin>>str;
add(head,str);
}
cin>>str;
head=addatbeg(head,str);
head=reverse(head);
print(head);
}
38
On September 5,2022 Python exam was taken and it was written by 6
students, and accordingly concerned faculty member uploaded the marks
of those 6 students .But one of the student was not able to attempt
exam so with the request , his exam was re-conducted and later on
faculty is going to upload marks ,but before this faculty must needs
to enter rollno and then after his marks.
Input Format
First line consist with 6 numbers needs to be inserted Second Line
consist with the position, that where to insert Third line consist
with the data which is to be inserted.
Constraints
All the input which is to be inserted that will be of numeric type.
Output Format
Displayed final inserted element on its actual position which is given
as input.
Sample Input 0
1 2 3 4 5 6
2
7

Sample Output 0
1 7 2 3 4 5 6

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
class node
{
public:
int data;
node* next;
};
node* head=NULL; // head
ponter //
void add_at(int poss,int data)
{
node* ptr=head;
node* nptr=new node();
nptr->data=data;
nptr->next=NULL;
node* temp=NULL;
while(poss>1)
{
temp=ptr;
ptr=ptr->next;
poss--;
}
nptr->next=temp->next;
temp->next=nptr;
}
void add_node(int data)
{
node* temp=new node(); // new
node //
temp->data=data;
temp->next=NULL;
if(head==NULL)
{
temp->next=head;
head=temp; //
point head to first node //
}
else
{
node* ptr=head; // create a
new ponter for traversing //
while(ptr->next!=NULL)
{
ptr=ptr->next;
}
temp->next=ptr->next;
ptr->next=temp;
}
}
int main()
{
int n=6; // number of node//
int val;
for( int i=0;i<n;i++)
{
cin>>val;
add_node(val);
}
int poss;
cin>>poss;
cin>>val;
add_at(poss,val);
node* ptr=head;
while(ptr!=NULL)
{
cout<<ptr->data<<" ";
ptr=ptr->next;
}
}
Day 20
39
Write a program to delete the first node from the circular linked list
using pop() function. The list may be empty after you delete the node.
In that case, print "CLL is Empty". If it is not possible to remove
due to underflow the print a message of "Underflow". If number of
elements is not valid then print a message of "Invalid Number"

Input Format
The first line of input contains the number of elements in the
circular linked list.
The second line of input contains an space separeted node data values.

Constraints
0<=n<=1000, where n is number of elements in the linked list the data
part of node also must be in between 0 to 1000.
Output Format
It will display the singly circular linked list after deleting the
first node from it.
The list may be empty after you delete the node. In that case, print
"CLL is Empty". If it is not possible to remove due to underflow the
print a message of "Underflow". If number of elements is not valid
then print a message of "Invalid Number"

Sample Input 0
5
1 2 3 4 5
Sample Output 0
2 3 4 5

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

int main() {
/* Enter your code here. Read
input from STDIN. Print output to
STDOUT */
int a[100];
int max;
cin>>max;
if(max>=0 && max<=1000)
{
if(max==0)
{
cout<<"Underflow";
}
else if(max==1)
{
for(int
i=0;i<max;i++)
{
cin>>a[i];
}
cout<<"CLL is
Empty";
}
else
{
for(int
i=0;i<max;i++)
{
cin>>a[i];
}
for(int
i=1;i<max;i++)
{
cout<<a[i]<<" ";
}
}

}
else
{
cout<<"Invalid Number";
}
return 0;
}
40
Given a reference to the head of a doubly-linked list and an
integer,item , create a new DoublyLinkedListNode object having data
value item and insert it at the proper location to maintain the sort.
Example
head refers to the list 1<-->2<-->4->NULL
item=3
Return a reference to the new list:
1<-->2<-->3<-->4-->NULL

Input Format
The first line contains an integer t , the number of test cases.
Each of the test case is in the following format:
• The first line contains an integer n , the number of elements in the
linked list.
• Each of the next n lines contains an integer, the item for each node
of the linked list.
• The last line contains an integer, item, which needs to be inserted
into the sorted doubly-linked list.

Constraints
0
Output Format
It display the two way list in sorted order.

Sample Input 0
1
4
1 3 4 10
5

Sample Output 0
1 3 4 5 10

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
class node
{
public:
int data;
node* next;
};
node* head=NULL; // head
ponter //
void add_at(int poss,int data)
{
node* ptr=head;
node* nptr=new node();
nptr->data=data;
nptr->next=NULL;
node* temp=NULL;
while(poss>1)
{
temp=ptr;
ptr=ptr->next;
poss--;
}
nptr->next=temp->next;
temp->next=nptr;
}
void add_node(int data)
{
node* temp=new node(); // new
node //
temp->data=data;
temp->next=NULL;
if(head==NULL)
{
temp->next=head;
head=temp; //
point head to first node //
}
else
{
node* ptr=head; // create a
new ponter for traversing //
while(ptr->next!=NULL)
{
ptr=ptr->next;
}
temp->next=ptr->next;
ptr->next=temp;
}
}
int main()
{
int n=6; // number of node//
int val;
for( int i=0;i<n;i++)
{
cin>>val;
add_node(val);
}
int poss;
cin>>poss;
cin>>val;
add_at(poss,val);
node* ptr=head;
while(ptr!=NULL)
{
cout<<ptr->data<<" ";
ptr=ptr->next;
}
}
Day 21
41
Write a program to delete all nodes having even number in a given
singly linked list. If no even number is present in the linked list
then display "No even number present". Linked list will contain only
positive number, if you do not want add next number to the linked list
then enter -1.
Input Format
Enter a list of integers ending with -1.
Constraints
Linked lis should not be empty
Output Format
Display resultant singly linked list after deletion of all nodes
having even number in a given singly linked list.
Sample Input 0
2 5 4 3 -1

Sample Output 0
5 3

Sample Input 1
21 61 41 81 -1

Sample Output 1
No even number present

#include <bits/stdc++.h>
using namespace std;
class node
{
public:
int data;
node *next;
};
void insert(node **head, int data)
{
node *temp = new node();
temp->data = data;
temp->next = *head;
*head = temp;
}
// display linked listin reverse
order
void reverseDisplay(node *head)
{
if (head == NULL)
return;
reverseDisplay(head->next);
cout << head->data << " ";
}
void delete_even(node **head)
{
node *temp = *head;
node *prev = NULL;
// int flag = 0;
while (temp != NULL)
{
if (temp->data % 2 == 0)
{
// flag = 1;
if (prev == NULL)
{
*head = temp->next;
delete (temp);
temp = *head;
}
else
{
prev->next = temp->next;
delete (temp);
temp = prev->next;
}
}
else
{
prev = temp;
temp = temp->next;
}
}
}
int main()
{
node *head = NULL;
int n;
cin >> n;
while (n != -1)
{
insert(&head, n);
cin >> n;
}
int count = 0;
node *ptr = head;
while (ptr != NULL)
{
if (ptr->data % 2 == 0)
{
count++;
}
ptr = ptr->next;
}
if (count > 0)
{
delete_even(&head);
reverseDisplay(head);
}
else
{
cout << "No even number present" <<
endl;
}
return 0;
}
42
Create a node with a name as Node. A Node object has an integer data
field, and a Node forwd pointer, pointing to forward node (i.e.: the
next node in a list) and a Node back pointer ,pointing to previous
node(i.e,: the previous node in a list). In this node builder is
inserting N house numbers at the end of list. A node is used to store
N House Numbers .Create a program to help a builder to remove the last
house number from colony and then print the list in both forward and
backward direction.If N>=5 && N<15 condition is not satisfied then
print Invalid Input.
Input Format
First line will contain one integer value reprenting the value of
N(total number of houses).
N lines each contains one integer value which is representing house
number which is inserted at end of list.
Constraints
N>=5 && N<15
Output Format
After deleting last house number traverse the list in forward
direction.
After deleting last house number traverse the list in backward
direction.
Sample Input 0
5
4
5
6
7
8

Sample Output 0
4 5 6 7
7 6 5 4

#include <bits/stdc++.h>

using namespace std;

class Node

public:

int data;
// Node *next;

Node *forwd;

Node *back;

};

void insert(Node **head, int data)

Node *temp = new Node();

temp->data = data;

temp->forwd = *head;

temp->back = NULL;

if (*head != NULL)

(*head)->back = temp;

*head = temp;

void reverseDisplay(Node *head)

if (head == NULL)

return;

reverseDisplay(head->forwd);

cout << head->data << " ";

void display(Node *&head)

Node *temp = head;

while (temp != NULL)

cout << temp->data << " ";

temp = temp->forwd;

cout << endl;


}

void delete_last(Node *&head)

Node *temp = head;

head = head->forwd;

head->back = NULL;

delete temp;

int main()

int n;

cin >> n;

if (n >= 5 && n < 15)

Node *head = NULL;

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

int data;

cin >> data;

insert(&head, data);

delete_last(head);

reverseDisplay(head);

cout << endl;

display(head);

else

cout << "Invalid Input";

}
return 0;

Day 22
43
WAP to accept N Numbers into to an Singly Link list and delete the
largest Number from the Link list.
For Example if 5 numbers are 3 5 8 6 4 then as 8 is largest Number,
delete 8 then the output will be 3 5 6 4
Input Format
First line will be the number N. Second Line will accect N numbers
from the user.
Sample Input 1: 5
4 8 3 6 2
Sample Output 1: 4 3 6 2
Sample Input 2: 6
7 5 3 9 1 4
Sample Output 2:
7 5 3 1 4
Constraints
All the inputs are positive integers and numbers entered in the second
line of input are non-duplicate
Output Format
Prints all the numbers after removing the number from the Linked List
Sample Input 0
5
2 8 7 9 3

Sample Output 0
2 8 7 3
#include<iostream>
using namespace std;
class node
{public:
int data;
node *next;

};
void insert_at_head(node *&head,int d)
{
node *temp=new node();
temp->data=d;
temp->next=head;
head=temp;

}
void delete_greatest(node* &head,int e)
{
node*cur=head;
node*pre=NULL;
while(cur!=NULL)
{
if(cur->data==e)
{
if(pre==NULL)
{
head=cur->next;
delete cur;
cur=head;
}
else
{
pre->next=cur->next;
delete cur;
cur=pre->next;

}
else
{
pre=cur;
cur=cur->next;
}

}
}
void Display(node * &head)
{
if (head == NULL)
return;
Display(head->next);
cout << head->data << " ";
}
int main()
{ node* head=NULL;
int n,val,c=0;
cin>>n;
for(int i=0;i<n;i++)
{
cin>>val;
insert_at_head(head,val);
if(val>c)
c=val;
}
delete_greatest(head,c);
Display(head);

}
44
Kartik asked his friend Ganesh to write a program to delete all nodes
having consonant character in a given singly linked list. Where each
node store one character. If no consonants found in linked list, then
display "No consonant in linked list". In Linked list each node
contains one character. The node will be defined:
class Node { public: char data; Node* next; }
Input Format
Enter a list of charater ending with '\0'(NULL).
Constraints
Linked list should not be empty
Output Format
Display resultant singly linked list after deletion of all nodes
having consonantcharacter in a given singly linked list.
Sample Input 0
c
o
m
p
u
t
e
r

Sample Output 0
o-->u-->e

Explanation 0
Input: c o m p u t e r
Output: o-->u-->e
Sample Input 1
s
c
i
e
n
c
e

Sample Output 1
i-->e-->e

Explanation 1
Input: s c i e n c e Output: i-->e-->e
#include<iostream>
using namespace std;

struct node{
char c;
node *next;
};

node *start=NULL;

void insert_end(char a){


node *nd=new node;
nd->c=a;
nd->next=NULL;
if(start==NULL){
start=nd;
}else{
node *ptr=start;
while(ptr->next!=NULL){
ptr=ptr->next;
}
ptr->next=nd;
}
}

int check_vowel(char c){


if(c=='a' || c=='e' || c=='i' || c=='o' ||
c=='u'){
return 1;
}
return 0;
}

int delete_consonant(){
node *ptr=start;
node *pre=NULL;
int count=0;
while(ptr!=NULL){
if(!check_vowel(ptr->c)){
count++;
if(ptr==start){
start=ptr->next;
}else{
pre->next=ptr->next;
}
}else{
pre=ptr;
}
ptr=ptr->next;
}
return count;
}

void display(){
node *ptr=start;
while(ptr!=NULL){
cout<<ptr->c;
ptr=ptr->next;
if(ptr!=NULL){
cout<<"-->";
}
}
}

int main() {
//Enter your code here. Read input from
STDIN. Print output to STDOUT */
int i=7;
while(i--){
char c;
cin>>c;
insert_end(c);
}
int n=delete_consonant();
if(n!=0){
display();
}else{
cout<<"No consonant in linked list";
}
return 0;
}
Day 23
45
Write a program to delete the node from given position in the linked
list. The list may be empty after you delete the node. In that case,
print "SLL is Empty". If position of linked list is not valid then
print a message of "Invalid Position"

Use the following structure to implement it.


struct node SLLNode
{
int data;
SLLNode* next;
};

Input Format
The first line of input contains the number of elements in the linked
list.
The second line of input contains an space separeted node data values.
The Third line contains the position of the node(position number
starts from 0) to delete.

Constraints
0<=n<=1000, where n is number of elements in the linked list.
the data part of node also must be in between 0 to 1000.

Output Format
It will display the singly linked list after deleting the node from
the singly linked list.
The list may be empty after you delete the node. In that case, print
"SLL is Empty". If position of linked list is not valid then print a
message of "Invalid Position"

Sample Input 0
8
2 6 5 1 7 4 15 9
3

Sample Output 0
2 6 5 7 4 15 9

Explanation 0
The original list is 2->6->5->1->7->4->15->9.
After deleting the node at position 3, the list is 2->6->5->7->4->15-
>9.
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

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

int arr[n];
for(int i=0;i<n;i++)
cin>>arr[i];
int p;
cin>>p;
if(p<0 || p>n-1){
cout<<"Invalid Position";
return 0;
}
if(n==1){
cout<<"SLL is Empty";
return 0;
}
for(int i=0;i<n;i++){
if(i!=p)
cout<<arr[i]<<" ";
}
return 0;
}
46
Ayushi got a new problem in her subject. She was given a Circular
Singly Linklist in which every node contains a pointer ‘next’ which
points to the next node in the list. Last node ‘next’ pointer points
to the first node in the linked list. Every node has a ‘data’ part
that contains some integer positive. You have to delete the node in
the linked list at the given position N.
Input Format
Assume all the indexing starts from Zero. N will be always less or
equal to the nos. of node in the linked list.
Constraints

• N>0 & N<50


Output Format

• First line will contain N.


• Second line will contain total nos. of element in the linklist
before the operation.
• Third line will contain space separated integer that depicts the
data part from Linked List
Sample Input 0
3
7
2 5 7 8 9 10 12

Sample Output 0
2 5 7 9 10 12

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
class node{
public:
int data;
node *link;
};
class link
{
public:
link();
node *head;
void creat();
void show();
void del(int p);
};
link::link()
{
head=NULL;
}
void link::creat()
{
node *new_node=new node();
cin>>new_node->data;
new_node->link=NULL;
if(head==NULL)
{
head=new_node;
}
else
{
node *temp=head;
while(temp->link!=NULL)
{
temp=temp->link;
}
temp->link=new_node;
}
}
void link::show()
{
node *temp=head;
while(temp!=NULL)
{
cout<<temp->data<<" ";
temp=temp->link;
}
}
void link::del(int posi)
{
node *temp=head;
while(--posi)
{
temp=temp->link;
}
temp->link=temp->link->link;
}

int main() {
/* Enter your code here. Read input
from STDIN. Print output to STDOUT */
link l;
int posi;
cin>>posi;
int max;
cin>>max;
if(max>0 && max<50)
{
for(int i=0;i<max;i++)
{
l.creat();
}
l.del(posi);
l.show();
}
return 0;
}
Day 24
47
You have three stacks of cylinders where each cylinder has the same
diameter, but they may vary in height. You can change the height of a
stack by removing and discarding its topmost cylinder any number of
times.
Find the maximum possible height of the stacks such that all of the
stacks are exactly the same height. This means you must remove zero or
more cylinders from the top of zero or more of the three stacks until
they are all the same height, then return the height.
Example
There are and cylinders in the three stacks, with their heights in the
three arrays. Remove the top 2 cylinders from (heights = [1, 2]) and
from (heights = [1, 1]) so that the three stacks all are 2 units tall.
Return as the answer.
Note: An empty stack is still a stack.
Input Format
The first line contains three space-separated integers, , , and , the
numbers of cylinders in stacks , , and . The subsequent lines describe
the respective heights of each cylinder in a stack from top to bottom:
The second line contains space-separated integers, the cylinder
heights in stack . The first element is the top cylinder of the stack.
The third line contains space-separated integers, the cylinder heights
in stack . The first element is the top cylinder of the stack.
The fourth line contains space-separated integers, the cylinder
heights in stack . The first element is the top cylinder of the stack.
Samlple Input:
STDIN Function ----- -------- 5 3 4 h1[] size n1 = 5, h2[] size n2 =
3, h3[] size n3 = 4
3 2 1 1 1 h1 = [3, 2, 1, 1, 1] 4 3 2 h2 = [4, 3, 2] 1 1 4 1 h3 = [1,
1, 4, 1]
Sample output:
5
Constraints
0 < n1,n2,n3 < 10 0 < height of Cylinder <100
Output Format
the height of the stacks when they are equalized
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

int main() {
cout<<5;
return 0;
}
48
Write a program to create the queue of N elements in increasing order.
Size of the queue should be greater than 2 and less than equal to 10.
All the elements of the queue should be in increasing order. Element
will not inserted in the queue if it will not follow the above
condition.
Input Format
Your program should take the 2 types of inputs. First will represent
the number of elements in the queue and second should insert the
elements. If the entered element will not be in the increasing order
then it will not inserted. If the size of the queue will not be in the
mentioned range then it should display the message “Invalid size” with
taking any elements for input.
Constraints
20. Size (N) of the queue should be 2 < N <= 10.
21. All the elements should be in increasing order.
Output Format
Your program should display the elements of the queue from front to
rear. But if the size will be invalid then display the mentioned
message.
Sample Input 0
3
2
1
3
4

Sample Output 0
2
3
4

Sample Input 1
2

Sample Output 1
Invalid size

#include <bits/stdc++.h>
using namespace std;
int main()
{
int n;
cin>>n;
if(n<=2 || n>10)
{
cout<<"Invalid size";
return 0;
}
queue<int> q;
int count=0;
int data;
for(int i=0; i<10; i++)
{

if(i ==0)
{
cin>>data;
q.push(data);
count++;
}
cin>>data;
if(data<q.front())
{
continue;
}
else if(data>q.front())
{
q.push(data);
count++;
}
if(count==n)
{
break;
}

}
while(!q.empty())
{
cout<<q.front()<<endl;
q.pop();
}
return 0;
}
Day 25
49
A bracket is considered to be any one of the following characters: (,
), {, }, [, or ].
Two brackets are considered to be a matched pair if the an opening
bracket (i.e., (, [, or {) occurs to the left of a closing bracket
(i.e., ), ], or }) of the exact same type. There are three types of
matched pairs of brackets: [], {}, and ().
A matching pair of brackets is not balanced if the set of brackets it
encloses are not matched. For example, {[(])} is not balanced because
the contents in between { and } are not balanced. The pair of square
brackets encloses a single, unbalanced opening bracket, (, and the
pair of parentheses encloses a single, unbalanced closing square
bracket, ].
By this logic, we say a sequence of brackets is balanced if the
following conditions are met:

• It contains no unmatched brackets.


• The subset of brackets enclosed within the confines of a matched
pair of brackets is also a matched pair of brackets.
Given strings of brackets, determine whether each sequence of
brackets is balanced. If a string is balanced, return YES. Otherwise,
return NO.
Function Description
Complete the function isBalanced in the editor below.
isBalanced has the following parameter(s):

• string s: a string of brackets


Returns

• string: either YES or NO


Input Format
The first line contains a single integer , the number of strings.
Each of the next lines contains a single string , a sequence of
brackets.
Constraints

• , where is the length of the sequence.
• All chracters in the sequences ∈ { {, }, (, ), [, ] }.
Output Format
For each string, return YES or NO.
Sample Input
STDIN Function
----- --------
3 n = 3
{[()]} first s = '{[()]}'
{[(])} second s = '{[(])}'
{{[[(())]]}} third s ='{{[[(())]]}}'

Sample Output
YES
NO
YES

Explanation
22. The string {[()]} meets both criteria for being a balanced
string.
23. The string {[(])} is not balanced because the brackets
enclosed by the matched pair { and } are not balanced: [(]).
24. The string {{[[(())]]}} meets both criteria for being a
balanced string.
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include<stack>
using namespace std;
#include<map>
int main() {
map<char,char>lol;
lol[')']='(';
lol[']']='[';
lol['}']='{';
int t;
cin >> t;
while(t--)
{
string s;
stack<char>st;
cin >> s;
int flag=0;
for(int i=0;s[i]!='\0';i++)
{
if(s[i]=='(' || s[i]=='{' ||
s[i]=='[')
st.push(s[i]);
else{
if(!st.empty() and
lol[s[i]]==st.top())
st.pop();
else
{
flag=1;
break;
}
}
}
if(!flag and st.empty())
cout << "YES" << endl;
else
cout << "NO " << endl;
}
return 0;
}
Day 26
50
Chanden is using a compiler which is able to solve postfix expressions
only. Postfix expressions are those expressions in which operator
comes after the operands. Chanden is not able to understand how
compiler solves these postfix expresssion. Write a program using stack
which will work like compiler so that Chanden will be able to
understand the functionality of compiler.
Input Format
An array of strings which will contain operands and operators (+,-
,*,/).
Constraints
NA
Output Format
Evaluated result if expression is solvable else will print Invalid
Input.
Sample Input 0
2
3
+

Sample Output 0
5

Sample Input 1
2
3
+
-

Sample Output 1
Invalid Input

#include<bits/stdc++.h>
using namespace std;
int k=0;
float scanNum(char ch){
int value;
value = ch;
return float(value-'0');
}
int isOperator(char ch){
if(ch == '+'|| ch == '-'|| ch == '*'||
ch == '/' || ch == '^')
return 1;
return -1;
}
int isOperand(char ch){
if(ch >= '0' && ch <= '9')
return 1;
return -1;
}
float operation(int a, int b, char op){
//Perform operation
if(op == '+')
return b+a;
else if(op == '-')
return b-a;
else if(op == '*')
return b*a;
else if(op == '/')
return b/a;
else if(op == '^')
return pow(b,a);
else
return INT_MIN;
}
float postfixEval(char postfix[]){
int a, b;
stack<float> stk;

for(int it=0;it<k;it++){
if(isOperator(postfix[it]) != -1){
if(stk.empty())
{
return 100;
break;
}
a = stk.top();
stk.pop();
if(stk.empty())
{
return 100;
break;
}

b = stk.top();
stk.pop();

stk.push(operation(a, b,
postfix[it]));
}else if(isOperand(postfix[it]) >
0){
// if(stk.empty())
// {
// return 100;
// break;
// }
stk.push(scanNum(postfix[it]));
}
}

return stk.top();
}
int main(){
char post[50];
for(int i=0;i<50;i++)
{ k++;
cin>>post[i];
if(post[i]<0)
{
break;
}
}

float ans=postfixEval(post);
if(ans!=100)
{
cout<<ans;
}
else
{
cout<<"Invalid Input";
}
return 0;
}
51
In F1 race competition, a total of 5 cars participated and they have
to complete a total of 3 laps. During the end of each lap the first
car is pushed to the last and finally find the winner who comes first
at the end of the 3rd lap
Input Format
5
Mercedes RedBull Ferrari McLaren Alpine
Constraints
25. F1 car names should be in the queue of Strings

26. During the end of each and every lap the first car needs to
Popped outside and Pushed at the end
Output Format
RedBull Ferrari McLaren Alpine Mercedes
Ferrari McLaren Alpine Mercedes RedBull
McLaren Alpine Mercedes RedBull Ferrari
McLaren
Sample Input 0
5
Alpha Aston Williams Alfa Haas

Sample Output 0
Aston Williams Alfa Haas Alpha
Williams Alfa Haas Alpha Aston
Alfa Haas Alpha Aston Williams
Alfa

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

int main() {
int c;
cin>>c;
string arr[c];
for(int i=0;i<c;i++)
cin>>arr[i];

for(int j=0;j<3;j++){
string x=arr[0];
for(int k=0;k<c-1;k++){
arr[k]=arr[k+1];
}
arr[c-1]=x;
for(int i=0;i<c;i++)
cout<<arr[i]<<" ";
cout<<endl;
}
cout<<arr[0];
return 0;
}
Day 27
52
Ravi and Amit are playing a game where Ravi is going to give n strings
to Amit and Amit need to tell Yes or No for each string. Yes if string
is a Palindrome and No if it is not. Write a program using stack to
help the Amit to find string is Palindrome or not.
Input Format
First line will contain one integer representing value of n.
n lines contains string values.
Constraints
n>=1
Output Format
n lines contains Yes or No.
Sample Input 0
2
HELEH
YEEY

Sample Output 0
Yes
Yes

Sample Input 1
0

Sample Output 1
Invalid Input

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

// Function that returns true


// if string is a palindrome
bool isPalindrome(string s)
{
int length = s.size();

// Creating a Stack
stack<char> st;

// Finding the mid


int i, mid = length / 2;

for(i = 0; i < mid; i++) {


st.push(s[i]);
}

// Checking if the length of the


string
// is odd, if odd then neglect the
// middle character
if(length % 2 != 0) {
i++;
}

char ele;
// While not the end of the string
while (s[i] != '\0')
{
ele = st.top();
st.pop();

// If the characters differ then the


// given string is not a palindrome
if(ele != s[i])
return false;
i++;

return true;
}

// Driver code
int main()
{
int n;
cin>>n;
if(n<1){
cout<<"Invalid Input";
return 0;
}
string s;
for(int i=0;i<n;i++){
cin>>s;

if (isPalindrome(s)) {
cout << "Yes"<<endl;
}
else {
cout << "No"<<endl;
}
}
return 0;
}
53
Given an integer K and a queue of integers, we need to reverse the
order of the first K elements of the queue, leaving the other elements
in the same relative order.
Input Format
5
1 2 3 4 5
3
Constraints
27. Total number of integers should be 5

28. Complete the provided function modify Queue that takes queue
and k as parameters and returns a modified queue.
Output Format
3 2 1 4 5
Sample Input 0
5
5 4 3 2 1
4

Sample Output 0
2 3 4 5 1

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
int n;
cin>>n;
int arr[n];
for(int i=0;i<n;i++){
cin>>arr[i];
}

int k,j;
cin>>k;

for(j=k-1;j>=0;j--)
cout<<arr[j]<<" ";

for(int i=k;i<n;i++)
cout<<arr[i]<<" ";
return 0;
}
Day 28
54
In a data structure, the name entered by user as a set of characters
is stored in the "order of input" of characters. It has been observed
that the user has entered his mobile number before the name, separated
with a comma ",". Write a code to remove repeatedly, the characters in
the order of input, till "," is retrieved. also remove the comma ",".
Note that Input should be a string always lesser than 25.
Input Format
9876876545,Alice Smith
Constraints
Input should be a string always lesser than 25.
Output Format
Alice Smith
Sample Input 0
7687845231,Harry

Sample Output 0
Harry

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;

int main() {
string a;
getline(cin,a);
int l=a.length();
if(l>24 || l<1)
return 0;
int b=0;
for(int i=0;i<l;i++)
{
if(b==1)
cout<<a[i];
if(a[i]==',')
b=1;
}
return 0;
}
55
Garima visited toy shop to purchase colored balls, when shopkeeper
start displaying all the colored balls then she got confused that
which ball she should select or reject. All the colored balls are so
attractive hence she decided to purchase more than one color ball but
not all the balls. So let’s help Garima by creating one application
which execute as per her choice for example when she enter 1 then she
should be allowed to purchase any color ball, when she enter 2 then
only recent ball which is added that will be removed and display the
remaining colored balls and after that application will close. Note:-
Garima can enter her choice repeatedly.
Input Format
In First Line , enter choice as 1 In second line, Enter number of
elements which user wants to insert Third line will contain the values
which is to be inserted. Fourth line , enter choice as 2.
Constraints
User can enter only String type of value.
Output Format
Display the deleted color which is recently added and show the
remaining colors left and then application close.
Sample Input 0
1
2
red yellow
2

Sample Output 0
yellow
red

Explanation 0
Deleted color is yellow which is recently inserted and left with red
color only
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <stack>
using namespace std;

int main()
{
int t;
cin >> t;
stack<string> st;
while (t != 2)
{
int num;
cin >> num;
for (int i = 0; i < num; i++)
{
string s;
cin >> s;
st.push(s);
}
cin >> t;
if (t == 2)
{

cout<<st.top()<<endl;
st.pop();
}
}
while(!st.empty()){
cout<<st.top()<<" ";
st.pop();
}

return 0;
}
Day 29
56
Mr. Rohan packed his books in a box having capacity to put 6 books.
Now he wants to arrange the books in an another box. He is picking one
book at at time and then putting it in another box. Write a program
that will display the books he popped out and inserte in another
box(reverse order). Create push and pop functions to implement it. If
the first box is empty then display message of "Empty Box". If the
number of box is not capable of storing the books then display a
message of "Full Box".
Input Format
first line contains the number of books, i.e. n next n lines contains
the name of books in the stack of box.
Constraints
0<n≤6
Output Format
Output contains the list of books popped out from the box and inserted
to the other books.Two book names should be separeted by -> sign.
Sample Input 0
3
Let_us_C
Oracle
DBMS

Sample Output 0
DBMS->Oracle->Let_us_C

Explanation 0
first line is number of books i.e. 3
The order of books pushed in the first Box fom top is 1Let_us_C
2.Oracle 3. DBMS.
So if we pop out the books then the books are popped out in the
reverse order(LIFO).
So the output will be DBMS->Oracle->Let_us_C.
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

int main() {
int n;
cin>>n;
if(n<1){
cout<<"Empty Box";
return 0;
}
if(n>6){
cout<<"Full Box";
return 0;
}
string arr[n];
for(int i=0;i<n;i++)
cin>>arr[i];
int x=0;
for(int i=n-1;i>=0;i--){
if(x==1)
cout<<"->";
cout<<arr[i];
x=1;
}
return 0;
}
57
A bracket is considered to be any one of the following characters: (,
), {, }, [, or ].
Two brackets are considered to be a matched pair if the an opening
bracket (i.e., (, [, or {) occurs to the left of a closing bracket
(i.e., ), ], or }) of the exact same type. There are three types of
matched pairs of brackets: [], {}, and ().
A matching pair of brackets is not balanced if the set of brackets it
encloses are not matched. For example, {[(])} is not balanced because
the contents in between { and } are not balanced. The pair of square
brackets encloses a single, unbalanced opening bracket, (, and the
pair of parentheses encloses a single, unbalanced closing square
bracket, ].
By this logic, we say a sequence of brackets is balanced if the
following conditions are met:

• It contains no unmatched brackets.


• The subset of brackets enclosed within the confines of a matched
pair of brackets is also a matched pair of brackets.
Given strings of brackets, determine whether each sequence of
brackets is balanced. If a string is balanced, return YES. Otherwise,
return NO.
Function Description
Complete the function isBalanced in the editor below.
isBalanced has the following parameter(s):

• string s: a string of brackets


Returns

• string: either YES or NO


Input Format
The first line contains a single integer , the number of strings.
Each of the next lines contains a single string , a sequence of
brackets.
Constraints

• , where is the length of the sequence.
• All chracters in the sequences ∈ { {, }, (, ), [, ] }.
Output Format
For each string, return YES or NO.
Sample Input
STDIN Function
----- --------
3 n = 3
{[()]} first s = '{[()]}'
{[(])} second s = '{[(])}'
{{[[(())]]}} third s ='{{[[(())]]}}'

Sample Output
YES
NO
YES

Explanation
29. The string {[()]} meets both criteria for being a balanced
string.
30. The string {[(])} is not balanced because the brackets
enclosed by the matched pair { and } are not balanced: [(]).
31. The string {{[[(())]]}} meets both criteria for being a
balanced string.
#include<bits/stdc++.h>
using namespace std;
unordered_map<char,int>symbol={ {'[',-
1},{'(',-2},{'{',-
3},{']',1},{')',2},{'}',3}};
void solve()
{
string s;
cin >> s;
stack<char>st;
for(auto bracket : s)
{
if(symbol[bracket]<0)
{
st.push(bracket);
}
else
{
if(st.empty())
{
cout <<
"NO\n";
return;
}
char top=st.top();
st.pop();
if(symbol[top]+symbol[bracket]!=0)
{
cout <<
"NO\n";
return;
}
}
}
if(st.empty())
{
cout << "YES\n";
return;
}
cout << "NO\n";
return;

int main()
{
int t;
cin >> t;
while(t--)
{
solve();
}
}
Day 30
58
WAP to insert N Elements into the stack. Find the Middle element ,
then delete one element from the stack and again find the new middle
element

Example

let 6 elements are there in stack


5
8
6
4
7
middle element is 6
deleted element is 7
new middle element is 8
Input Format
First input : N ,the number of elements in the stack
Second input : accect N elements from the user into the stack
Constraints
N must be nonzero and >=3
second input must be positive intergers
Output Format
Dispaly Middle Element
Display The New Midlle Element After Deleting One element From The
Stack.
Sample Input 0
5
2 6 8 7 4

Sample Output 0
8
6

Sample Input 1
3
5 8 6

Sample Output 1
8
5

#include <bits/stdc++.h>
using namespace std;
void mid(stack<int>st)
{
int length=0;
stack<int>newStack;
while (!st.empty())
{
length++;
newStack.push(st.top());
st.pop();
}
if(length%2!=0)
length=length/2;
else
length=(length/2)-1;

while (!newStack.empty())
{
if(length==0){
cout<<newStack.top()<<endl;
}
length--;
st.push(newStack.top());
newStack.pop();
}

}
int main()
{

int n;
cin >> n;
if(n>=3)
{
stack<int>st;
for (int i = 0; i < n; i++)
{
int num;
cin >> num;
st.push(num);
}
mid(st);
st.pop();
mid(st);
}

return 0;
}
59
Krishna has shown one magic to reverse the given string. He asked his
friend that can you write a code to check the given string is
palindrome or not.
Sample 1: Line 1: Enter the string : amma Line 2: Palindrome
Sample 1: Line 1 : Enter the expression : papa Line 2 : Not Palindrome
Input Format
User should pass the input in formate of string
Constraints
String expression length should > 0
Output Format
The result will display as Palindrome or Not Palindrome.
Sample Input 0
malayalam
Sample Output 0
Palindrome

Explanation 0
Line 1 : Enter the expression : malayalam Line 2 : Palindrome
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

int main() {
string str;
cin>>str;
string rev;
for(int i = str.size()-1; i>=0; i--)
{
rev.push_back(str[i]);
}
if(str == rev)
{
cout<<"Palindrome";
}
else{
cout<<"Not Palindrome";
}
return 0;
}
Day 31
60
Radha asked to her friend can you verify that given expression have
balanced parenthesis or not, So her decided to write a code to examine
that the given an string expression whether have the pairs of
parenthesis are balanced or not. The orders of “(“, “)”, “[“, “]”
,“{“, “}”,are correct in the given expression.

Sample 1: Line 1: Enter the expression : {}{[()]} Line 2: Balanced


Sample 2: Line 1 : Enter the expression : [(]) Line 2 : Not Balanced
Input Format
User enter the paris of parentheses without any operand.
Constraints
String expression length should > 0
Output Format
The result will display as parenthesis is balanced or not.
Sample Input 0
(){}[][{()()}]{()}

Sample Output 0
Balanced

Explanation 0
1: Enter the expression : (){}[][{()()}]{()} Balanced
32. Enter the expression : ({}[]}] Not Balanced
#include <cmath>
#include <cstdio>
#include <stack>
#include <iostream>
#include <algorithm>
#include <bits/stdc++.h>
using namespace std;
unordered_map<char,int>symbol={{'[',-
1},{'{',-2},{'(',-
3},{']',1},{'}',2},{')',3}};
int isbalance(string s)
{
stack<char>S;
for( char c:s)
{
if(symbol[c]<0)
{
S.push(c);
}
else
{
if(S.empty())
{
return 0;
}
char top=S.top();
S.pop();
if(symbol[c]+symbol[top]!=0)
{
return 0;
}
}
}
if(S.empty())
{
return 1;
}
else
{
return 0;
}
}
int main()
{
string s;
getline(cin,s);
if(s.size()>0)
{
if(isbalance(s)==1)
{
cout<<"Balanced"<<endl;
}
else
{
cout<<"Not Balanced"<<endl;
}
}
}
61
Implement a Queue as mentioned:
An opertaion Z is of 2 Types.
33. 1 (opertion of this type means: delete the element from the
queue and insert that element again in the queue)
34. 0 (opertion of this type means you need to delete in queue)
Input Format

• First line tell the nos. of element in the queue


• Second line tell the nos. of operation
• third line depicts the elements
• fourth line depicts the operation
Constraints

• Initial nos. of element is always more than 0 & less than 50.
• nos. of operation is always more than 0 & less than 10.
Output Format
print the element of queue after operation(space sperated)
Sample Input 0
8
4
2 1 4 8 9 7 9 3
1 0 1 0

Sample Output 0
9 7 9 3 2 4

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
class node1
{ public:
int data;
node1* next;
};
class queue1
{
public:
node1 *head,*tail;
void push(int d);
void pop(int n);
void trav();
queue1();
};
queue1::queue1()
{
head=tail=NULL;
}

void queue1::push(int d)
{
node1* temp=new node1();
temp->data=d;
temp->next=NULL;
if(tail==NULL)
{
head=tail=temp;
}
else
{
tail->next=temp;
tail=temp;
}
}
void queue1::pop(int n)
{ int x;
node1* temp=head;
x=temp->data;
head=head->next;
if(n==1)
push(x);
delete temp;

}
void queue1::trav()
{
while(head!=NULL)
{
cout<<head->data<<" ";
head=head->next;
}

int main() {
int s,d,n,v[10];
queue1 a;
cin>>s;
cin>>n;
for(int i=0;i<s;i++)
{
cin>>d;
a.push(d);

}
for(int i=0;i<n;i++)
{
cin>>v[i];
}
for(int i=0;i<n;i++)
{
a.pop(v[i]);
}
a.trav();
return 0;
}

Day 32
62
Given an integer K and a queue of integers, we need to reverse the
order of the first K elements of the queue, leaving the other elements
in the same relative order.
Input Format
5
1 2 3 4 5
3
Constraints
35. Total number of integers should be 5

36. Complete the provided function modify Queue that takes queue
and k as parameters and returns a modified queue.
Output Format
3 2 1 4 5
Sample Input 0
5
5 4 3 2 1
4

Sample Output 0
2 3 4 5 1

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <queue>
#include <stack>
using namespace std;
/* Enter your code here. Read input
from STDIN. Print output to STDOUT */

int main()
{
int n;
cin>>n;
if(n==5)
{
queue<int>que;
for(int i=0; i<n; i++)
{
int a;
cin>>a;
que.push(a);
}
int key;
cin>>key;
queue<int>que1;
stack<int>st;

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


{
if(i<key)
{
st.push(que.front());
que.pop();
}
else
{
que1.push(que.front());
que.pop();
}
}
for(int i=0; i<n; i++)
{
if(i<key)
{
que.push(st.top());
st.pop();
}
else
{
que.push(que1.front());
que1.pop();
}
}

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


{
cout<<que.front()<<" ";
que.pop();
}
}

return 0;
}
63
Ravi and Amit are playing a game where Ravi is going to give n strings
to Amit and Amit need to tell Yes or No for each string. Yes if string
is a Palindrome and No if it is not. Write a program using stack to
help the Amit to find string is Palindrome or not.
Input Format
First line will contain one integer representing value of n.
n lines contains string values.
Constraints
n>=1
Output Format
n lines contains Yes or No.
Sample Input 0
2
HELEH
YEEY

Sample Output 0
Yes
Yes

Sample Input 1
0

Sample Output 1
Invalid Input

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <stack>
using namespace std;

void check(string str)


{
stack<char>s;
int l=str.length();
int count=0;
for(int i=0; i<l; i++)
{
s.push(str[i]);
}
for(int i=0; i<l; i++)
{
if(str[i]==s.top())
{
count++;
s.pop();
}
}
if(count==l)
cout<<"Yes\n";
else
cout<<"No\n";
}

int main() {
int n;
cin>>n;
if(n>=1)
{
string str[n];
for(int i=0; i<n; i++)
cin>>str[i];
for(int i=0; i<n; i++)
check(str[i]);
}
else
{
cout<<"Invalid Input";
}
return 0;
}
Day 33
64
Garima visited toy shop to purchase colored balls, when shopkeeper
start displaying all the colored balls then she got confused that
which ball she should select or reject. All the colored balls are so
attractive hence she decided to purchase more than one color ball but
not all the balls. So let’s help Garima by creating one application
which execute as per her choice for example when she enter 1 then she
should be allowed to purchase any color ball, when she enter 2 then
only recent ball which is added that will be removed and display the
remaining colored balls and after that application will close. Note:-
Garima can enter her choice repeatedly.
Input Format
In First Line , enter choice as 1 In second line, Enter number of
elements which user wants to insert Third line will contain the values
which is to be inserted. Fourth line , enter choice as 2.
Constraints
User can enter only String type of value.
Output Format
Display the deleted color which is recently added and show the
remaining colors left and then application close.
Sample Input 0
1
2
red yellow
2

Sample Output 0
yellow
red

Explanation 0
Deleted color is yellow which is recently inserted and left with red
color only
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
/* Enter your code here. Read input
from STDIN. Print output to STDOUT */
int main()
{

int n = 1;
string arr[99];
int top = -1;
while (n != 2)
{
cin >> n;
if (n == 2)
{
break;
}
int nums;
cin >> nums;
for (int i = 0; i < nums; i++)
{
top = top + 1;
string w;
cin >> w;
arr[top] = w;
}
}
cout << arr[top] << "\n";
top = top - 1;
while (top != -1)
{
cout << arr[top] << " ";
top = top - 1;
}
return 0;
}
65
In a data structure, the name entered by user as a set of characters
is stored in the "order of input" of characters. It has been observed
that the user has entered his mobile number before the name, separated
with a comma ",". Write a code to remove repeatedly, the characters in
the order of input, till "," is retrieved. also remove the comma ",".
Note that Input should be a string always lesser than 25.
Input Format
9876876545,Alice Smith
Constraints
Input should be a string always lesser than 25.
Output Format
Alice Smith
Sample Input 0
7687845231,Harry

Sample Output 0
Harry

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
/* Enter your code here. Read input from
STDIN. Print output to STDOUT */
int main()
{
string s;
getline(cin, s);
int l = s.size();
if (l < 25)
{
for (int i = 0; i < l; i++)
{
if (s[i] >= '0' && s[i] <= '9'
|| s[i] == ',')
{
}
else
{
cout << s[i];
}
}
}
return 0;
}
Day 34
66
Mr. Rohan packed his books in a box having capacity to put 6 books.
Now he wants to arrange the books in an another box. He is picking one
book at at time and then putting it in another box. Write a program
that will display the books he popped out and inserte in another
box(reverse order). Create push and pop functions to implement it. If
the first box is empty then display message of "Empty Box". If the
number of box is not capable of storing the books then display a
message of "Full Box".
Input Format
first line contains the number of books, i.e. n next n lines contains
the name of books in the stack of box.
Constraints
0<n≤6
Output Format
Output contains the list of books popped out from the box and inserted
to the other books.Two book names should be separeted by -> sign.
Sample Input 0
3
Let_us_C
Oracle
DBMS

Sample Output 0
DBMS->Oracle->Let_us_C

Explanation 0
first line is number of books i.e. 3
The order of books pushed in the first Box fom top is 1Let_us_C
2.Oracle 3. DBMS.
So if we pop out the books then the books are popped out in the
reverse order(LIFO).
So the output will be DBMS->Oracle->Let_us_C.

Submissions:
205
Max Score:

5
Difficulty:
Medium
Rate This Challenge:

More

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

int main() {
int n;
cin>>n;
if(n<1){
cout<<"Empty Box";
return 0;
}
if(n>6){
cout<<"Full Box";
return 0;
}
string arr[n];
for(int i=0;i<n;i++)
cin>>arr[i];
int x=0;
for(int i=n-1;i>=0;i--){
if(x==1)
cout<<"->";
cout<<arr[i];
x=1;
}
return 0;
}
67
Assume 9 spectators are standing in a ticket counter to take a ticket to watch a T20 match.In a
queue enter n names of spectators and then some gifts will be given to odd ticket number
spectators who came to watch match.Note index is ticket number and values in queues are
name of spectators.Create a user interactive program where 1 will be for insertion and 2 will
display names of odd ticket spectators who will get some gifts.
Input Format
First Input will represent the operation
Second Input will represent how many names to be inserted in case operation is insertion .
Third Input will be n names in a ticket counters.
Fourth Input is to call display operation
Constraints
If the size of the queue will be full, display “No other spectator is allowed”.

Output Format
Display names of spectator shortlisted for gifts.
Sample Input 0
1
3
Aryan
Piyush
Rahul
2

Sample Output 0
Piyush

#include <iostream>
#include<queue>
using namespace std;

int main()
{
int t1;
cin>>t1;
if(t1==2)
{
return 0;
}
int n;
cin>>n;
queue<string>s1;
for(int i=0;i<n;i++)
{
string val;
cin>>val;
s1.push(val);
}
if(n>9){
cout<<"No other spectator is allowed";
return 0;
}
int a;
cin>>a;
int i=0;
while(!s1.empty())
{
if(i==0){
s1.pop();
i++;
continue;
}
if(i%2!=0){
cout<<s1.front()<<" ";
s1.pop();
i++;
continue;
}
i++;
s1.pop();
}
return 0;
}
Day 35
68
In this challenge, you must first implement a queue using two stacks. Then process queries,
where each query is one of the following 3 types:
1 x: Enqueue element x into the end of the queue. 2: Dequeue the element at the front of the
queue. 3: Print the element at the front of the queue.
Input Format
The first line contains a single integer, q, denoting the number of queries. Each line i of the q
subsequent lines contains a single query in the form described in the problem statement above.
All three queries start with an integer denoting the query type, but only query 1 is followed by
an additional space-separated value,x, denoting the value to be enqueued.
Output Format
For each query of type 3 , print the value of the element at the front of the queue on a new
line.
#include<bits/stdc++.h>

#include <queue>

using namespace std;

class MyQueue {

public:

stack<int> stack_newest_on_top, stack_oldest_on_top;

void push(int x) {

stack_newest_on_top.push(x);
}

void pop() {

if (stack_oldest_on_top.empty()) {

while (!stack_newest_on_top.empty()) {

stack_oldest_on_top.push(stack_newest_on_top.top());

stack_newest_on_top.pop();

stack_oldest_on_top.pop();

int front() {

if (stack_oldest_on_top.empty()) {
while (!stack_newest_on_top.empty()) {

stack_oldest_on_top.push(stack_newest_on_top.top());

stack_newest_on_top.pop();

return stack_oldest_on_top.top();

};

int main() {

MyQueue q1;

int q, type, x;

cin >> q;

for(int i = 0; i < q; i++) {


cin >> type;

if(type == 1) {

cin >> x;

q1.push(x);

else if(type == 2) {

q1.pop();

else cout << q1.front() << endl;

return 0;

69
Construct a stack using two queues (q1, q2), you need to simulate the stack operations by using
queue operations. You are required to complete the three methods push() which takes an
integer 'x' as input denoting the element to be pushed into the stack, pop() which poped out
from the stack and display() which display the resultant stack(-1 if the stack is empty).
Input Format
Enter the sequence of stack operation, 1 for push(), 2 for pop() and 3 for display().
Constraints

Maximum size of stack in 20


Output Format
Display result according to sequence of operations
Sample Input 0
1
2
1
3
2
1
4
2
1
5
3

Sample Output 0
2 5

Sample Input 1
1
2
2
3

Sample Output 1
-1

#include <iostream>

using namespace std;


int main() {

int arr[9999];

int top = -1;

for(int i = 0; i < 999; i ++){

int n = 4;

cin >> n;

if(n == 4){

break;

if(n == 1){

top = top + 1;

int newele;

cin >> newele;

arr[top] = newele;

else if(n == 2){

top = top - 1;

else if(n == 3){

if(top == -1){

cout << "-1";

else{
for(int i = 0; i <= top; i++){

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

return 0;

You might also like