对象已移动

可在此处找到该文档 Minimum replacements with 0 to sort the array – 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 replacements with 0 to sort the array

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


Given an array A[] of N integers, the duty is to search out the minimal variety of operations to type the array in non-decreasing order, by selecting an integer X and changing all of the occurrences of X within the array with 0.

Examples:

Enter: N = 5, A[] = {2, 2, 1, 1, 3}
Output: 1
Rationalization: We select X = 2 and substitute all of the occurrences of two with 0. Now the array turns into {2, 2, 1, 1, 3} -> {0, 0, 1, 1, 3} , which is sorted in growing order.

Enter: N = 4, A[] = {2, 4, 1, 2}
Output: 3

 

Method: The issue will be solved simply with the assistance of a Map. 

Observations:

There are 2 instances that must be thought of :

  • Case 1: Similar factor happens greater than as soon as non-contiguously 
    • Contemplate the array : {1,6,3,4,5,3,2}. 
    • Now, since 3 at index 5 is larger than its subsequent factor, so we are going to make that 0 (in addition to 3 at index 2). 
    • The array turns into {1,6,0,4,5,0,2}. 
    • So, the one strategy to type the array could be to make all the weather earlier than the zeroes equal to 0. i.e. the array turns into {0,0,0,0,0,0,2}.
  • Case 2: Aspect at ith index is larger than the factor at (i+1)th index :
    • Contemplate the array : {1,2,3,5,4}. 
    • Because the factor on the third index is larger than the factor at 4th index, we now have to make the factor at third index equal to zero. 
    • So , the array turns into {1,2,3,0,4}. 
    • Now, the one strategy to type the array could be to make all the weather earlier than the zero equal to 0. i.e. the array turns into {0,0,0,0,4}.

It may be noticed that ultimately Case 2 breaks all the way down to Case 1.

Contemplating the above instances, the issue will be solved following the beneath steps :

  • Declare a hash map and add the frequency of every factor of the array into the map.
  • Iterate by the array from the again, i.e. from i=N-1 to i=0.
  • At every iteration, deal with Instances 1 and a couple of as defined above.
  • If iteration completes, return 0.

Beneath is the implementation of this method:

C++

#embrace <bits/stdc++.h>

utilizing namespace std;

 

int minimumReplacements(int A[], int N)

{

    

    map<int, int> mp;

 

    

    

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

        mp[A[i]]++;

    }

 

    

    for (int i = N - 1; i >= 0; i--) {

 

        

        

        whereas (i > 0 && A[i] == A[i - 1]) {

            mp[A[i]]--;

            i--;

        }

 

        mp[A[i]]--;

 

        

        

        if (mp[A[i]] == 0) {

            mp.erase(A[i]);

        }

 

        

        if (mp.discover(A[i]) != mp.finish()) {

            return mp.measurement();

        }

 

        

        if (i > 0 && A[i - 1] > A[i]) {

            return mp.measurement();

        }

    }

 

    

    

    return 0;

}

 

int fundamental()

{

    int N = 5;

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

 

    

    int reply = minimumReplacements(A, N);

    cout << reply;

}

Time Complexity: O(N)
Auxiliary House: O(N) 



Source link

Tags: arrayMinimumreplacementsSort
Previous Post

Adonit’s $45 Stylus Magnetically Charges Off the iPad

Next Post

Dan Edlebeck, Co-Founder of SEI: Introducing Sei Network and Its Best-In-Class On-chain Orderbook

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
Dan Edlebeck, Co-Founder of SEI: Introducing Sei Network and Its Best-In-Class On-chain Orderbook

Dan Edlebeck, Co-Founder of SEI: Introducing Sei Network and Its Best-In-Class On-chain Orderbook

New FDA Ruling To Boost Hearing Aid Quality And Affordability

New FDA Ruling To Boost Hearing Aid Quality And Affordability

  • 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