11 June
11 June
There is a queue of n people in front of the local electronics store. All of them have their phones
at various battery percentages and are looking to buy a portable charger quickly before their
battery runs out. The server at the counter understands this and he doesn’t want any of his
customers’ phones to run out of charge. He, being an algorithmist, quickly determines that he
wants to implement bubble sort technique to sort the customers in the queue, such that, the
person whose phone is going to die the quickest, comes to the front of the line and gets a portable
charger.
The server at the counter is an algorithmist and not a programmer, hence he asks you to do the
job for him.
INPUT
The first line of input is n (1≤n≤100), the number of customers in the queue
The second line of input is the battery percentages of the n customers (positive numbers less than
100) each separated by a space.
OUTPUT
Print the battery percentages of the customers in a line after the sorting is completed.
Sample Input 0
8
3 4 5 2 4 10 18 1
Sample Output 0
1 2 3 4 4 5 10 18
Code:-
#include <stdio.h>
int main()
{
int array[100], n, c, d, swap;
scanf("%d", &n);
scanf("%d", &array[c]);
swap = array[d];
array[d] = array[d+1];
array[d+1] = swap;
return 0;
Formally, you are given an array of size n where the first n - 1 elements are sorted in ascending
order and the last element can be any value. Sort this array by sliding the last element into its
correct position.
INPUT
The first line of input is n (1≤n≤100), the number of customers in the queue. The second line of
input is the battery percentages of the n customers (positive numbers less than 100)
the first n - 1 of which are standing in ascending order according to their battery percentage.
OUTPUT
Print the battery percentages of the customers in a line after the new person has been put in his
right place in the queue.
Sample Input 0
6
135794
Sample Output 0
134579
Code:-
#include <stdio.h>
int main()
scanf("%d", &n);
scanf("%d", &array[c]);
swap = array[d];
array[d] = array[d+1];
array[d+1] = swap;
return 0;
Tisha went to her orchard and plucked a bunch of oranges. Now she wants to keep aside some of
the oranges to for herself and her children but also sell the biggest ones on the market. She takes
a look at the last orange in her hand and decides that she would like to eat it. She seems to think
it fair that all the oranges smaller than the orange in her hand can go to her children. All the
oranges bigger than this one will be sold on the market. Write a program to help Tisha do this
partition. Assume the oranges are neatly stacked in a row.
k=0
Check orange number i = 0,1,2,3.. n - 1 :
for ith orange :
if (orange[i] is smaller than or equal to orange[n-1]) :
swap orange[i] with orange[k]
k++
swap orange[k] with orange[n-1]
Simulate the process used by Tisha and print the resultant array of the size of oranges.
INPUT
The first line of input is n (1≤n≤100), the number of oranges Tisha plucked from the orchard
The second line of input are the diameters of the oranges you just took (positive numbers) each
separated by a space. Assume the last orange (orange[n - 1]) to be the one Tisha took in her
hand.
OUTPUT
Print the sizes of the oranges in a row after the partition has been done
Sample Input 0
8
43861195
Sample Output 0
43115698
Code:-
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int n,i,t,j=0;
scanf("%d",&n);
int a[n];
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
int p=a[n-1];
for(i=0;i<n-1;i++)
if(a[i]<=a[n-1])
t=a[i];
a[i]=a[j];
a[j]=t;
j++;
t=a[j];
a[j]=a[n-1];
a[n-1]=t;
for(i=0;i<n;i++)
printf("%d ",a[i]);
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
return 0;
Input Format
Constraints
Output Format
Output N lines. Each line must contain one date. The dates must appear in a sorted format.
Sample Input 0
4
9 8 1996
31 4 1995
30 4 1996
25 12 1997
Sample Output 0
31 4 1995
30 4 1996
9 8 1996
25 12 1997
Code:-
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
struct date
{
int day;
int month;
int year;
};
int main()
int N,i,j;
scanf("%d",&N);
for(i=0;i<N;i++)
scanf("%d%d%d",&input[i].day,&input[i].month,&input[i].year);
for(i=0;i<N;i++)
for(j=i+1;j<N;j++)
if(input[i].year>input[j].year)
input[i] = input[j];
input[j] = temp;
{
struct date temp = input[i];
input[i] = input[j];
input[j] = temp;
input[i] = input[j];
input[j] = temp;
printf("%d %d %d\n",input[i].day,input[i].month,input[i].year);
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
return 0;
#define RESET 0
int sudokuSolver();
int findEmptyCell();
int isValid();
void printGrid();
void inputGrid();
int row,col;
//this variable was used just to keep track of number of recursive calls
long int totalNumOfCalls=0;
void main(){
int i,j,solution=0;
char ch;
clrscr();
printf("You can change the puzzle before running the program \nby changing the values in the
\"grid\" array\n\n");
printf("The Entered Sudoku puzzle is: \n");
printGrid();
printf("Press 'c' to confirm and solve, or 'e' to exit: ");
ch=getch();
if(ch=='e')
exit(0);
else if(ch=='c'){
clrscr();
solution=sudokuSolver();
if(solution){
printf("\nThe Solved Sudoku is: \n\n");
printGrid();
}
else
printf("\nNo Possible Solution!!\n\n");
getch();
}
int findEmptyCell(){
int i,j;
for(i=row;i<=8;i++)
for(j=0;j<=8;j++){
if(grid[i][j]==0)
{
row=i;col=j;
return 1;
}
}
return 0;
}
// to check the presence of number in 3X3 box
for(i=rowStart;i<=rowStart+2;i++)
for(j=colStart;j<=colStart+2;j++)
if(grid[i][j]==num)return 0;
return 1;
}
int sudokuSolver(){
int digit;
int prevRow,prevCol; // for backtracking
totalNumOfCalls++;
if(!findEmptyCell())
return 1;
for(digit=1;digit<=9;digit++){
if(isValid(row,col,digit)){
grid[row][col]=digit;
prevRow=row;prevCol=col;
if(sudokuSolver())
return 1;
//while backtracking assigning previous values to row and col
row=prevRow;col=prevCol;
grid[row][col]=RESET;
}
return 0;
}
void printGrid(){
int i,j;
printf("\t-------------------------\n");
for(i=0;i<9;i++){
printf("\t");
for(j=0;j<9;j++){
if(j==0)
printf("| ");
if(grid[i][j]==0)
printf(". ");
else
printf("%d ",grid[i][j]);
if((j+1)%3==0 )
printf("| ");
if((i+1)%3==0 )
printf("\n\t-------------------------");
printf("\n");
}
}
Sample Input #1:
In a team queue, each element is a part of a team. If a new element wants to enter the queue, it
first scans the queue to find his teammates and joins in the queue behind them. If the element has
no teammates already waiting in the queue then he joins at the end of the queue (Bad Luck
Element). Dequeing operation in a team queue is the same as that in a normal queue.
Input Format
The input file will contain one or more test cases. Each test case begins with the number of teams
t (1 ≤ t ≤ 1000).
Then t team descriptions follow, each one consisting of the number of elements belonging to the
team and the elements themselves. Elements are integers in the range 0..999999. A team may
consist of up to 1000 elements.
Warning:
A test case may contain up to 200000 (two hundred thousand) commands, so the implementation
of the team queue should be efficient: both enqueing and dequeuing of an element should only
take constant time.
Output Format
For each test case, first print a line saying ‘Scenario #k’, where k is the number of the test case.
Then, for each ‘DEQUEUE’ command, print the element which is dequeued on a single line.
Print a blank line after each test case, even after the last one
Sample Input 0
2
3 101 102 103
3 201 202 203
ENQUEUE 101
ENQUEUE 201
ENQUEUE 102
ENQUEUE 202
ENQUEUE 103
ENQUEUE 203
DEQUEUE
DEQUEUE
DEQUEUE
DEQUEUE
DEQUEUE
DEQUEUE
STOP
0
Sample Output 0
Scenario #1
101
102
103
201
202
203
C++ Code:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
int id[maxn];
bool f[1002];
int main() {
int t, cas = 1, n, x;
char s[20];
while (scanf("%d",&t)!=EOF&&t!=0)
queue<int> team;
scanf("%d", &n);
f[i] = false;
scanf("%d", &x);
id[x] = i;
while (scanf("%s",s)!=EOF&&strcmp(s,"STOP")!=0)
if (!strcmp(s, "ENQUEUE"))
scanf("%d", &x);
if (!f[id[x]])
team.push(id[x]);
f[id[x]] = true;
q[id[x]].push(x);
else
{
printf("%d\n", q[top].front());
q[top].pop();
if (q[top].empty())
team.pop();
f[top] = false;
printf("\n");
return 0;