LeetCode 2601: Prime Subtraction Operation Solution

Master LeetCode problem 2601 (Prime Subtraction Operation), a medium challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

2601. Prime Subtraction Operation

Problem Explanation

Explanation

To solve this problem, we can iterate through the array nums and for each element, we need to check if we can make it strictly increasing by using the given operation. To do this, we can pick a prime number less than the current element and subtract it from the current element. If we are able to do this for all elements and make the array strictly increasing, we return true. Otherwise, we return false.

We can create a helper function to check if a number is prime. Then, for each element in the array, we iterate through prime numbers less than the element and try subtracting each prime number to see if we can make the array strictly increasing.

Time Complexity:

  • The time complexity of this approach is O(n * sqrt(max_element)), where n is the number of elements in the array and max_element is the maximum element in the array.

Space Complexity:

  • The space complexity of this approach is O(1) as we are not using any extra space except for a few variables.

Solution Code

class Solution {
    public boolean canMakeIncreasing(int[] nums) {
        for (int i = 0; i < nums.length; i++) {
            int num = nums[i];
            boolean isIncreasing = false;
            for (int j = 2; j < num; j++) {
                if (isPrime(j) && num - j > 0) {
                    nums[i] = num - j;
                    isIncreasing = true;
                    break;
                }
            }
            if (!isIncreasing) {
                return false;
            }
        }
        return true;
    }
    
    private boolean isPrime(int num) {
        if (num <= 1) {
            return false;
        }
        for (int i = 2; i <= Math.sqrt(num); i++) {
            if (num % i == 0) {
                return false;
            }
        }
        return true;
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 2601 (Prime Subtraction Operation)?

This page provides optimized solutions for LeetCode problem 2601 (Prime Subtraction Operation) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 2601 (Prime Subtraction Operation)?

The time complexity for LeetCode 2601 (Prime Subtraction Operation) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 2601 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 2601 in Java, C++, or Python.

Back to LeetCode Solutions