Given a string consisting of only 0’s and 1’s. Now you are given N non-intersecting ranges L, R ( L <= R), more specifically [L1, R1], [L2, R2], …, [LN, RN], No two of these intervals overlap — formally, for each valid i, j such that i!=j, either Ri<Lj or Rj<Li.
The task is to find a valid permutation which will hold two following conditions simultaneously:
- Sum of numbers between all N given ranges will be maximum.
- The string will be lexicographically largest. A string 1100 is lexicographically larger than string 1001.
First we put 1’s in position 2 and 3 then in 5 as
there are no 1’s left, the string formed is 01101.
In the above example we 1st put 1 in 1st and 2nd position then we have another ‘1’ left,
So, we use it to maximize the string lexicographically and we put it in the 3rd position and thus the rearrangement is complete.
- First priority is given to make the count of 1’s between all l and r to be max. We count the number of 1’s in the array and store in a variable.
- After taking input we update the range of each l and r by 1 to just mark the position to be filled with 1 first.
- Then, we take prefix sum of the array so that we get the positions where to fix the 1’s first. Then we run a loop in that prefix sum array from left. If we get any position with value greater than 1 that means we have a l-r in that index. We continue to put 1’s in those indices until the count of 1 becomes zero.
- Now after the maximization operation is finished and if there are some 1’s left then we start the lexicographic maximization. We again start a loop from left of the prefix sum array if we find an index having value 0 which indicates that there is no l-r having that index then we put a 1 in that index and thus continue until all remaining 1’s are filled.
Below is the implementation of the above approach:
- Queries for elements having values within the range A to B in the given index range using Segment Tree
- Number of ways to arrange N numbers which are in a range from 1 to K under given constraints.
- Count maximum occurrence of subsequence in string such that indices in subsequence is in A.P.
- Remove one bit from a binary number to get maximum value
- Queries for elements having values within the range A to B using MO's Algorithm
- Queries to check if any non-repeating element exists within range [L, R] of an Array
- Rearrange two given arrays such that sum of same indexed elements lies within given range
- Count number of indices such that s[i] = s[i+1] : Range queries
- Range sum queries for anticlockwise rotations of Array by K indices
- Queries to count frequencies of a given character in a given range of indices
- Arrange first N natural numbers such that absolute difference between all adjacent elements > 1
- Number of ways to arrange K different objects taking N objects at a time
- Arrange the numbers in the Array as per given inequalities
- Arrange the array such that upon performing given operations an increasing order is obtained
- Count of maximum occurring subsequence using only those characters whose indices are in GP
- Maximum XOR value of maximum and second maximum element among all possible subarrays
- Queries to find frequencies of a string within specified substrings
- Convert Hexadecimal value String to ASCII value String
- Find the starting indices of the substrings in string (S) which is made by concatenating all words from a list(L)
- Find indices of all occurrence of one string in other
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.