对象已移动

可在此处找到该文档 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

Minor update(4) for Vivaldi Android Browser 7.4
Softwares

Minor update(4) for Vivaldi Android Browser 7.4

by admin
June 21, 2025
How AI Medical Coding Software Reduces Errors & Accelerates Billing in 2025
Softwares

How AI Medical Coding Software Reduces Errors & Accelerates Billing in 2025

by admin
June 22, 2025
10+ Best Free Portfolio & Lookbook Templates for InDesign in 2025 — Speckyboy
Softwares

10+ Best Free Portfolio & Lookbook Templates for InDesign in 2025 — Speckyboy

by admin
June 20, 2025
User Guide For CS-Cart Product Search By Barcode
Softwares

User Guide For CS-Cart Product Search By Barcode

by admin
June 18, 2025
Open Talent platforms emerging to match skilled workers to needs, study finds
Softwares

Open Talent platforms emerging to match skilled workers to needs, study finds

by admin
June 16, 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
Pamela Anderson raves about new natural, makeup-free look: ‘It’s freedom’

Pamela Anderson raves about new natural, makeup-free look: ‘It’s freedom’

October 8, 2023
Alec Baldwin indicted again for ‘Rust’ shooting that left cinematographer dead – National

Alec Baldwin indicted again for ‘Rust’ shooting that left cinematographer dead – National

January 21, 2024
I Tried Calocurb For 90 Days. Here’s My Review.

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

January 8, 2025
Guide for Bagisto Quick Commerce

Guide for Bagisto Quick Commerce

October 16, 2024
A look into CAMPUS, ShopBack’s new Singapore HQ at Pasir Panjang

A look into CAMPUS, ShopBack’s new Singapore HQ at Pasir Panjang

July 2, 2022
8BitDo Retro Mechanical Keyboard C64 Review

8BitDo Retro Mechanical Keyboard C64 Review

March 24, 2025
Bones: All Of Brennan’s Interns, Ranked

Bones: All Of Brennan’s Interns, Ranked

June 15, 2021
Gabby Logan shares emotional family update in rare post with daughter Lois

Gabby Logan shares emotional family update in rare post with daughter Lois

September 25, 2024
Cobra Kai star Martin Kove ‘kicked out of fan convention’ after allegedly ‘biting’ co-star Alicia Hannah-Kim

Cobra Kai star Martin Kove ‘kicked out of fan convention’ after allegedly ‘biting’ co-star Alicia Hannah-Kim

June 24, 2025
Liam Neeson Drives a Bus to Escape Himalayan Kidnappers in Trailer For ICE ROAD: VENGEANCE — GeekTyrant

Liam Neeson Drives a Bus to Escape Himalayan Kidnappers in Trailer For ICE ROAD: VENGEANCE — GeekTyrant

June 24, 2025
Maroon 5’s New Album ‘Love Is Like’ Release Date, Tour Dates Announced

Maroon 5’s New Album ‘Love Is Like’ Release Date, Tour Dates Announced

June 23, 2025
Google adds AI features to Chromebook Plus devices

Google adds AI features to Chromebook Plus devices

June 23, 2025
Generations: The Legacy Teasers on SABC1 June

Generations: The Legacy Teasers on SABC1 June

June 23, 2025
‘Elio’ Had Pixar’s Worst Box Office Opening Weekend Ever

‘Elio’ Had Pixar’s Worst Box Office Opening Weekend Ever

June 23, 2025
Love Island Season 7 Recap: Week 3 Twists Amid Casa Amor (Updating Daily)

Love Island Season 7 Recap: Week 3 Twists Amid Casa Amor (Updating Daily)

June 23, 2025
Is ChatGPT Catching Google on Search Activity? [Infographic]

Is ChatGPT Catching Google on Search Activity? [Infographic]

June 23, 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

  • Cobra Kai star Martin Kove ‘kicked out of fan convention’ after allegedly ‘biting’ co-star Alicia Hannah-Kim
  • Liam Neeson Drives a Bus to Escape Himalayan Kidnappers in Trailer For ICE ROAD: VENGEANCE — GeekTyrant
  • Maroon 5’s New Album ‘Love Is Like’ Release Date, Tour Dates Announced
  • 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