DSA - Unit 4 - Lecture 24 Coding
DSA - Unit 4 - Lecture 24 Coding
admin.lpucolab438.examly.io/course
No. of Sections: 1
No. of Questions: 12
Total Duration: 90 min
Section 1 - Coding
Section Summary
No. of Questions: 12
Duration: 90 min
Additional Instructions:
None
Q1.
Problem Statement
Imagine you are a software developer working for a logistics company. Your task is to
create a program that can efficiently sort a list of packages based on their weights using
the Quick-Sort algorithm. Each package has a weight value associated with it.
The program should take an array of package objects as input, where each object
contains the weight of a package. The program should then sort the array in ascending
order based on the weights of the packages.
Input Format
The first line of input consists of an integer N, representing the number of packages.
The second line consists of N integers, representing the weight of the packages,
separated by space.
Output Format
The output displays N integers representing the weights of the packages after they have
been sorted in ascending order.
Constraints
N>0
1/56
weight > 0
4
15 25 35 10
10 15 25 35
3
120 60 80
60 80 120
Imagine you are a computer programmer tasked with creating a program to organize a
collection of years in ascending order. This program will take a list of years as input and
efficiently sort them using the Quick-Sort algorithm, ensuring that the years are arranged
chronologically.
Input Format
The first line of input consists of an integer N, representing the number of years.
Output Format
The output prints the sorted dates in chronological order.
Constraints
N>0
5
2014 2009 2000 1997 1865
3
1496 3015 2056
2/56
Time Limit: - ms Memory Limit: - kb Code Size: - kb
Q3.
Problem Statement
Imagine you are a teacher preparing the final grades for your class. You have a list of
your students' GPAs, and you want to see who performed the best in the class. You
decide to use this program to quickly sort and display the GPAs in descending order to
identify the top students based on their GPAs.
Your task is to implement a recursive function using the Quick-Sort algorithm. The
program should prompt the user for the number of students, input the GPA, and sort the
same in descending order.
Input Format
The first line of input consists of an integer N, representing the number of students.
The second line consists of N floating point numbers, representing the GPA of the
students.
Output Format
The output prints the sorted GPAs in descending order, from the highest GPA to the
lowest, rounded off to one decimal place.
Constraints
N>0
3
3.6 4.4 2.9
6
1.2 4.9 3.5 2.7 5.0 3.1
3/56
You are working on a project to implement a feature that displays the list of users in
alphabetical order based on their names.
To achieve this, you decide to use the Quick-Sort algorithm to efficiently sort the names.
The user inputs the number of users and then the name of each user. Once the names
are collected, the program must apply the Quick Sort algorithm to sort and display the
names in alphabetical order.
Input Format
The first line of input consists of an integer N, representing the number of users.
The following N lines consist of the names of the users (starting with uppercase letters).
Output Format
The output prints the sorted list of names in alphabetical order.
Constraints
N>0
5
Alice
Denver
Aadhil
Charlie
Zen
Aadhil
Alice
Charlie
Denver
Zen
6
Chandler
Monica
Ross
Joey
Rachel
Phoebe
4/56
Chandler
Joey
Monica
Phoebe
Rachel
Ross
You are working as a software engineer at a technology company, and your team is
developing a utility tool for sorting binary numbers efficiently. The utility is designed to sort
an array of integers based on the number of 1s in their binary representation. In case of
two or more integers having the same number of 1s, you need to sort them in ascending
order.
Write a program that takes an array of integers as input and uses the Quick-Sort
algorithm to sort the integers in ascending order based on the number of 1s in their binary
representation.
Example 1
Input:
arr = [0,1,2,3,4,5,6,7,8]
Output:
[0,1,2,4,8,3,5,6,7]
Explanation:
5/56
[7] has 3 bits.
Example 2
Input:
arr = [1024,512,256,128,64,32,16,8,4,2,1]
Output:
[1,2,4,8,16,32,64,128,256,512,1024]
Explanation:
All integers have 1 bit in the binary representation, you should just sort them in ascending
order.
Input Format
The first line of input consists of an integer N, representing the number of elements.
Output Format
The output prints the sorted integers in ascending order based on the number of 1s in
their binary representation.
Constraints
N>0
9
0 1 2 3 4 5 6 7 8
0 1 2 4 8 3 5 6 7
11
1024 512 256 128 64 32 16 8 4 2 1
6/56
Time Limit: - ms Memory Limit: - kb Code Size: - kb
Q6.
Problem Statement
Your mission is to write a program that can unscramble the message by sorting the
characters in ascending order using the Quick-Sort algorithm.
Input Format
The first line of input consists of an integer N, representing the number of characters.
Output Format
The output prints the sorted characters in ascending order.
Constraints
N>0
5
b n h u j
b h j n u
6
w e r h j k
e h j k r w
7/56
Write a program that takes an array of words as input and uses the QuickSort algorithm to
sort the words in reverse lexicographical order.
Input Format
The first line of input consists of an integer N, representing the number of strings.
Output Format
The output displays the words sorted in reverse lexicographical order.
Constraints
N>0
2
Cap Cat
Cat Cap
5
Chair Door Mouse Keyboard Table
You are given an array of integers containing both negative and positive numbers. Your
task is to rearrange the elements in such a way that all negative numbers come before
the positive numbers. After rearranging, you need to sort the numbers in ascending order
using the Quick-Sort algorithm.
8/56
2. quickSort(int arr[], int low, int high): This is the standard QuickSort function that
takes an array of integers arr, the starting index low, and the ending index high. It
sorts the numbers in ascending order.
Input Format
The first line of input consists of an integer N, representing the number of elements in the
array.
Output Format
The output prints the sorted array, such that the negative elements come before the
positive elements, separated by space.
Constraints
N>0
5
6 5 -7 4 -1
-7 -1 4 5 6
8
1 4 5 9 -9 6 3 2
-9 1 2 3 4 5 6 9
As a diligent teacher, you have conducted a challenging exam for your students. Now that
the papers have been graded, you are eager to find out which students performed the
best and identify those who might need extra support.
To efficiently rank your students based on their marks, you decide to create a computer
program that sorts their exam marks in descending order using the Quick-Sort algorithm.
Input Format
The first line of input consists of an integer N, representing the number of students.
The second line consists of N floating-point numbers, representing the marks of the
students.
9/56
Output Format
The output prints the marks in descending order, placing the highest mark first and the
lowest mark last.
Constraints
N>0
5
78.3 54.7 96.4 32.7 53.1
4
50.6 65.1 84.3 36.4
You are working as a program developer at a renowned sports academy. As part of the
academy's performance evaluation system, you are tasked with sorting the athletes
based on their heights in descending order.
Write a program that takes an array of athlete names and their corresponding heights as
input. Your program should use the Quick-Sort algorithm to sort the athletes' names in
descending order based on their heights.
Input Format
The first line of input consists of an integer N, representing the number of athletes.
The following N lines consist of the athlete's name and height, separated by space.
Output Format
The output prints the athletes' names sorted in descending order based on their heights.
Constraints
N == names.length == heights.length
N>0
10/56
names[i] consists of lower and upper case English letters.
6
Mary 180
John 165
Emma 170
Joey 157
Tom 169
Cruise 175
3
Alice 155
Bob 185
Bob 150
You are given a sequence of characters, and your task is to sort them in descending
order using the Quick-Sort algorithm.
Write a program that takes an integer N as input, representing the number of characters
in the sequence. Then, read N characters as input. Implement the quick sort algorithm to
sort the characters and print the sorted sequence.
Input Format
The first line of input consists of an integer N, representing the number of characters.
Output Format
The output prints the sorted characters in descending order.
Constraints
N>0
11/56
5
q w e r t
w t r q e
5
m a n g o
o n m g a
You are working as a programmer at a sports academy, and the academy holds various
sports competitions regularly. As part of the academy's system, you need to sort the
scores of the participants in descending order using the Quick-Sort algorithm.
Write a program that takes the scores of N participants as input and uses the
QuickSortalgorithm to sort and display the scores in descending order.
Input Format
The first line of input consists of an integer N, representing the number of participants.
The second line consists of N space-separated integers, representing the scores of the
participants.
Output Format
The output prints the scores sorted in descending order.
Constraints
N>0
5
78 54 96 32 53
96 78 54 53 32
4
53 65 84 36
12/56
84 65 53 36
Section 1 - Coding
Q1 Test Case Input Output
7
245 500 897 612 123 321 100
50
611 452 305 267 559 633 181 23 557 221 394 570 587 112 657 376 638 274 444 238 552
327 467 483 20 251 206 24 501 34 414 435 408 143 566 71 551 189 292 478 406 530 12
297 266 131 239 674 10 298
10 12 20 23 24 34 71 112 131 143 181 189 206 221 238 239 251 266 267 274 292 297
298 305 327 376 394 406 408 414 435 444 452 467 478 483 501 530 551 552 557 559
566 570 587 611 633 638 657 674
100
429 677 535 150 425 557 289 409 644 680 185 230 435 164 15 225 204 619 161 316 213
258 507 13 654 279 700 663 635 616 17 691 651 529 667 646 627 158 366 267 56 14
211 422 37 82 137 342 11 102 370 485 23 20 199 217 136 696 227 693 54 135 139 179
453 391 321 467 411 174 173 212 465 395 189 575 87 361 631 652 606 489 144 38 587
302 287 541 582 514 315 246 243 171 176 426 285 658 145 216
11 13 14 15 17 20 23 37 38 54 56 82 87 102 135 136 137 139 144 145 150 158 161 164
171 173 174 176 179 185 189 199 204 211 212 213 216 217 225 227 230 243 246 258
267 279 285 287 289 302 315 316 321 342 361 366 370 391 395 409 411 422 425 426
429 435 453 465 467 485 489 507 514 529 535 541 557 575 582 587 606 616 619 627
631 635 644 646 651 652 654 658 663 667 677 680 691 693 696 700
30
477 194 219 206 222 223 249 695 200 383 552 352 57 423 28 585 126 525 101 422 466
455 656 35 586 37 329 689 524 84
28 35 37 57 84 101 126 194 200 206 219 222 223 249 329 352 383 422 423 455 466 477
524 525 552 585 586 656 689 695
25
99 62 28 34 77 35 94 99 26 99 55 16 79 32 20 51 92 14 48 24 65 82 22 72 38
14 16 20 22 24 26 28 32 34 35 38 48 51 55 62 65 72 77 79 82 92 94 99 99 99
13/56
Weightage - 15 Input Output
10
99 55 16 79 32 20 51 92 14 48
14 16 20 32 48 51 55 79 92 99
4
15 25 35 10
10 15 25 35
3
120 60 80
60 80 120
Solution
14/56
#include <stdio.h>
int main() {
int n;
scanf("%d", &n);
int arr[n];
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
quickSort(arr, 0, n - 1);
printArray(arr, n);
return 0;
}
15/56
#include <iostream>
using namespace std;
int main()
{
int n;
cin >> n;
int arr[n];
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
quickSort(arr, 0, n - 1);
printArray(arr, n);
16/56
return 0;
}
1
2022
2022
2
2022 2021
2021 2022
4
2016 2018 3018 2017
5
1425 3657 8965 4123 1023
10
2000 2013 2014 2314 4152 3214 1320 2020 2023 1450
1320 1450 2000 2013 2014 2020 2023 2314 3214 4152
15
2563 2145 2365 7458 8965 5423 2012 2023 2021 2452 3210 4523 3102 3103 1265
1265 2012 2021 2023 2145 2365 2452 2563 3102 3103 3210 4523 5423 7458 8965
5
2014 2009 2000 1997 1865
3
1496 3015 2056
17/56
1496 2056 3015
Solution
#include <iostream>
#include <string>
return i + 1;
}
int main() {
int n;
cin >> n;
string years[n];
recursiveQuickSort(years, n);
return 0;
}
18/56
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
return i + 1;
}
int main() {
int n;
scanf("%d", &n);
char *years[n];
recursiveQuickSort(years, 0, n - 1);
19/56
return 0;
}
2
1.5 4.8
4.8 1.5
3
4.2 1.7 3.5
6
1.2 3.2 4.1 3.6 1.4 4.6
7
2.3 5.0 1.4 3.4 3.5 3.6 4.5
10
1.2 2.3 3.1 4.5 3.2 4.6 1.8 2.9 3.7 4.1
4.6 4.5 4.1 3.7 3.2 3.1 2.9 2.3 1.8 1.2
3
3.6 4.4 2.9
6
1.2 4.9 3.5 2.7 5.0 3.1
Solution
20/56
#include <iostream>
#include <iomanip>
int main() {
int numStudents;
return 0;
}
21/56
#include <stdio.h>
int main() {
int numStudents;
scanf("%d", &numStudents);
float gpa[numStudents];
return 0;
}
22/56
6
Alice
Peter
Olivia
Lucas
Sophia
Daniel
Alice
Daniel
Lucas
Olivia
Peter
Sophia
10
Liam
Sophia
Noah
Olivia
William
Ava
James
Isabella
Oliver
Emma
Ava
Emma
Isabella
James
Liam
Noah
Oliver
Olivia
Sophia
William
8
Ethan
Amelia
Henry
Mia
James
Ava
Logan
Emily
23/56
Amelia
Ava
Emily
Ethan
Henry
James
Logan
Mia
5
John
Emma
Adam
Sophia
Michael
Adam
Emma
John
Michael
Sophia
7
Michael
Emily
Jacob
Ava
Daniel
Mia
Matthew
Ava
Daniel
Emily
Jacob
Matthew
Mia
Michael
4
Sophia
Liam
Olivia
Noah
Liam
Noah
Olivia
Sophia
24/56
Weightage - 10 Sample Input Sample Output
5
Alice
Denver
Aadhil
Charlie
Zen
Aadhil
Alice
Charlie
Denver
Zen
6
Chandler
Monica
Ross
Joey
Rachel
Phoebe
Chandler
Joey
Monica
Phoebe
Rachel
Ross
Solution
25/56
#include <iostream>
#include <string>
int main() {
int n;
cin >> n;
string names[n];
quickSort(names, 0, n - 1);
return 0;
}
26/56
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main() {
int n;
scanf("%d", &n);
quickSort(names, 0, n - 1);
return 0;
}
27/56
Q5 Test Case Input Output
5
1 4 5 9 8
1 4 8 5 9
9
1 4 7 8 9 6 5 2 3
1 2 4 8 3 5 6 9 7
15
78 96 54 12 36 94 10 15 16 34 28 92 67 47 31
16 10 12 34 36 96 28 67 15 54 78 92 31 47 94
30
19 41 37 13 38 35 32 12 47 19 31 11 27 28 16 21 19 45 20 42 1 9 44 8 34 40 46 81
24 49
1 8 16 32 9 12 20 24 34 40 11 13 19 19 19 21 28 35 37 38 41 42 44 49 81 27 45 46
31 47
9
0 1 2 3 4 5 6 7 8
0 1 2 4 8 3 5 6 7
11
1024 512 256 128 64 32 16 8 4 2 1
Solution
28/56
#include <stdio.h>
#include <stdlib.h>
int main() {
int n;
scanf("%d", &n);
quickSort(arr, 0, n - 1);
29/56
printf("%d ", arr[i]);
}
free(arr);
return 0;
}
30/56
#include <iostream>
int main() {
int n;
cin >> n;
quickSort(arr, 0, n - 1);
delete[] arr;
return 0;
}
31/56
5
z x c v b
b c v x z
6
p l m n j i
i j l m n p
7
t f c v g h j
c f g h j t v
8
q m n w e r i k
e i k m n q r w
26
m n b v c x z a s d f g h j k l p o i u y t r e w q
a b c d e f g h i j k l m n o p q r s t u v w x y z
5
b n h u j
b h j n u
6
w e r h j k
e h j k r w
Solution
32/56
#include <iostream>
int main() {
int n;
cin >> n;
quickSort(characters, 0, n - 1);
printArray(characters, n);
delete[] characters;
return 0;
}
33/56
#include <stdio.h>
#include <stdlib.h>
int main() {
int n;
scanf("%d", &n);
if (characters == NULL) {
fprintf(stderr, "Memory allocation failed.\n");
return 1;
}
quickSort(characters, 0, n - 1);
printArray(characters, n);
free(characters);
34/56
return 0;
}
5
Apple Carrot Banana Kiwi Mango
5
Stefan Damon Elena Bonnie Enzo
8
Ant Bird Snake Camel Kangaroo Dog Elephant Lion
8
Cheetah Hyena Giraffe Bull Cow Buffalo Cat Dog
10
Interstellar Inception Manifest Flight828 Consciousness Galaxy Spectacles
Escalation DSA CPP
10
Compilation Output Maximum Allowed Memory Limit Generate Input Via Code
Via Output Memory Maximum Limit Input Generate Compilation Code Allowed
2
Cap Cat
Cat Cap
35/56
5
Chair Door Mouse Keyboard Table
Solution
36/56
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main() {
int n;
scanf("%d", &n);
quickSort(words, 0, n - 1);
37/56
#include <iostream>
#include <string>
int main() {
int n;
cin >> n;
quickSort(words, 0, n - 1);
delete[] words;
return 0;
}
6
1 4 5 -9 6 -2
-9 -2 1 4 5 6
38/56
10
1 -9 -6 -3 5 84 26 -98 14 -37
-98 -37 -9 -6 -3 1 5 14 26 84
15
98 74 56 32 14 25 36 96 87 -9 -6 -3 -2 -8 -7
-9 -8 -7 -6 -3 -2 14 25 32 36 56 74 87 96 98
25
98 74 56 32 14 25 37 95 99 96 -8 -9 -6 -7 -4 -1 -2 -3 -6 -5 23 36 97 85 75
-9 -8 -7 -6 -6 -5 -4 -3 -2 -1 14 23 25 32 36 37 56 74 75 85 95 96 97 98 99
5
6 5 -7 4 -1
-7 -1 4 5 6
8
1 4 5 9 -9 6 3 2
-9 1 2 3 4 5 6 9
Solution
39/56
#include <stdio.h>
#include <stdlib.h>
int main() {
int n;
scanf("%d", &n);
rearrangeNegativesBeforePositives(arr, n);
quickSort(arr, 0, n - 1);
40/56
free(arr);
return 0;
}
41/56
#include <iostream>
int main() {
int n;
cin >> n;
rearrangeNegativesBeforePositives(arr, n);
quickSort(arr, 0, n - 1);
42/56
delete[] arr;
return 0;
}
4
9.4 7.6 8.1 5.7
5
7.3 8.1 9.4 6.7 1.5
8
4.7 7.3 2.7 9.9 11.5 3.6 1.2 6.1
7
7.4 3.4 202.7 1.9 4.3 5.4 9.5
9
1.9 7.2 6.3 4.1 5.3 8.5 9.1 22.9 15.4
10
78.2 59.5 45.4 12.3 23.6 26.4 54.4 78.9 95.4 41.4
95.4 78.9 78.2 59.5 54.4 45.4 41.4 26.4 23.6 12.3
5
78.3 54.7 96.4 32.7 53.1
43/56
4
50.6 65.1 84.3 36.4
Solution
#include <iostream>
#include <iomanip> // Required for std::fixed and std::setprecision
int main() {
int n;
cin >> n;
quickSort(marks, 0, n - 1);
printArray(marks, n);
delete[] marks;
return 0;
}
44/56
#include <stdio.h>
#include <stdlib.h>
int main() {
int n;
scanf("%d", &n);
quickSort(marks, 0, n - 1);
printArray(marks, n);
free(marks);
return 0;
}
45/56
4
Keerthi 178
Charu 185
Preethi 190
Angel 175
5
Alice 178
Bob 198
Charles 165
David 197
Elena 156
6
Stefan 198
Damon 178
Elena 187
Bonnie 184
Alice 165
John 176
10
Milton 170
Sandeep 176
Dharun 180
Udhesh 190
Hari 177
Harry 168
Sharma 188
Patel 189
Dhoni 199
6
Mary 180
John 165
Emma 170
Joey 157
Tom 169
Cruise 175
46/56
Mary Cruise Emma Tom John Joey
3
Alice 155
Bob 185
Bob 150
Solution
47/56
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main() {
int n;
scanf("%d", &n);
48/56
free(names[i]);
}
free(names);
free(heights);
return 0;
}
49/56
#include <iostream>
#include <string>
int main() {
int n;
cin >> n;
delete[] names;
delete[] heights;
return 0;
}
6
a s d f g h
s h g f d a
50/56
Weightage - 20 Input Output
7
q w e r t y u
y w u t r q e
8
l k j h g f d s
s l k j h g f d
26
q w e r t y u i o p l k j h g f d s a z x c v b n m
z y x w v u t s r q p o n m l k j i h g f e d c b a
5
q w e r t
w t r q e
5
m a n g o
o n m g a
Solution
51/56
#include <stdio.h>
#include <stdlib.h>
int main() {
int n;
scanf("%d", &n);
quickSort(characters, 0, n - 1);
printArray(characters, n);
free(characters);
return 0;
}
52/56
#include <iostream>
int main() {
int n;
cin >> n;
quickSort(characters, 0, n - 1);
printArray(characters, n);
delete[] characters;
return 0;
}
4
9 7 8 5
9 8 7 5
53/56
5
7 8 9 6 1
9 8 7 6 1
8
4 7 2 9 11 3 1 6
11 9 7 6 4 3 2 1
7
7 3 2 1 4 5 9
9 7 5 4 3 2 1
9
1 7 6 4 5 8 9 22 15
22 15 9 8 7 6 5 4 1
10
78 59 45 12 23 26 54 79 95 41
95 79 78 59 54 45 41 26 23 12
5
78 54 96 32 53
96 78 54 53 32
4
53 65 84 36
84 65 53 36
Solution
54/56
#include <iostream>
int main() {
int n;
cin >> n;
quickSort(scores, 0, n - 1);
printArray(scores, n);
delete[] scores;
return 0;
}
55/56
#include <stdio.h>
#include <stdlib.h>
int main() {
int n;
scanf("%d", &n);
quickSort(scores, 0, n - 1);
printArray(scores, n);
free(scores);
return 0;
}
56/56