Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
[ad_1]
Given a binary string S and an array arr[] every of dimension N, we are able to decide any ingredient from the Array which is to the left of (or on the similar place) the indices of ‘1’s within the given binary string. The duty is to search out the utmost doable sum.
Examples:
Enter: arr[] = {20, 10, 30, 9, 20, 9}, string S = “011011”, N = 6
Output: 80
Rationalization: Choose 20, 10, 30 and 20 in Sum, so, Sum = 80.Enter: arr[] = {30, 20, 10}, string S = “000”, N = 3.
Output: 0
Strategy: The given downside could be solved by utilizing a precedence queue primarily based on the next concept:
Say there are Okay occurrences of ‘1’ in string S. It may be seen that we are able to prepare the characters in a approach such that we are able to decide the Okay most parts from the array that are to the left of the final prevalence of ‘1’ in S. So we are able to use a precedence queue to get these Okay most parts.
Comply with the steps to unravel this downside:
Under is the implementation of the above method.
|
Time Complexity: O(N * log N)
Auxiliary House: O(N)
[ad_2]