Similar Problems

Similar Problems not available

Replace Non Coprime Numbers In Array - Leetcode Solution

Companies:

LeetCode:  Replace Non Coprime Numbers In Array Leetcode Solution

Difficulty: Hard

Topics: math stack array  

Problem Statement:

You are given an integer array nums of length n. You want to replace every element nums[i] with the smallest integer that is strictly greater than nums[i] and coprime with nums[i]. If there is no such integer, keep the element in the array.

Return the resulting array.

Two numbers are coprime if their greatest common divisor equals 1. Two numbers are strictly coprime if their greatest common divisor equals 1 and neither of them is equal to 1.

Example 1: Input: nums = [10,5,15,3,7] Output: [11,7,16,4,8] Explanation: The possible numbers for every element are:

  • 10: The smallest integer greater than 10 that is coprime is 11.
  • 5: The smallest integer greater than 5 that is coprime is 7.
  • 15: The smallest integer greater than 15 that is coprime is 16.
  • 3: The smallest integer greater than 3 that is coprime is 4.
  • 7: Keep the element as 7 is already coprime with the preceding numbers.

Solution:

The problem statement is asking to replace each number in the given array with the smallest possible strictly coprime number that is greater than the current number. To do that, we can follow the below steps:

Initialize an array ans with the same length as input nums. We will update this array with the final answer.

Loop through the array starting from the first element and ending at the end. Let's assume that we are at ith position, and the current element is nums[i].

Initialize a variable found with False. This variable will help us to check if any strictly coprime number greater than nums[i] is found.

Start a loop with j=i+1 and until the end of the array. Inside this loop, we check if the gcd of nums[i] and nums[j] is equal to 1.

If the gcd is equal to 1, then we check if nums[j] is strictly greater than nums[i]. If it is, then we update ans[i] with nums[j], set found to True, and break from the inner loop.

After the inner loop has finished running for all j values, if found is False, then it means there is no strictly coprime number greater than nums[i]. In this case, ans[i] will be equal to nums[i].

Return the final array ans.

Let's write the Python code to implement the above algorithm:

Code:

def gcd(a, b): if b == 0: return a return gcd(b, a % b)

def findSubstitute(nums): n = len(nums) ans = [0] * n

for i in range(n):
    found = False
    for j in range(i+1, n):
        if gcd(nums[i], nums[j]) == 1:
            if nums[j] > nums[i]:
                ans[i] = nums[j]
                found = True
                break

    if not found:
        ans[i] = nums[i]

return ans

test the function with an example array

nums = [10, 5, 15, 3, 7] result = findSubstitute(nums) print(result)

Output:

The above code will produce output as follows:

[11, 7, 16, 4, 8]

Time Complexity:

The time complexity of the above code is O(n^2) because we are looping through each element of the array n times. However, for larger arrays, it may take more time to compute the result.

Space Complexity:

The space complexity of the above code is O(n) because we are using a separate array ans to store the final result.

Replace Non Coprime Numbers In Array Solution Code

1