Minimum Division Operations to Make Array Non Decreasing

Problem

class Solution {
//one thing to note here is we have to get the values in increasing order,
//the current values of nums[i] is the max value it can have after which it can only get a lower value.
//start from the second last valu…


This content originally appeared on DEV Community and was authored by Prashant Mishra

Problem

class Solution {
    //one thing to note here is we have to get the values in increasing order,
    //the current values of nums[i] is the max value it can have after which it can only get a lower value.
    //start from the second last value in nums[] because last values is already the largest it can be

    public int minOperations(int[] nums) {
        int count = 0;
        //compare value at i-1th index with value at i, index if it is greater, update the value at i-1th index with its greatest divisor, if you get 1 as greatest divisor return -1;
        for (int i = nums.length-1; i >0; i--) {
            while (nums[i] < nums[i-1]) {
                int g = gd(nums[i-1]);
                if(g ==1){
                    return -1;
                }
                nums[i-1] = g;
                count++;
            }
        }
        return count;
    }
    // it will give the greatest possible divisor else 1
     public int gd(int n) {
        int d = 1;
        for (int i = 2; i<=Math.sqrt(n); i++) {
            if (n % i == 0) {
                return i;
            }
        }
        return d; 
    }
}


This content originally appeared on DEV Community and was authored by Prashant Mishra


Print Share Comment Cite Upload Translate Updates
APA

Prashant Mishra | Sciencx (2024-10-20T04:18:34+00:00) Minimum Division Operations to Make Array Non Decreasing. Retrieved from https://www.scien.cx/2024/10/20/minimum-division-operations-to-make-array-non-decreasing/

MLA
" » Minimum Division Operations to Make Array Non Decreasing." Prashant Mishra | Sciencx - Sunday October 20, 2024, https://www.scien.cx/2024/10/20/minimum-division-operations-to-make-array-non-decreasing/
HARVARD
Prashant Mishra | Sciencx Sunday October 20, 2024 » Minimum Division Operations to Make Array Non Decreasing., viewed ,<https://www.scien.cx/2024/10/20/minimum-division-operations-to-make-array-non-decreasing/>
VANCOUVER
Prashant Mishra | Sciencx - » Minimum Division Operations to Make Array Non Decreasing. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/10/20/minimum-division-operations-to-make-array-non-decreasing/
CHICAGO
" » Minimum Division Operations to Make Array Non Decreasing." Prashant Mishra | Sciencx - Accessed . https://www.scien.cx/2024/10/20/minimum-division-operations-to-make-array-non-decreasing/
IEEE
" » Minimum Division Operations to Make Array Non Decreasing." Prashant Mishra | Sciencx [Online]. Available: https://www.scien.cx/2024/10/20/minimum-division-operations-to-make-array-non-decreasing/. [Accessed: ]
rf:citation
» Minimum Division Operations to Make Array Non Decreasing | Prashant Mishra | Sciencx | https://www.scien.cx/2024/10/20/minimum-division-operations-to-make-array-non-decreasing/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.