Logo

dev-resources.site

for different kinds of informations.

Minimum Division Operations to Make Array Non Decreasing

Published at
10/20/2024
Categories
leetcode
contest
420
Author
prashantrmishra
Categories
3 categories in total
leetcode
open
contest
open
420
open
Author
15 person written this
prashantrmishra
open
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 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; 
    }
}
Enter fullscreen mode Exit fullscreen mode
contest Article's
30 articles in total
Favicon
Minimum Division Operations to Make Array Non Decreasing
Favicon
Count Substrings With K-Frequency Characters I
Favicon
Cybersecurity Quiz Alert!
Favicon
Maximum Possible Number by Binary Concatenation
Favicon
Maximum Multiplication Score
Favicon
Find the Sequence of Strings Appeared on the Screen
Favicon
Remove Methods from project
Favicon
count-of-substrings-containing-every-vowel-and-k-consonants-i
Favicon
Participate in the SecureChain AI Thread Contest for a Chance to Win
Favicon
India DevOps League 2024
Favicon
প্রোগ্রামিং প্রতিযোগিতার নানান ধরন ।
Favicon
🎉 iPhone 15 Pro Max Giveaway! 🎉
Favicon
The brand new gaming contest is here, introducing the #GlobalGamers Challenge to build games with Flutter!
Favicon
Announcing the Brainstory Holiday Giveaway Contest: Unleash Your Creativity and Win Big! 🧠🎁
Favicon
Join the MiniScript "Advent of Code" contest!
Favicon
Announcing the Directus Panel Quest Hackathon
Favicon
InterSystems Python Programming Contest 2023
Favicon
🏆 Contest - Kintone Customization Contest 2023
Favicon
Hi DEV fellows 🥳❗
Favicon
I made a programming animation; guess the language to win art!
Favicon
InterSystems Developer Tools Contest
Favicon
InterSystems IRIS for Health Contest: FHIR for Women's Health
Favicon
InterSystems Interoperability Contest: Building Sustainable Solutions
Favicon
Contest: Share a Good Plan
Favicon
InterSystems Full Stack Contest 2022
Favicon
InterSystems Grand Prix Programming Contest 2022
Favicon
InterSystems Globals Contest
Favicon
Solving the Hash Code 2022 Practice Challenge with 70 lines of code
Favicon
Hasura Tips & Tricks Contest
Favicon
InterSystems Datasets Contest

Featured ones: