DSA - Unit 4 - Lecture 23 Coding
DSA - Unit 4 - Lecture 23 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
Alex is a young computer science enthusiast who loves solving coding problems. One
day, Alex stumbled upon a unique challenge related to sorting numbers. The challenge
involves sorting a list of integers with a unique twist: Alex aims to place the even numbers
in their original input order first, followed by the odd numbers in the same input order.
Your task is to help Alex implement a logic of merge sort and a recursive function to
arrange the even and odd numbers separately.
Input Format
The first line contains an integer n, the number of integers in the list.
The second line contains n space-separated integers, a[i], representing the elements of
the list.
Output Format
The output displays a single line containing n space-separated integers, representing the
sorted list, with even numbers appearing first in the given input order, followed by odd
numbers in the given input order.
1/75
Refer to the sample outputs for the exact format.
Constraints
1 ≤ n ≤ 30
4
1 2 3 4
2 4 1 3
5
1 5 3 -4 0
-4 0 1 5 3
You have a dictionary with a collection of characters. Implement a program that sorts the
characters in lexicographic order using the recursive Merge Sort algorithm.
Call the function mergeSort to sort the array of characters. The program should take the
unsorted list of characters as input and output the sorted list of characters.
Note: The lexicographical order of characters will be 'A', 'B', 'C', …, 'Y', 'Z', 'a', 'b', 'c', …,
'y', 'z'.
Input Format
The first line of input consists of the characters n, representing the number of
charactersin the dictionary.
Output Format
2/75
The output displays a sorted list of characters in lexicographic order, with each character
separated by a space.
Constraints
MAX_Char_Length 30
1 ≤ n ≤ 30
4
z g v a
Sorted Characters: a g v z
3
a b a
Sorted Characters: a a b
5
a b A c D
Sorted Characters: A D a b c
Arsha is working on a project where binary data is an essential part of the processing. To
optimize data handling, Arsha needs to sort an array containing only two types of
elements: 0s and 1s. Alex decides to implement a merge sort algorithm to efficiently sort
this binary data.
Write a program to assist Arsha in implementing the logic of merge sort along with a
recursive function to sort an array of binary data in ascending order.
Input Format
3/75
The first line contains an integer, representing the number of elements in the array.
The second line contains n space-separated integers, where each integer is either 0 or 1.
Output Format
The output displays the following result:
If the input contains elements other than 0 and 1, print "Invalid input.".
Otherwise, print a single line containing n space-separated integers, representing
the sorted array in ascending order.
Constraints
1 ≤ n ≤ 20
5
1 0 1 0 1
0 0 1 1 1
3
1 0 2
Invalid input.
4
1 1 1 1
1 1 1 1
2
0 0
0 0
4/75
You are developing a leaderboard system for an online game. The scores of the players
are stored in an array of integers. To display the leaderboard, you need to sort the scores
in ascending order.
Write code to implement a recursive merge sort algorithm for sorting the scores.
Input Format
The first line of input consists of an integer n, representing the number of scores in the
array.
The second line of input consists of n space-separated integers, representing the scores
of the players.
Output Format
The first line of output displays the initial array of scores before sorting.
The second line of output displays the sorted array of scores after applying the merge
sort algorithm.
Constraints
1 <= n <= 10
5
3 1 4 2 5
3 1 4 2 5
1 2 3 4 5
3
9 7 8
9 7 8
7 8 9
5/75
Time Limit: - ms Memory Limit: - kb Code Size: - kb
Q5.
Problem Statement
In a mystical land known as Eldoria, ancient wizards use magical runes to cast powerful
spells. These runes are represented by single characters, each possessing a unique
magical property. However, the wizards have a challenge: they need these magical runes
sorted in a specific order for their spells to work correctly.
Write a program to help the wizards of Eldoria sort their magical runes based on their
potency. Each rune is represented by a single character, and each character holds a
unique level of magical power, determined by its position in the ASCII table.
Your task is to implement a merge sorting logic and recursive function to arrange these
magical runes in descending order of their magical potency.
Input Format
The first line of input consists of an integer n, representing the number of magical runes
to be sorted.
Output Format
The output displays a single line containing the magical runes sorted in descending order
of their magical potency, separated by spaces.
Constraints
1 <= n <= 25
Each character in the array is a single uppercase letter or a single lowercase letter.
8
G h I J K L m n
n m h L K J I G
6/75
6
a Z A B M Z
a Z Z M B A
At the "Fresh Mart" grocery store, they recently received a large shipment of various
products, and they need to organize their inventory efficiently. They've hired you to help
them sort the products in descending order based on their product IDs.
Develop a program to aid the Fresh Mart team in organizing their inventory using a
recursive function and implementing the merge sort logic. Each product in the store is
identified by a distinct product ID, and the objective is to present the products to
customers in descending order.
Input Format
The first line consists of an integer, n, representing the number of products in the
inventory.
The next line consists of n space-separated integers, each representing a unique Product
ID.
Output Format
The output displays a single line containing the Product IDs sorted in descending order,
separated by spaces.
Constraints
1 ≤ n ≤ 10
1 ≤ Product ID ≤ 1000
5
1 9 7 6 8
9 8 7 6 1
4
754 312 505 44
7/75
Time Limit: - ms Memory Limit: - kb Code Size: - kb
Q7.
Problem Statement
Alice is working on a project that requires sorting and displaying the frequency of
elements in an array. She needs your help to write a program to sort an array of positive
integers and display the elements sorted by frequency in descending order. If two
elements have the same frequency, they should be sorted in ascending order of their
values.
For this project, your task is to implement the logic of the merge sort and a recursive
function.
Input Format
The first line contains an integer n, representing the number of elements in the array.
Output Format
The output prints a single line containing the sorted elements of the array separated by
spaces.
Constraints
max_n = 100
1 ≤ n ≤ 25
6
1 1 2 3 3 3
3 3 3 1 1 2
7
2 2 3 1 3 2 3
2 2 2 3 3 3 1
6
1 2 3 1 2 3
8/75
1 1 2 2 3 3
Write a program to sort a given set of characters. The characters should be sorted in such
a way that vowels appear first, followed by consonants. Within the vowels and
consonants, maintain the original input order of characters.
Your task is to assist John in implementing the logic for merge sort and a recursive
function to achieve the goal stated in the program.
Input Format
The first line consists of an integer n, representing the number of characters in the list.
Output Format
The output displays the characters sorted first by vowels, maintaining the original order of
input, followed by consonants in the same input order.
Constraints
1 ≤ n ≤ 15
1 ≤ |characters| ≤ 15
8
e A b a i o u D
e A a i o u b D
8
r a E y T m b N
9/75
a E r y T m b N
3
b h l
b h l
Sarah is an avid programmer who loves to solve interesting problems. Today, she
encountered a unique challenge related to sorting odd numbers from an array of integers.
She wants to sort the odd numbers in ascending order while keeping the even numbers in
their original positions.
Sarah has contacted you for help with this challenge. Can you implement both the logic of
merge sort and a recursive function to achieve the above task?
Input Format
The first line contains an integer n, representing the number of elements in the array.
The second line contains n space-separated integers, representing the elements of the
array.
Output Format
If there are no odd numbers in the array, output prints "None".
If there are odd numbers in the array, the output displays sorted odd numbers separated
by a space in ascending order.
Constraints
1 ≤ n ≤ 10
5
105 236 789 415 364
10/75
105 415 789
4
-9 -47 56 32
-47 -9
3
2 4 6
None
You are a student attending a class on sorting techniques taught by Professor Smith.
Today, Professor Smith is explaining how to efficiently sort both positive and negative
values using the merge sort algorithm and a recursive function. To better understand the
concept, you decide to write a program.
Your task is to write code to implement a recursive merge sort algorithm for sorting
positive and negative floating-point numbers, as explained by Professor Smith.
Input Format
The first line contains an integer n, representing the number of values to be sorted.
Output Format
The output displays a single line containing n space-separated floating-point numbers,
representing the sorted values in ascending order. Each number is rounded to two
decimal places.
Constraints
1 ≤ n ≤ 15
11/75
Sample Input Sample Output
5
3.14 1.1 2.71 0.5 1.618
Sorted Array:
0.50 1.10 1.62 2.71 3.14
6
-2.5 5.0 0.0 -1.5 2.0 3.5
Sorted Array:
-2.50 -1.50 0.00 2.00 3.50 5.00
John is a student who just received his test scores (floating-point numbers) for various
subjects. He wants to organize his scores in descending order so that he can see his
highest scores first. Can you help him write a program to sort his test scores in
descending order?
Your task is to write a program to sort John's test scores in descending order using the
Merge Sort algorithm and a recursive function.
Input Format
The first line contains an integer, n, representing the number of test scores John has
received.
The next line contains n floating-point numbers separated by spaces, each representing
John's test score.
Output Format
The output displays the sorted test scores in descending order, each rounded to two
decimal places.
Constraints
1 ≤ n ≤ 15
12/75
Sample Input Sample Output
4
9.8 1.1 3.3 7.7
Sorted Array:
9.80 7.70 3.30 1.10
5
3.14 1.1 2.71 0.5 1.618
Sorted Array:
3.14 2.71 1.62 1.10 0.50
Raj wants to learn a sequence of integers. His task is to count the frequency of each
unique integer in the sequence and then sort them based on their frequencies in
ascending order. If two integers have the same frequency, sort them in ascending order of
their values.
For this goal, your task is to implement the logic of the merge sort and a recursive
function.
Example 1
Input:
nums = [1, 1, 2, 2, 2, 3]
Output:
[3,1,1,2,2,2]
Explanation:
13/75
'3' has a frequency of 1, '1' has a frequency of 2, and '2' has a frequency of 3.
Example 2
Input:
nums = [3, 2, 1, 3, 2]
Output:
[1,2,2,3,3]
Explanation:
'3' and '2' both have a frequency of 2, so they are sorted based on their actual values,
with the smaller value coming first.
Input Format
The first line of input contains an integer, n, representing the number of integers in the
sequence.
The second line of input contains n space-separated integers, arr [i], representing the
elements of the sequence.
Output Format
The output displays a single line containing the sorted integers separated by space
according to their frequencies and values.
Constraints
max_n = 100
1 <= n <= 25
6
1 1 2 2 2 3
3 1 1 2 2 2
14/75
5
3 2 1 3 2
1 2 2 3 3
Section 1 - Coding
Q1 Test Case Input Output
4
9 2 7 4
2 4 9 7
6
12 45 7 24 9 17
12 24 45 7 9 17
4
100 17 41 120
100 120 17 41
3
5 10 30
10 30 5
10
-2 3 -4 5 -6 7 -8 9 11 3
-2 -4 -6 -8 3 5 7 9 11 3
30
50 10 90 30 80 20 70 60 20 34 2 3 4 5 6 2 3 4 5 6 3 5 2 7 8 9 11 23 24 56
50 10 90 30 80 20 70 60 20 34 2 4 6 2 4 6 2 8 24 56 3 5 3 5 3 5 7 9 11 23
4
1 2 3 4
15/75
2 4 1 3
5
1 5 3 -4 0
-4 0 1 5 3
Solution
16/75
#include <iostream>
int i = 0, j = 0, k = left;
int main() {
int n;
cin >> n;
17/75
int arr[n];
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
mergeSort(arr, 0, n - 1);
return 0;
}
18/75
#include <stdio.h>
int i = 0, j = 0, k = left;
int main() {
int n;
scanf("%d", &n);
int arr[n];
for (int i = 0; i < n; i++) {
19/75
scanf("%d", &arr[i]);
}
mergeSort(arr, 0, n - 1);
return 0;
}
6
w x y z a b
Sorted Characters: a b w x y z
5
q p o n m
Sorted Characters: m n o p q
10
e j k p l n m i h o
Sorted Characters: e h i j k l m n o p
12
c a d b f e h j i g k l
Sorted Characters: a b c d e f g h i j k l
15
e d C b a f g h i j K l m N o
Sorted Characters: C K N a b d e f g h i j l m o
26
g p j h a l e c f o b n m d k i r q s t u v w x y z
Sorted Characters: 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
20/75
4
z g v a
Sorted Characters: a g v z
3
a b a
Sorted Characters: a a b
5
a b A c D
Sorted Characters: A D a b c
Solution
21/75
#include <stdio.h>
#define MAX_Char_LENGTH 30
22/75
arr[p][l] = temp[q][l];
l++;
}
arr[p][l] = '\0';
}
}
int main() {
int size;
scanf("%d", &size);
char arr[size][MAX_Char_LENGTH];
return 0;
}
23/75
#include <iostream>
#define MAX_Char_LENGTH 30
int main() {
int size;
cin >> size;
char arr[size][MAX_Char_LENGTH];
24/75
for (int i = 0; i < size; i++) {
cin >> arr[i];
}
return 0;
}
7
1 0 1 0 1 0 2
Invalid input.
10
0 1 0 0 1 1 0 1 0 1
0 0 0 0 0 1 1 1 1 1
12
0 1 0 1 0 1 0 1 0 1 0 1
0 0 0 0 0 0 1 1 1 1 1 1
10
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
15
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
20
0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1
0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1
25/75
Weightage - 25 Sample Input Sample Output
5
1 0 1 0 1
0 0 1 1 1
3
1 0 2
Invalid input.
4
1 1 1 1
1 1 1 1
2
0 0
0 0
Solution
26/75
#include <stdio.h>
int i = 0, j = 0, k = left;
int main() {
int n;
scanf("%d", &n);
int arr[n];
27/75
scanf("%d", &arr[i]);
if (arr[i] != 0 && arr[i] != 1) {
printf("Invalid input.\n");
return 0;
}
}
mergeSort(arr, 0, n - 1);
return 0;
}
28/75
#include <iostream>
using namespace std;
int i = 0, j = 0, k = left;
int main() {
int n;
cin >> n;
int arr[n];
29/75
for (int i = 0; i < n; i++) {
cin >> arr[i];
if (arr[i] != 0 && arr[i] != 1) {
cout << "Invalid input." << endl;
return 0;
}
}
mergeSort(arr, 0, n - 1);
return 0;
}
8
5 3 7 1 8 2 6 4
5 3 7 1 8 2 6 4
1 2 3 4 5 6 7 8
5
27 19 36 14 5
27 19 36 14 5
5 14 19 27 36
7
42 38 51 47 56 40 33
42 38 51 47 56 40 33
33 38 40 42 47 51 56
7
25 17 20 23 18 16 22
25 17 20 23 18 16 22
16 17 18 20 22 23 25
10
23 61 85 96 74 56 85 14 20 0
30/75
23 61 85 96 74 56 85 14 20 0
0 14 20 23 56 61 74 85 85 96
7
18 22 15 10 24 19 13
18 22 15 10 24 19 13
10 13 15 18 19 22 24
5
3 1 4 2 5
3 1 4 2 5
1 2 3 4 5
3
9 7 8
9 7 8
7 8 9
Solution
31/75
#include <stdio.h>
int leftArr[n1];
int rightArr[n2];
int i = 0, j = 0, k = left;
int main() {
int size;
scanf("%d", &size);
32/75
int array[size];
for (int i = 0; i < size; i++) {
scanf("%d", &array[i]);
}
return 0;
}
33/75
#include <iostream>
using namespace std;
int leftArr[n1];
int rightArr[n2];
int i = 0, j = 0, k = left;
int main() {
int size;
34/75
cin >> size;
int array[size];
for (int i = 0; i < size; i++) {
cin >> array[i];
}
return 0;
}
7
a C d B E E G
d a G E E C B
5
a b c d i
i d c b a
10
F B K G j C l D A I
l j K I G F D C B A
12
L K J I H g F E D c B a
g c a L K J I H F E D B
24
X W V U t S R q P O N M L k J I H g F E D C B A
t q k g X W V U S R P O N M L J I H F E D C B A
35/75
Weightage - 25 Input Output
25
P q R s T U V W X Y Z A b C D e F G H I j K L m N O
s q m j e b Z Y X W V U T R P N L K I H G F D C A
8
G h I J K L m n
n m h L K J I G
6
a Z A B M Z
a Z Z M B A
Solution
36/75
#include <stdio.h>
char left_arr[n1];
char right_arr[n2];
int i = 0, j = 0, k = left;
while (i < n1 && j < n2) {
if (left_arr[i] >= right_arr[j]) { // Compare in descending order
arr[k] = left_arr[i];
i++;
} else {
arr[k] = right_arr[j];
j++;
}
k++;
}
int main() {
int n;
scanf("%d", &n);
char arr[n];
for (int i = 0; i < n; i++)
37/75
scanf(" %c", &arr[i]); // Read single characters
mergeSortDescending(arr, 0, n - 1);
return 0;
}
38/75
#include <iostream>
using namespace std;
char left_arr[n1];
char right_arr[n2];
int i = 0, j = 0, k = left;
while (i < n1 && j < n2) {
if (left_arr[i] >= right_arr[j]) { // Compare in descending order
arr[k] = left_arr[i];
i++;
} else {
arr[k] = right_arr[j];
j++;
}
k++;
}
int main() {
int n;
cin >> n;
char arr[MAX_ARRAY_LENGTH];
39/75
for (int i = 0; i < n; i++)
cin >> arr[i]; // Read single characters
mergeSortDescending(arr, 0, n - 1);
return 0;
}
5
1 7 98 6 3
98 7 6 3 1
5
78 6 4 2 3
78 6 4 3 2
7
4 5 9 7 1 3 6
9 7 6 5 4 3 1
7
9 6 3 1 4 8 2
9 8 6 4 3 2 1
10
117 216 43 114 578 125 182 876 710 912
10
7 9 63 45 1 8 3 4 91 22
91 63 45 22 9 8 7 4 3 1
40/75
5
1 9 7 6 8
9 8 7 6 1
4
754 312 505 44
Solution
41/75
#include <iostream>
42/75
{
int mid;
if (low < high)
{
mid=(low+high)/2;
// Split the data into two half.
MergeSort(a, low, mid);
MergeSort(a, mid+1, high);
int main()
{
int n, i;
cin>>n;
int arr[n];
for(i = 0; i < n; i++)
{
cin>>arr[i];
}
MergeSort(arr, 0, n-1);
for (i = (n-1); i >=0; i--)
cout<<arr[i]<<" ";
return 0;
}
43/75
#include <stdio.h>
44/75
int main() {
int n, i;
scanf("%d", &n);
int arr[n];
for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
MergeSort(arr, 0, n - 1);
for (i = (n - 1); i >= 0; i--) {
printf("%d ", arr[i]);
}
return 0;
}
9
1 1 6 4 5 6 1 4 1
1 1 1 1 4 4 6 6 5
12
6 56 15 45 74 87 96 5 4 2 3 56
56 56 2 3 4 5 6 15 45 74 87 96
15
5 98 85 84 21 45 5 5 78 5 63 21 98 78 21
5 5 5 5 21 21 21 78 78 98 98 45 63 84 85
25
89 6 9 8 7 55 55 78 5 5 78 5 78 89 6 9 45 65 79 78 45 65 78 89 9
78 78 78 78 78 5 5 5 9 9 9 89 89 89 6 6 45 45 55 55 65 65 7 8 79
8
3 2 5 3 5 2 1 5
5 5 5 2 2 3 3 1
7
3 3 8 9 1 1 1
45/75
1 1 1 3 3 8 9
6
1 1 2 3 3 3
3 3 3 1 1 2
7
2 2 3 1 3 2 3
2 2 2 3 3 3 1
6
1 2 3 1 2 3
1 1 2 2 3 3
Solution
46/75
#include <stdio.h>
void merge(int arr[], int temp[], int left, int mid, int right) {
int n1 = mid - left + 1;
int n2 = right - mid;
int i = 0, j = 0, k = left;
int index = 0;
for (int i = 0; i < n; i++) {
int isAlreadyCounted = 0;
for (int j = 0; j < index; j++) {
if (values[j] == arr[i]) {
freqArr[j][1]++;
isAlreadyCounted = 1;
break;
}
}
if (!isAlreadyCounted) {
values[index] = arr[i];
freqArr[index][0] = arr[i];
freqArr[index][1] = 1;
index++;
47/75
}
}
temp = freqArr[i][1];
freqArr[i][1] = freqArr[j][1];
freqArr[j][1] = temp;
}
}
}
int main() {
int n;
scanf("%d", &n);
int arr[MAX_N];
countAndSortFrequency(arr, n);
return 0;
}
48/75
#include <iostream>
void merge(int arr[], int temp[], int left, int mid, int right) {
int n1 = mid - left + 1;
int n2 = right - mid;
int i = 0, j = 0, k = left;
int index = 0;
for (int i = 0; i < n; i++) {
int isAlreadyCounted = 0;
for (int j = 0; j < index; j++) {
if (values[j] == arr[i]) {
freqArr[j][1]++;
isAlreadyCounted = 1;
break;
}
}
if (!isAlreadyCounted) {
values[index] = arr[i];
freqArr[index][0] = arr[i];
49/75
freqArr[index][1] = 1;
index++;
}
}
temp = freqArr[i][1];
freqArr[i][1] = freqArr[j][1];
freqArr[j][1] = temp;
}
}
}
int main() {
int n;
cin >> n;
int arr[MAX_N];
countAndSortFrequency(arr, n);
return 0;
}
3
y X Z
y X Z
7
J i h I j G H
i I J h j G H
50/75
5
p O q R s
O p q R s
10
P p C c F f Z z R r
P p C c F f Z z R r
12
L g H t a X b Z R Y p u
a u L g H t X b Z R Y p
15
X o L e O y O U b e l i e v e
o e O O U e i e e X L y b l v
8
e A b a i o u D
e A a i o u b D
8
r a E y T m b N
a E r y T m b N
3
b h l
b h l
Solution
51/75
#include <iostream>
void merge(char characters[], int left, int middle, int right, int
consonant_count[]) {
int n1 = middle - left + 1;
int n2 = right - middle;
char leftArray[MAX_N];
char rightArray[MAX_N];
int leftConsonantCount[MAX_N];
int rightConsonantCount[MAX_N];
int i = 0, j = 0, k = left;
while (i < n1 && j < n2) {
if (leftConsonantCount[i] <= rightConsonantCount[j]) {
characters[k] = leftArray[i];
consonant_count[k] = leftConsonantCount[i];
i++;
} else {
characters[k] = rightArray[j];
consonant_count[k] = rightConsonantCount[j];
j++;
}
k++;
}
52/75
characters[k] = rightArray[j];
consonant_count[k] = rightConsonantCount[j];
j++;
k++;
}
}
int main() {
int n;
cin >> n;
char characters[MAX_N];
int consonant_count[MAX_N];
mergeSort(characters, 0, n - 1, consonant_count);
return 0;
}
53/75
#include <stdio.h>
void merge(char characters[], int left, int middle, int right, int
consonant_count[]) {
int n1 = middle - left + 1;
int n2 = right - middle;
char leftArray[n1];
char rightArray[n2];
int leftConsonantCount[n1];
int rightConsonantCount[n2];
int i = 0, j = 0, k = left;
while (i < n1 && j < n2) {
if (leftConsonantCount[i] <= rightConsonantCount[j]) {
characters[k] = leftArray[i];
consonant_count[k] = leftConsonantCount[i];
i++;
} else {
characters[k] = rightArray[j];
consonant_count[k] = rightConsonantCount[j];
j++;
}
k++;
}
54/75
k++;
}
}
int main() {
int n;
scanf("%d", &n);
char characters[n];
int consonant_count[n];
mergeSort(characters, 0, n - 1, consonant_count);
return 0;
}
5
2 8 98 6 4
None
5
-78 6 4 2 3
7
4 5 9 7 1 3 6
1 3 5 7 9
55/75
Weightage - 15 Input Output
7
-9 6 -3 -1 4 8 2
-9 -3 -1
10
7 6 3 14 8 5 12 86 71 12
3 5 7 71
10
7 9 -63 45 1 8 3 4 -91 22
-91 -63 1 3 7 9 45
5
105 236 789 415 364
4
-9 -47 56 32
-47 -9
3
2 4 6
None
Solution
56/75
#include <iostream>
using namespace std;
void merge(int arr[], int left[], int left_size, int right[], int right_size) {
int i = 0, j = 0, k = 0;
mergeSortOdd(left, mid);
mergeSortOdd(right, n - mid);
int main() {
int n;
cin >> n;
int arr[n];
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
int oddCount = 0;
for (int i = 0; i < n; i++) {
if (arr[i] % 2 != 0) {
oddCount++;
57/75
}
}
if (oddCount == 0) {
cout << "None";
} else {
mergeSortOdd(arr, n);
for (int i = 0; i < n; i++) {
if (arr[i] % 2 != 0) {
cout << arr[i] << " ";
}
}
cout << endl;
}
return 0;
}
58/75
#include <stdio.h>
void merge(int arr[], int left[], int left_size, int right[], int right_size) {
int i = 0, j = 0, k = 0;
mergeSortOdd(left, mid);
mergeSortOdd(right, n - mid);
int main() {
int n;
scanf("%d", &n);
int arr[n];
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
int oddCount = 0;
for (int i = 0; i < n; i++) {
if (arr[i] % 2 != 0) {
oddCount++;
59/75
}
}
if (oddCount == 0) {
printf("None");
} else {
mergeSortOdd(arr, n);
for (int i = 0; i < n; i++) {
if (arr[i] % 2 != 0) {
printf("%d ", arr[i]);
}
}
printf("\n");
}
return 0;
}
4
9.8 1.1 3.3 7.7
Sorted Array:
1.10 3.30 7.70 9.80
3
-1.0 -2.0 -3.0
Sorted Array:
-3.00 -2.00 -1.00
6
-2.5 5.0 0.0 -1.5 -2.0 -3.5
Sorted Array:
-3.50 -2.50 -2.00 -1.50 0.00 5.00
8
5.5 4.4 3.3 2.2 1.1 0.0 -1.1 -2.2
Sorted Array:
-2.20 -1.10 0.00 1.10 2.20 3.30 4.40 5.50
10
10.1 20.2 30.3 40.4 50.5 60.6 70.7 80.8 90.9 100.0
60/75
Sorted Array:
10.10 20.20 30.30 40.40 50.50 60.60 70.70 80.80 90.90 100.00
15
3.14 1.1 2.71 0.5 1.618 2.0 0.25 4.0 5.5 1.25 0.75 6.0 7.5 8.0 9.0
Sorted Array:
0.25 0.50 0.75 1.10 1.25 1.62 2.00 2.71 3.14 4.00 5.50 6.00 7.50 8.00 9.00
5
3.14 1.1 2.71 0.5 1.618
Sorted Array:
0.50 1.10 1.62 2.71 3.14
6
-2.5 5.0 0.0 -1.5 2.0 3.5
Sorted Array:
-2.50 -1.50 0.00 2.00 3.50 5.00
Solution
61/75
#include <stdio.h>
int i = 0, j = 0, k = left;
int main() {
int n;
scanf("%d", &n);
float arr[n];
for (int i = 0; i < n; i++) {
62/75
scanf("%f", &arr[i]);
}
mergeSort(arr, 0, n - 1);
printf("Sorted Array:\n");
for (int i = 0; i < n; i++) {
printf("%.2f ", arr[i]);
}
printf("\n");
return 0;
}
63/75
#include <iostream>
#include <iomanip>
int i = 0, j = 0, k = left;
int main() {
int n;
cin >> n;
64/75
float arr[n];
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
mergeSort(arr, 0, n - 1);
return 0;
}
6
2.5 5.0 0.0 1.5 2.0 3.5
Sorted Array:
5.00 3.50 2.50 2.00 1.50 0.00
3
1.0 2.0 3.0
Sorted Array:
3.00 2.00 1.00
6
2.5 5.0 0.0 14.5 2.0 5.5
Sorted Array:
14.50 5.50 5.00 2.50 2.00 0.00
8
5.5 4.4 3.3 2.2 1.1 0.0 1.1 2.2
Sorted Array:
5.50 4.40 3.30 2.20 2.20 1.10 1.10 0.00
10
10.1 20.2 30.3 40.4 50.5 60.6 70.7 80.8 90.9 100.0
Sorted Array:
100.00 90.90 80.80 70.70 60.60 50.50 40.40 30.30 20.20 10.10
65/75
Weightage - 25 Input Output
15
3.14 1.1 2.71 0.5 1.618 2.0 0.25 4.0 5.5 1.25 0.75 6.0 7.5 8.0 9.0
Sorted Array:
9.00 8.00 7.50 6.00 5.50 4.00 3.14 2.71 2.00 1.62 1.25 1.10 0.75 0.50 0.25
4
9.8 1.1 3.3 7.7
Sorted Array:
9.80 7.70 3.30 1.10
5
3.14 1.1 2.71 0.5 1.618
Sorted Array:
3.14 2.71 1.62 1.10 0.50
Solution
66/75
#include <stdio.h>
int i = 0, j = 0, k = left;
int main() {
int n;
scanf("%d", &n);
float arr[n];
for (int i = 0; i < n; i++) {
67/75
scanf("%f", &arr[i]);
}
mergeSort(arr, 0, n - 1);
printf("Sorted Array:\n");
for (int i = 0; i < n; i++) {
printf("%.2f ", arr[i]);
}
printf("\n");
return 0;
}
68/75
#include <iostream>
#include <iomanip>
int i = 0, j = 0, k = left;
int main() {
int n;
cin >> n;
69/75
float arr[n];
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
mergeSort(arr, 0, n - 1);
return 0;
}
9
1 1 6 4 5 6 1 4 1
5 4 4 6 6 1 1 1 1
12
6 56 15 45 74 87 96 5 4 2 3 56
2 3 4 5 6 15 45 74 87 96 56 56
15
5 98 85 84 21 45 5 5 78 5 63 21 98 78 21
45 63 84 85 78 78 98 98 21 21 21 5 5 5 5
25
89 6 9 8 7 55 55 78 5 5 78 5 78 89 6 9 45 65 79 78 45 65 78 89 9
7 8 79 6 6 45 45 55 55 65 65 5 5 5 9 9 9 89 89 89 78 78 78 78 78
8
3 2 5 3 5 2 1 5
1 2 2 3 3 5 5 5
7
3 3 8 9 1 1 1
70/75
8 9 3 3 1 1 1
6
1 1 2 2 2 3
3 1 1 2 2 2
5
3 2 1 3 2
1 2 2 3 3
Solution
71/75
#include <stdio.h>
void merge(int arr[], int temp[], int left, int mid, int right) {
int n1 = mid - left + 1;
int n2 = right - mid;
int i = 0, j = 0, k = left;
int index = 0;
for (int i = 0; i < n; i++) {
int isAlreadyCounted = 0;
for (int j = 0; j < index; j++) {
if (values[j] == arr[i]) {
freqArr[j][1]++;
isAlreadyCounted = 1;
break;
}
}
if (!isAlreadyCounted) {
values[index] = arr[i];
freqArr[index][0] = arr[i];
freqArr[index][1] = 1;
72/75
index++;
}
}
temp = freqArr[i][1];
freqArr[i][1] = freqArr[j][1];
freqArr[j][1] = temp;
}
}
}
int main() {
int n;
scanf("%d", &n);
int arr[MAX_N];
countAndSortFrequency(arr, n);
return 0;
}
73/75
#include <iostream>
using namespace std;
void merge(int arr[], int temp[], int left, int mid, int right) {
int n1 = mid - left + 1;
int n2 = right - mid;
int i = 0, j = 0, k = left;
int index = 0;
for (int i = 0; i < n; i++) {
int isAlreadyCounted = 0;
for (int j = 0; j < index; j++) {
if (values[j] == arr[i]) {
freqArr[j][1]++;
isAlreadyCounted = 1;
break;
}
}
if (!isAlreadyCounted) {
values[index] = arr[i];
freqArr[index][0] = arr[i];
74/75
freqArr[index][1] = 1;
index++;
}
}
int main() {
int n;
cin >> n;
int arr[MAX_N];
countAndSortFrequency(arr, n);
return 0;
}
75/75