Decrease operations to make all the weather of given Subarrays distinct
[ad_1]
Given an arr[] of constructive integers and begin[] and finish[] arrays of size L, Which incorporates the beginning and finish indexes of L variety of nonintersecting subarrays as a begin[i] and finish[i] for all (1 ≤ i ≤ L). An operation is outlined as beneath:
 Choose an ordered steady or non – steady a part of the subarray after which add an integer let’s say A to all the 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 subarrays distinct.
Observe: If a couple of subarrays incorporates the identical component, It doesn’t matter. Simply every subarray ought to include distinct parts in itself.
Examples:
Enter: arr[] = {2, 2, 1, 2, 3}, begin[] = {1, 3}, finish[] = {2, 5}
Output: 1
Clarification: First subarray : [start[1], finish[1]] = {2, 2}.
Second subarray : [start[3], finish[5]] = {1, 2, 3}.
In First subarray selected subsequence from index 1 to 1 as {2} and plus any integer random integer let say 1.Then, First subarray = {3, 2}. Now, each subarray incorporates distinct parts in itself. Complete variety of required operation are 1.Enter: arr[] = {1, 2, 3, 3, 4, 5}, begin[] = {1, 4}, finish[] = {3, 6}
Output: 0
Clarification: It may be verified that subarrays {1, 2, 3} and {3, 4, 5} each are distinct in itself. Due to this fact, whole variety of required operations are 0.
Strategy: Implement the concept beneath to unravel the issue
Making all the weather distinct in a subarray by given operation solely relies upon upon the utmost frequency in an subarray, If we transformed excessive frequent component in distinct type then remainder of the frequencies lower than max frequency could be make distinct concurrently. For extra readability see the idea of method.Acquire the utmost frequency in every subarray and apply the algorithm offered beneath.
Idea of method:
Suppose our subarray A[] is = {2, 2, 2, 2, 2, 2, 2, 2, 2, 2}. Highest frequency is 10 right here.
First operation: Selected ordered subsequence from index 6 to 10 and add any integer let say 1 to all parts in subsequence. Then, A[] = {2, 2, 2, 2, 2, 3, 3, 3, 3, 3}. Highest frequency is 5 until right here.
Second Operation: Selected ordered subsequence from index 3 to five and eight to 10 add any integer let say 2 to all parts in subsequence. Then, A[] = {2, 2, 4, 4, 4, 3, 3, 5, 5, 5}. Highest frequency is 3 until right here.
Third Operation: Selected ordered subsequence from index 4 to five and 9 to 10 add any integer let say 3 to all parts in subsequence. Then, A[] = {2, 2, 4, 7, 7, 3, 3, 5, 8, 8}
Fourth Operation: Selected ordered subsequence of indices : {1, 4, 6, 9} add any integer let say 10 to all parts in subsequence. 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 component has 5 frequency and we efficiently convert them into distinct parts. This offers us concept that If we attempt to make max frequent component distinct, Then remainder of the weather having frequency lower than or equal to max frequency could 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 beneath offered algorithm to unravel the issue.
Algorithm to unravel the issue:
1. Create a variable let’s say min_operations to retailer whole variety of minimal operations required for all subarrays.
2. For Every given subarray comply with the steps beneath:
 Rely frequency of every component and retailer them in HashMap.
 Traverse the HashMap to acquire most frequency in a variable let’s say max_frequency.
 Create and initialize counter variable to zero.
 Apply the beneath 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 subarray print the worth of min_operations.
Under is the code to implement the method:
Java

Time Complexity: O(N)
Auxiliary Area: O(N)
[ad_2]