对象已移动

可在此处找到该文档 Minimum operations to make Array elements 0 by decrementing pair or single element – New Self New Life
New Self New Life
No Result
View All Result
  • Home
  • Entertainment
  • Celebrity
  • Cinema
  • Music
  • Digital Lifestyle
  • Social Media
  • Softwares
  • Devices
  • Home
  • Entertainment
  • Celebrity
  • Cinema
  • Music
  • Digital Lifestyle
  • Social Media
  • Softwares
  • Devices
New Self New Life
No Result
View All Result
Home Softwares

Minimum operations to make Array elements 0 by decrementing pair or single element

by admin
3 years ago
in Softwares
Best Coding Practices For Rest API Design
Share on FacebookShare on Twitter


View Dialogue

Enhance Article

Save Article

Like Article

Given an array arr[] of dimension N, the duty is to search out the minimal variety of operations required to scale back all three parts of the array to zero. Following operations are allowed:

  • Scale back 2 totally different array parts by one.
  • Scale back a single array ingredient by one.

Instance:

Enter: arr[] = {1, 2, 3}, N = 3
Output: 3
Rationalization : Operation 1: scale back 3 and a couple of to get {1, 1, 2}
Operation 2: reduuce 1 and a couple of to get {1, 0, 1}
Operation 3: scale back each 1s to get {0, 0, 0}

Enter: arr[] = {5, 1, 2, 9, 8}, N = 5
Output: 13

 

Method:

This drawback might be solved utilizing grasping method. The thought is to scale back the two largest parts at a time or (if not doable) 1 at a time.  As we want the most important parts in every step, we are able to use a max heap.

The next steps might be taken to unravel this method:

  • Provoke a rely variable as 0.
  • Insert all the weather in a max heap.
    • Scale back the 2 largest parts.
    • Insert the diminished values once more into the heap.
  • Repeat above talked about steps till all array parts turn out to be zero and improve the rely at every iteration.
  • Cease when all array parts are zero.

Beneath is the implementation of the above method:

Java

import java.util.*;

  

class GFG {

  

    public static int reduceArray(int arr[], int N)

    {

  

        int rely = 0;

        PriorityQueue<Integer> pq = new PriorityQueue<>();

  

        for (int i = 0; i < N; i++) {

            pq.add(arr[i] * -1);

        }

        whereas (pq.dimension() > 1) {

            int temp1 = pq.ballot();

            int temp2 = pq.ballot();

            rely++;

            temp1++;

            temp2++;

            if (temp1 != 0)

                pq.add(temp1);

            if (temp2 != 0)

                pq.add(temp2);

        }

        if (pq.dimension() > 0)

            rely -= pq.ballot();

        return rely;

    }

  

    

    public static void important(String[] args)

    {

        int arr[] = { 1, 2, 3 };

        int N = 3;

        int rely = reduceArray(arr, N);

        System.out.println(rely);

    }

}

Time Complexity: O(N * logN)
Auxiliary Area: O(N)



Source link

Tags: arraydecrementingElementElementsMinimumOperationspairSingle
Previous Post

Kanye West: Rapper’s biggest feuds through the years | Drake, Pete Davidson, Peppa Pig and more

Next Post

The Best Heavyweight T-Shirt Brands For Men In 2022

Related Posts

Best AI Agents Development Companies in 2025
Softwares

Best AI Agents Development Companies in 2025

by admin
July 28, 2025
Minor update(3) for Vivaldi Android Browser 7.5
Softwares

Minor update(3) for Vivaldi Android Browser 7.5

by admin
July 27, 2025
User Guide For 360 Degree Product Image For Wix
Softwares

User Guide For 360 Degree Product Image For Wix

by admin
July 24, 2025
New open-source tool makes complex data understandable
Softwares

New open-source tool makes complex data understandable

by admin
July 25, 2025
BrowserStack launches Figma plugin for detecting accessibility issues in design phase
Softwares

BrowserStack launches Figma plugin for detecting accessibility issues in design phase

by admin
July 22, 2025
Next Post
The Best Heavyweight T-Shirt Brands For Men In 2022

The Best Heavyweight T-Shirt Brands For Men In 2022

House of the Dragon’s Matt Smith Wonders if the Show Has Too Many Sex Scenes

House of the Dragon's Matt Smith Wonders if the Show Has Too Many Sex Scenes

  • Trending
  • Comments
  • Latest
Critics And Fans Disagree On Netflix’s Controversial Fantasy Show With Near-Perfect RT Score

Critics And Fans Disagree On Netflix’s Controversial Fantasy Show With Near-Perfect RT Score

July 5, 2025
How well did you know Ozzy? Take this quiz – National

How well did you know Ozzy? Take this quiz – National

July 28, 2025
Why unFTP, how to run, embed or extend with Rust

Why unFTP, how to run, embed or extend with Rust

June 22, 2021
Sunny Pawar now: What happened to the Lion actor and what is he doing now? | Explainer

Sunny Pawar now: What happened to the Lion actor and what is he doing now? | Explainer

November 30, 2024
I Tried Calocurb For 90 Days. Here’s My Review.

I Tried Calocurb For 90 Days. Here’s My Review.

January 8, 2025
10 Best Netflix Original Thriller Shows, Ranked

10 Best Netflix Original Thriller Shows, Ranked

June 22, 2025
The US Government Issues New Directives on AI Development

The US Government Issues New Directives on AI Development

July 24, 2025
Nothing helped his eczema, so this S’porean invented his own fix

Nothing helped his eczema, so this S’porean invented his own fix

July 25, 2025
Hardwell Reveals Avicii Asked Him to Collaborate On "Bromance"

Hardwell Reveals Avicii Asked Him to Collaborate On "Bromance"

July 29, 2025
Flowtica Scribe AI Pen : An AI Assistant in Your Pocket

Flowtica Scribe AI Pen : An AI Assistant in Your Pocket

July 29, 2025
Spotify Stock Dips On Q2 Earnings Miss, Focus On Ads Business

Spotify Stock Dips On Q2 Earnings Miss, Focus On Ads Business

July 29, 2025
Robin Williams’s Daughter On AI, Matthew Lawrence Backlash

Robin Williams’s Daughter On AI, Matthew Lawrence Backlash

July 29, 2025
How to Maximize Your Content on Both Sites

How to Maximize Your Content on Both Sites

July 29, 2025
July 25-27 Box Office Recap – ‘The Fantastic Four: First Steps’ opens with a fantastic $117.6M domestically. But it disappoints overseas, earning just $99M. Worldwide, ‘Jurassic World Rebirth’ crosses $700M, ‘How to Train Your Dragon’ crosses $600M, while ‘F1’ and ‘Superman’ cross $500M.

July 25-27 Box Office Recap – ‘The Fantastic Four: First Steps’ opens with a fantastic $117.6M domestically. But it disappoints overseas, earning just $99M. Worldwide, ‘Jurassic World Rebirth’ crosses $700M, ‘How to Train Your Dragon’ crosses $600M, while ‘F1’ and ‘Superman’ cross $500M.

July 29, 2025
How a Soundtrack Reunited Fleetwood Mac for ‘Tango in the Night’

How a Soundtrack Reunited Fleetwood Mac for ‘Tango in the Night’

July 28, 2025
Best AI Agents Development Companies in 2025

Best AI Agents Development Companies in 2025

July 28, 2025
New Self New Life

Your source for entertainment news, celebrities, celebrity news, and Music, Cinema, Digital Lifestyle and Social Media and More !

Categories

  • Celebrity
  • Cinema
  • Devices
  • Digital Lifestyle
  • Entertainment
  • Music
  • Social Media
  • Softwares
  • Uncategorized

Recent Posts

  • Hardwell Reveals Avicii Asked Him to Collaborate On "Bromance"
  • Flowtica Scribe AI Pen : An AI Assistant in Your Pocket
  • Spotify Stock Dips On Q2 Earnings Miss, Focus On Ads Business
  • Home
  • Disclaimer
  • DMCA
  • Privacy Policy
  • Cookie Privacy Policy
  • Terms and Conditions
  • Contact us

Copyright © 2021 New Self New Life.
New Self New Life is not responsible for the content of external sites. slotsfree  creator solana token

No Result
View All Result
  • Home
  • Entertainment
  • Celebrity
  • Cinema
  • Music
  • Digital Lifestyle
  • Social Media
  • Softwares
  • Devices

Copyright © 2021 New Self New Life.
New Self New Life is not responsible for the content of external sites.

New Self New Life