Daily LeetCode Problems: Problem 2366. Minimum Replacements to Sort the Array

Efficient Array Sorting: Minimum Replacements for Non-Decreasing Order

Monit Sharma
3 min readAug 30, 2023

Introduction:

Welcome to another insightful problem-solving article! Today, we’ll explore problem 2366 from LeetCode: “Minimum Replacements to Sort the Array”. This intriguing problem involves replacing array elements with their summ ands to achieve a sorted non-decreasing order. We’ll dive into the problem statement, design a strategy for optimization, analyze the complexity, and present a step-by-step solution.

Problem Statement:

We’re given an integer array nums, and our objective is to sort it in non-decreasing order. However, we can perform a unique operation: replace any element with two elements that sum to it. Our task is to determine the minimum number of such operations needed to achieve a sorted non-decreasing array.

Approach:

To tackle this problem efficiently, we’ll utilize a mathematical approach. We’ll focus on the key insight that the most efficient way to sort the array is to split each element into its prime factors. This is due to the fundamental theorem of arithmetic, which states that each positive integer can be uniquely represented as a product of prime numbers.

Pseudocode:

def minOperations(nums: List[int]) -> int:
result = 0
for num in nums:
while num % 2 == 0:
num //= 2
result += 1
return result

Complexity Analysis:

Let’s analyze the complexity of our solution:

  • Time complexity: We iterate through each element in the array once, and for each element, we divide it by 2 until it’s no longer even. Therefore, the time complexity is O(n * log(max_num)), where max_num is the maximum element in the array.
  • Space complexity: We use a constant amount of extra space, resulting in a space complexity of O(1).

Step-by-Step Solution:

  1. Initialize a variable result to keep track of the total operations.
  2. Iterate through each element num in the array:
  • While num is even, divide it by 2 and increment result by 1.
  1. Return the final result.

Full Solution

Python

class Solution:
def minimumReplacement(self, nums: List[int]) -> int:
ans = 0

max = nums[-1]
for i in range(len(nums) - 2, -1, -1):
ops = (nums[i] - 1) // max
ans += ops
max = nums[i] // (ops + 1)

return ans

Java

class Solution {
public long minimumReplacement(int[] nums) {
long ans = 0;

int max = nums[nums.length - 1];
for (int i = nums.length - 2; i >= 0; --i) {
final int ops = (nums[i] - 1) / max;
ans += ops;
max = nums[i] / (ops + 1);
}

return ans;
}
}

C++

class Solution {
public:
long long minimumReplacement(vector<int>& nums) {
long long ans = 0;

int max = nums.back();
for (int i = nums.size() - 2; i >= 0; --i) {
const int ops = (nums[i] - 1) / max;
ans += ops;
max = nums[i] / (ops + 1);
}

return ans;
}
};

Conclusion:

In this article, we explored problem 2366, “Minimum Replacements to Sort the Array,” and devised an efficient strategy to sort an array in non-decreasing order using the minimum number of operations. By leveraging the concept of prime factors and their unique representation, we were able to optimize the sorting process. Understanding this problem sheds light on the significance of prime numbers in mathematical and computational contexts. Implementing our approach will help you achieve array sorting with minimal operations. Happy sorting!

--

--