Search This Blog

2024/04/08

Javascript Interview Question: Sort Even and Odd Indices Independently (LeetCode)


 Sort Even and Odd Indices Independently:
You are given a 0-indexed integer array nums. Rearrange the values of nums
according to the following rules:

Sort the values at odd indices of nums in non-increasing order. For example,
if nums = [4,1,2,3] before this step, it becomes [4,3,2,1] after. The values
at odd indices 1 and 3 are sorted in non-increasing order. Sort the values
at even indices of nums in non-decreasing order. For example, if
nums = [4,1,2,3] before this step, it becomes [2,1,4,3] after. The values
at even indices 0 and 2 are sorted in non-decreasing order. Return the
array formed after rearranging the values of nums.

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

Explanation:
First, we sort the values present at odd indices (1 and 3) in
non-increasing order. So, nums changes from [4,1,2,3] to [4,3,2,1].
Next, we sort the values present at even indices (0 and 2) in
non-decreasing order. So, nums changes from [4,1,2,3] to [2,3,4,1].
Thus, the array formed after rearranging the values is
[2,3,4,1].

Example 2:
Input: nums = [2,1]
Output: [2,1]

Explanation:
Since there is exactly one odd index and one even index, no
rearrangement of values takes place. The resultant array formed is
[2,1], which is the same as the initial array.

Constraints:
1 <= nums.length <= 100
1 <= nums[i] <= 100

Solution 1:
var sortEvenOdd = function (nums) {
let lastOddIndex;
let lastEvenIndex;

//length is odd
if (nums.length % 2 == 1) {
lastOddIndex = nums.length - 2;
lastEvenIndex = nums.length - 1;
}

//length is even
if (nums.length % 2 == 0) {
lastOddIndex = nums.length - 1;
lastEvenIndex = nums.length - 2;
}

//odd indices in decreasing order
for (let i = 1; i <= lastOddIndex - 2; i = i + 2) {
for (let j = i + 2; j <= lastOddIndex; j = j + 2) {
if (nums[i] < nums[j]) {
[nums[i], nums[j]] = [nums[j], nums[i]];
}
}
}

//even indices in increasing order
for (let i = 0; i <= lastEvenIndex - 2; i = i + 2) {
for (let j = i + 2; j <= lastEvenIndex; j = j + 2) {
if (nums[i] > nums[j]) {
[nums[i], nums[j]] = [nums[j], nums[i]];
}
}
}
return nums;
};

let result = sortEvenOdd([2,1]);
console.log(result);

Solution 2:
var sortEvenOdd = function (nums) {
//result will be pushed into following array
let newArray = [];

//even indices in increasing
let evenIndicesArray = nums.filter((m, index) => {
return index % 2 == 0;
});

console.log("Even Indices Array Before Sorting:", evenIndicesArray);

//increasing order sort
evenIndicesArray = evenIndicesArray.sort((a, b) => {
return a - b;
});
console.log(
"Even Indices Array After Sorting In Increasing Order:",
evenIndicesArray
);

//odd indices in decreasing
let oddIndicesArray = nums.filter((m, index) => {
return index % 2 == 1;
});
console.log("Odd Indices Array Before Sorting:", oddIndicesArray);

//decreasing order sort
oddIndicesArray = oddIndicesArray.sort((a, b) => {
return b - a;
});
console.log(
"Odd Indices Array After Sorting In Decreasing Order:",
oddIndicesArray
);

//merging evenIndicesArray & oddIndicesArray
for (let i = 0; i < nums.length; i++) {
if (i % 2 == 0) {
//add element from even array
newArray[i] = evenIndicesArray[i / 2];
} else {
//add element from even array
newArray[i] = oddIndicesArray[(i - 1) / 2];
}
}

return newArray;
};

let nums = [
36, 45, 32, 31, 15, 41, 9, 46, 36, 6, 15, 16, 33, 26, 27, 31, 44, 34,
];
console.log("Length:", nums.length);
console.log("Input Array:", nums);
let result = sortEvenOdd(nums);
console.log("MyResult:",result);

Output:
Length: 18
Input Array: [
36, 45, 32, 31, 15, 41, 9,
46, 36, 6, 15, 16, 33, 26,
27, 31, 44, 34
]
MyResult: [
9, 46, 15, 45, 15, 41, 27,
34, 32, 31, 33, 31, 36, 26,
36, 16, 44, 6
]

No comments:

Post a Comment