0% found this document useful (0 votes)
2 views10 pages

Coding Training Week - 6 Tasks (II and III B.tech-CSE, ECE, EEE)

The document outlines coding tasks for Week #6 in a Computer Science & Engineering course, focusing on sorting problems. It includes various challenges such as sorting a matrix by diagonals, sorting linked lists, and sorting arrays based on frequency and power values. Each task is accompanied by examples and expected outputs to guide students in their implementations.

Uploaded by

gsc619999
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views10 pages

Coding Training Week - 6 Tasks (II and III B.tech-CSE, ECE, EEE)

The document outlines coding tasks for Week #6 in a Computer Science & Engineering course, focusing on sorting problems. It includes various challenges such as sorting a matrix by diagonals, sorting linked lists, and sorting arrays based on frequency and power values. Each task is accompanied by examples and expected outputs to guide students in their implementations.

Uploaded by

gsc619999
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Department of Computer Science & Engineering

----------------------------------------------------------------------------------------------------------------

Coding Classes - Week # 6 Tasks

Academic Year: 2024-2025 Year & Semester: II & III B. Tech, IInd Sem

Branch: CSE/ECE/EEE Topic: LeetCode Problems on


Sorting - 2

1. Sort Matrix by Diagonals

You are given an n x n square matrix of integers grid. Return the matrix such that:
 The diagonals in the bottom-left triangle (including the middle diagonal) are sorted
in non-increasing order.
 The diagonals in the top-right triangle are sorted in non-decreasing order.

Example 1:

Input: grid = [[1,7,3],[9,8,2],[4,5,6]]


Output: [[8,2,3],[9,6,7],[4,5,1]]

Explanation:

The diagonals with a black arrow (bottom-left triangle) should be sorted in non-increasing
order:
 [1, 8, 6] becomes [8, 6, 1].
 [9, 5] and [4] remain unchanged.
The diagonals with a blue arrow (top-right triangle) should be sorted in non-decreasing order:
 [7, 2] becomes [2, 7].
 [3] remains unchanged.

Example 2:

Input: grid = [[0,1],[1,2]]


Output: [[2,1],[1,0]]

Explanation:
The diagonals with a black arrow must be non-increasing, so [0, 2] is changed to [2, 0]. The
other diagonals are already in the correct order.

Example 3:

Input: grid = [[1]]


Output: [[1]]

Explanation:
Diagonals with exactly one element are already in order, so no changes are needed.

2. Sort List

Given the head of a linked list, return the list after sorting it in ascending order.

Example 1:

Input: head = [4,2,1,3]


Output: [1,2,3,4]

Example 2:
Input: head = [-1,5,3,4,0]
Output: [-1,0,3,4,5]

Example 3:

Input: head = []
Output: []

3. Sort Characters By Frequency

Given a string s, sort it in decreasing order based on the frequency of the characters.
The frequency of a character is the number of times it appears in the string.
Return the sorted string. If there are multiple answers, return any of them.

Example 1:

Input: s = "tree"
Output: "eert"

Explanation: 'e' appears twice while 'r' and 't' both appear once.
So 'e' must appear before both 'r' and 't'. Therefore "eetr" is also a valid answer.

Example 2:

Input: s = "cccaaa"
Output: "aaaccc"

Explanation: Both 'c' and 'a' appear three times, so both "cccaaa" and "aaaccc" are valid
answers.
Note that "cacaca" is incorrect, as the same characters must be together.

Example 3:

Input: s = "Aabb"
Output: "bbAa"

Explanation: "bbaA" is also a valid answer, but "Aabb" is incorrect.


Note that 'A' and 'a' are treated as two different characters.

4. Shortest Unsorted Continuous Subarray

Given an integer array nums, you need to find one continuous subarray such that if you
only sort this subarray in non-decreasing order, then the whole array will be sorted in non-
decreasing order.
Return the shortest such subarray and output its length.

Example 1:

Input: nums = [2,6,4,8,10,9,15]


Output: 5
Explanation: You need to sort [6, 4, 8, 10, 9] in ascending order to make the whole array
sorted in ascending order.
Example 2:
Input: nums = [1,2,3,4]
Output: 0

Example 3:
Input: nums = [1]
Output: 0

5. Sort Array By Parity

Given an integer array nums, move all the even integers at the beginning of the array
followed by all the odd integers.
Return any array that satisfies this condition.

Example 1:

Input: nums = [3,1,2,4]


Output: [2,4,3,1]

Explanation: The outputs [4,2,3,1], [2,4,1,3], and [4,2,1,3] would also be accepted.

Example 2:

Input: nums = [0]


Output: [0]

6. Sort an Array

Given an array of integers nums, sort the array in ascending order and return it.
You must solve the problem without using any built-in functions in O(nlog(n)) time
complexity and with the smallest space complexity possible.

Example 1:

Input: nums = [5,2,3,1]


Output: [1,2,3,5]

Explanation: After sorting the array, the positions of some numbers are not changed (for
example, 2 and 3), while the positions of other numbers are changed (for example, 1 and 5).

Example 2:

Input: nums = [5,1,1,2,0,0]


Output: [0,0,1,1,2,5]

Explanation: Note that the values of nums are not necessarily unique.

7. Squares of a Sorted Array

Given an integer array nums sorted in non-decreasing order, return an array of the squares
of each number sorted in non-decreasing order.
Example 1:

Input: nums = [-4,-1,0,3,10]


Output: [0,1,9,16,100]

Explanation: After squaring, the array becomes [16,1,0,9,100].


After sorting, it becomes [0,1,9,16,100].

Example 2:

Input: nums = [-7,-3,2,3,11]


Output: [4,9,9,49,121]

8. Relative Sort Array

Given two arrays arr1 and arr2, the elements of arr2 are distinct, and all elements in arr2 are
also in arr1.
Sort the elements of arr1 such that the relative ordering of items in arr1 are the same as
in arr2. Elements that do not appear in arr2 should be placed at the end
of arr1 in ascending order.

Example 1:

Input: arr1 = [2,3,1,3,2,4,6,7,9,2,19], arr2 = [2,1,4,3,9,6]


Output: [2,2,2,1,4,3,3,9,6,7,19]

Example 2:

Input: arr1 = [28,6,22,8,44,17], arr2 = [22,28,8,6]


Output: [22,28,8,6,17,44]

9. Sort the Matrix Diagonally

A matrix diagonal is a diagonal line of cells starting from some cell in either the topmost
row or leftmost column and going in the bottom-right direction until reaching the matrix's
end. For example, the matrix diagonal starting from mat[2][0], where mat is a 6 x 3 matrix,
includes cells mat[2][0], mat[3][1], and mat[4][2].
Given an m x n matrix mat of integers, sort each matrix diagonal in ascending order and
return the resulting matrix.

Example 1:

Input: mat = [
[3,3,1,1],
[2,2,1,2],
[1,1,1,2]]
Output: [
[1,1,1,1],
[1,2,2,2],
[1,2,3,3]]

Example 2:

Input: mat = [
[11,25,66,1,69,7],
[23,55,17,45,15,52],
[75,31,36,44,58,8],
[22,27,33,25,68,4],
[84,28,14,11,5,50]]

Output: [
[5,17,4,1,52,7],
[11,11,25,45,8,69],
[14,23,25,44,58,15],
[22,27,31,36,50,66],
[84,28,75,33,55,68]]

10. Sort Integers by The Number of 1 Bits

You are given an integer array arr. Sort the integers in the array in ascending order by the
number of 1's in their binary representation and in case of two or more integers have the same
number of 1's you have to sort them in ascending order.
Return the array after sorting it.

Example 1:

Input: arr = [0,1,2,3,4,5,6,7,8]


Output: [0,1,2,4,8,3,5,6,7]

Explanation: [0] is the only integer with 0 bits.


[1,2,4,8] all have 1 bit.
[3,5,6] have 2 bits.
[7] has 3 bits.
The sorted array by bits is [0,1,2,4,8,3,5,6,7]

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.

11. Sort Integers by The Power Value

The power of an integer x is defined as the number of steps needed to


transform x into 1 using the following steps:
 if x is even then x = x / 2
 if x is odd then x = 3 * x + 1

For example, the power of x = 3 is 7 because 3 needs 7 steps to become 1 (3 --> 10 --> 5 -->
16 --> 8 --> 4 --> 2 --> 1).

Given three integers lo, hi and k. The task is to sort all integers in the interval [lo, hi] by the
power value in ascending order, if two or more integers have the same power value sort
them by ascending order.

Return the kth integer in the range [lo, hi] sorted by the power value.
Notice that for any integer x (lo <= x <= hi) it is guaranteed that x will transform
into 1 using these steps and that the power of x is will fit in a 32-bit signed integer.

Example 1:

Input: lo = 12, hi = 15, k = 2


Output: 13

Explanation: The power of 12 is 9 (12 --> 6 --> 3 --> 10 --> 5 --> 16 --> 8 --> 4 --> 2 --> 1)
The power of 13 is 9
The power of 14 is 17
The power of 15 is 17
The interval sorted by the power value [12,13,14,15]. For k = 2 answer is the second element
which is 13.
Notice that 12 and 13 have the same power value and we sorted them in ascending order.
Same for 14 and 15.

Example 2:

Input: lo = 7, hi = 11, k = 4
Output: 7

Explanation: The power array corresponding to the interval [7, 8, 9, 10, 11] is [16, 3, 19, 6,
14].
The interval sorted by power is [8, 10, 11, 7, 9].
The fourth number in the sorted array is 7.

12. Range Sum of Sorted Subarray Sums

You are given the array nums consisting of n positive integers. You computed the sum of all
non-empty continuous subarrays from the array and then sorted them in non-decreasing
order, creating a new array of n * (n + 1) / 2 numbers.
Return the sum of the numbers from index left to index right (indexed from 1), inclusive, in
the new array. Since the answer can be a huge number return it modulo 109 + 7.

Example 1:

Input: nums = [1,2,3,4], n = 4, left = 1, right = 5


Output: 13

Explanation: All subarray sums are 1, 3, 6, 10, 2, 5, 9, 3, 7, 4. After sorting them in non-
decreasing order we have the new array [1, 2, 3, 3, 4, 5, 6, 7, 9, 10]. The sum of the numbers
from index le = 1 to ri = 5 is 1 + 2 + 3 + 3 + 4 = 13.
Example 2:

Input: nums = [1,2,3,4], n = 4, left = 3, right = 4


Output: 6

Explanation: The given array is the same as example 1. We have the new array [1, 2, 3, 3, 4,
5, 6, 7, 9, 10]. The sum of the numbers from index le = 3 to ri = 4 is 3 + 3 = 6.

Example 3:

Input: nums = [1,2,3,4], n = 4, left = 1, right = 10


Output: 50

13. Sort Array by Increasing Frequency

Given an array of integers nums, sort the array in increasing order based on the frequency of
the values. If multiple values have the same frequency, sort them in decreasing order.
Return the sorted array.

Example 1:

Input: nums = [1,1,2,2,2,3]


Output: [3,1,1,2,2,2]

Explanation: '3' has a frequency of 1, '1' has a frequency of 2, and '2' has a frequency of 3.

Example 2:

Input: nums = [2,3,1,3,2]


Output: [1,3,3,2,2]

Explanation: '2' and '3' both have a frequency of 2, so they are sorted in decreasing order.

Example 3:

Input: nums = [-1,1,-6,4,5,-6,1,4,1]


Output: [5,-1,4,4,-6,-6,1,1,1]

14. Sorting the Sentence

A sentence is a list of words that are separated by a single space with no leading or trailing
spaces. Each word consists of lowercase and uppercase English letters.

A sentence can be shuffled by appending the 1-indexed word position to each word then
rearranging the words in the sentence.

For example, the sentence "This is a sentence" can be shuffled as "sentence4 a3 is2
This1" or "is2 sentence4 This1 a3".
Given a shuffled sentence s containing no more than 9 words, reconstruct and return the
original sentence.
Example 1:

Input: s = "is2 sentence4 This1 a3"


Output: "This is a sentence"

Explanation: Sort the words in s to their original positions "This1 is2 a3 sentence4", then
remove the numbers.

Example 2:

Input: s = "Myself2 Me1 I4 and3"


Output: "Me Myself and I"

Explanation: Sort the words in s to their original positions "Me1 Myself2 and3 I4", then
remove the numbers.

15. GCD Sort of an Array

You are given an integer array nums, and you can perform the following
operation any number of times on nums:

Swap the positions of two elements nums[i] and nums[j] if gcd(nums[i], nums[j]) >
1 where gcd(nums[i], nums[j]) is the greatest common divisor of nums[i] and nums[j].
Return true if it is possible to sort nums in non-decreasing order using the above swap
method, or false otherwise.

Example 1:

Input: nums = [7,21,3]


Output: true

Explanation: We can sort [7,21,3] by performing the following operations:


- Swap 7 and 21 because gcd(7,21) = 7. nums = [21,7,3]
- Swap 21 and 3 because gcd(21,3) = 3. nums = [3,7,21]

Example 2:
Input: nums = [5,2,6,2]
Output: false

Explanation: It is impossible to sort the array because 5 cannot be swapped with any other
element.

Example 3:

Input: nums = [10,5,9,3,15]


Output: true

We can sort [10,5,9,3,15] by performing the following operations:


- Swap 10 and 15 because gcd(10,15) = 5. nums = [15,5,9,3,10]
- Swap 15 and 3 because gcd(15,3) = 3. nums = [3,5,9,15,10]
- Swap 10 and 15 because gcd(10,15) = 5. nums = [3,5,9,10,15]

You might also like