对象已移动

可在此处找到该文档 Minimize operations to make all the elements of given Subarrays distinct – 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

Minimize operations to make all the elements of given Subarrays distinct

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


Given an arr[] of optimistic integers and begin[] and finish[] arrays of size L, Which comprises the beginning and finish indexes of L variety of non-intersecting sub-arrays as a begin[i] and finish[i]  for all (1 ≤ i ≤ L). An operation is outlined as under: 

  • Choose an ordered steady or non – steady a part of the sub-array after which add an integer let’s say A to the entire parts of the chosen half.

Then, the duty is to output the minimal quantity of given operations required to make all the weather of all given non – intersecting sub-arrays distinct.

Notice: If a couple of sub-arrays comprises the identical aspect, It doesn’t matter. Simply every sub-array ought to comprise distinct parts in itself.

Examples:

Enter: arr[] = {2, 2, 1, 2, 3}, begin[] = {1, 3}, finish[] = {2, 5}               
Output: 1
Rationalization: First sub-array : [start[1], finish[1]] = {2, 2}.
Second sub-array : [start[3], finish[5]] = {1, 2, 3}.
In First sub-array selected sub-sequence from index 1 to 1 as {2} and plus any integer random integer let say 1.Then, First sub-array = {3, 2}. Now, each sub-array comprises distinct parts in itself. Whole variety of required operation are 1.

Enter: arr[] = {1, 2, 3, 3, 4, 5}, begin[] = {1, 4}, finish[] = {3, 6}
Output: 0
Rationalization: It may be verified that sub-arrays {1, 2, 3} and {3, 4, 5} each are distinct in itself. Subsequently, whole variety of required operations are 0.

Method: Implement the thought under to resolve the issue

Making all the weather distinct in a sub-array by given operation solely relies upon upon the utmost frequency in an sub-array, If we transformed excessive frequent aspect in distinct type then remainder of the frequencies lower than max frequency might be make distinct concurrently. For extra readability see the  idea of strategy.Receive the utmost frequency in every sub-array and apply the algorithm offered under.

Idea of strategy:

Suppose our sub-array A[] is = {2, 2, 2, 2, 2, 2, 2, 2, 2, 2}. Highest frequency is 10 right here.

First operation: Selected ordered sub-sequence from index 6 to 10 and add any integer let say 1 to all parts in sub-sequence. Then,   A[] = {2, 2, 2, 2, 2, 3, 3, 3, 3, 3}. Highest frequency is 5 until right here. 

Second Operation: Selected ordered sub-sequence from index 3 to five and eight to 10 add any integer let say 2 to all parts in sub-sequence. Then,  A[] = {2, 2, 4, 4, 4, 3, 3, 5, 5, 5}. Highest frequency is 3 until right here.

Third Operation: Selected ordered sub-sequence from index 4 to five and 9 to 10 add any integer let say 3 to all parts in sub-sequence. Then,  A[] = {2, 2, 4, 7, 7, 3, 3, 5, 8, 8}

Fourth Operation: Selected ordered sub-sequence of indices : {1, 4, 6, 9}  add any integer let say 10 to all parts in sub-sequence. Then,  A[] = {12, 2, 4, 17, 7, 13, 3, 5, 18, 8}

Thus, Solely 4 operation are required to transform into distinct parts. At second operation we are able to see the each 2 and three aspect has 5 frequency and we efficiently convert them into distinct parts. This provides us concept that If we attempt to make max frequent aspect distinct, Then remainder of the weather having frequency lower than or equal to max frequency might be transformed into distinct concurrently.    

In above instance, It may be clearly seen that, At every operation we’re changing the precisely half of the parts, If worth of max_frequency is even at that present operation in any other case we’re changing (max_frequency+1)/2 parts in addition to we’re decreasing the worth of max_frequency in the identical method.Thus we are able to conclude the under offered algorithm to resolve the issue.

Algorithm to resolve the issue:

      1. Create a variable let’s say min_operations to retailer whole variety of minimal operations required for all              sub-arrays.

      2. For Every given sub-array observe the steps under:

  •  Depend frequency of every aspect and retailer them in Hash-Map.
  • Traverse the Hash-Map to acquire most frequency in a variable let’s say max_frequency.
  • Create and initialize counter variable to zero.
  • Apply the under algorithm for acquiring minimal variety of operations required.   

            whereas(max_frequency > 1)
              {
                      if (isEven(max_frequency))
                       {
                             max_frequency/=2;
                        }
                      else
                       {
                             max_frequency = (max_frequency+1)/2;
                       }
                      counter++;
             }

  • On finishing the loop add worth of counter variable in min_operations.

      3. After apply above algorithm on every given sub-array print the worth of min_operations.  

Beneath is the code to implement the strategy:

Java

  

import java.io.*;

import java.lang.*;

import java.util.*;

  

class GFG {

    

    public static void essential(String args[])

    {

  

        

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

  

        

        

        int[] begin = { 1, 6 };

  

        

        

        int[] finish = { 5, 10 };

  

        

        

        int min_operations = 0;

  

        

        

        for (int i = 0; i < begin.size; i++) {

  

            

            int start_index = begin[i];

  

            

            int end_index = finish[i];

  

            

            

            HashMap<Integer, Integer> map = new HashMap<>();

  

            

            

            for (int j = start_index - 1; j < end_index;

                 j++) {

  

                

                

                map.put(arr[j], map.get(arr[j]) == null

                                    ? 1

                                    : map.get(arr[j]) + 1);

            }

  

            

            

            int max_frequency = 0;

  

            

            for (Map.Entry<Integer, Integer> set :

                 map.entrySet()) {

  

                

                

                max_frequency

                    = set.getValue() > max_frequency

                          ? set.getValue()

                          : max_frequency;

            }

  

            

            

            whereas (max_frequency > 1) {

  

                

                max_frequency

                    = max_frequency % 2 == 0

                          ? max_frequency / 2

                          : (max_frequency + 1) / 2;

  

                

                

                min_operations++;

            }

        }

  

        

        

        System.out.println(min_operations);

    }

}

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



Source link

Tags: DistinctElementsMinimizeOperationsSubarrays
Previous Post

Olivia Wilde and Harry Styles ‘thriving’ amid former nanny’s claims

Next Post

Zuma says Ramaphosa has suspended the Public Protector while she is investigating him

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
Zuma says Ramaphosa has suspended the Public Protector while she is investigating him

Zuma says Ramaphosa has suspended the Public Protector while she is investigating him

Anthropologie Sale: Get a $68 Top for Just $6 & More 91% Off Deals

Anthropologie Sale: Get a $68 Top for Just $6 & More 91% Off Deals

  • 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
8BitDo Retro Mechanical Keyboard C64 Review

8BitDo Retro Mechanical Keyboard C64 Review

March 24, 2025
I Tried Calocurb For 90 Days. Here’s My Review.

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

January 8, 2025
The Best Madras Shirt Brands For Men: Summer 2021 Edition

The Best Madras Shirt Brands For Men: Summer 2021 Edition

July 20, 2021
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
Guide for Bagisto Quick Commerce

Guide for Bagisto Quick Commerce

October 16, 2024
Bones: All Of Brennan’s Interns, Ranked

Bones: All Of Brennan’s Interns, Ranked

June 15, 2021
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
Looking back on the early days of LGBTQ2 rock – National

Looking back on the early days of LGBTQ2 rock – National

June 23, 2025
Lesser-Known Movie Facts You Might Not Know

Lesser-Known Movie Facts You Might Not Know

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

  • Maroon 5’s New Album ‘Love Is Like’ Release Date, Tour Dates Announced
  • Google adds AI features to Chromebook Plus devices
  • Generations: The Legacy Teasers on SABC1 June
  • 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