Coding Training Week - 6 Tasks (II and III B.tech-CSE, ECE, EEE)
Coding Training Week - 6 Tasks (II and III B.tech-CSE, ECE, EEE)
----------------------------------------------------------------------------------------------------------------
Academic Year: 2024-2025 Year & Semester: II & III B. Tech, IInd Sem
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:
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:
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:
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:
Example 2:
Input: head = [-1,5,3,4,0]
Output: [-1,0,3,4,5]
Example 3:
Input: head = []
Output: []
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"
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:
Example 3:
Input: nums = [1]
Output: 0
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:
Explanation: The outputs [4,2,3,1], [2,4,1,3], and [4,2,1,3] would also be accepted.
Example 2:
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:
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:
Explanation: Note that the values of nums are not necessarily unique.
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:
Example 2:
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:
Example 2:
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]]
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:
Example 2:
Explanation: All integers have 1 bit in the binary representation, you should just sort them in
ascending order.
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:
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.
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:
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:
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:
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:
Explanation: '3' has a frequency of 1, '1' has a frequency of 2, and '2' has a frequency of 3.
Example 2:
Explanation: '2' and '3' both have a frequency of 2, so they are sorted in decreasing order.
Example 3:
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:
Explanation: Sort the words in s to their original positions "This1 is2 a3 sentence4", then
remove the numbers.
Example 2:
Explanation: Sort the words in s to their original positions "Me1 Myself2 and3 I4", then
remove the numbers.
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:
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: