Find an N-length Binary String having maximum sum of elements from given ranges
Last Updated :
22 Jul, 2021
Given an array of pairs ranges[] of size M and an integer N, the task is to find a binary string of length N such that the sum of elements of the string from the given ranges is maximum possible.
Examples:
Input: N = 5, M = 3, ranges[] = {{1, 3}, {2, 4}, {2, 5}}
Output: 01100
Explanation:
Range [1, 3]: Freq of 0's = 1, Freq of 1's = 2. Sum = 1*2 = 2
Range [2, 4]: Freq of 0's = 1, Freq of 1's = 2. Sum = 1*2 = 2
Range [2, 5]: Freq of 0's = 2, Freq of 1's = 2. Sum = 2*2 = 4
Therefore, the required sum = 2 + 2 + 4 = 8, which is the maximum possible.
Input: N = 6, M = 1, ranges = {{1, 6}}
Output: 000111
Approach: The given problem can be solved by finding the absolute difference between the count of 0's and the count of 1's as small as possible for every range. Therefore, the idea is to place both i.e., 0 and 1 at an almost equal frequency in the string. The best way to do so is placing the 0s and 1s alternatively.
Hence, from the above observations, to generate the resultant string the idea is to iterate over the range [1, N] using the variable i and if the value of i is odd, then print 0, otherwise print 1.
Below is the implementation of the above approach.
C++
// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to find an N-length binary
// string having maximum sum of
// elements from all given ranges
void printBinaryString(int arr[][3], int N)
{
// Iterate over the range [1, N]
for (int i = 1; i <= N; i++) {
// If i is odd, then print 0
if (i % 2) {
cout << 0;
}
// Otherwise, print 1
else {
cout << 1;
}
}
}
// Driver Code
int main()
{
int N = 5, M = 3;
int arr[][3] = { { 1, 3 },
{ 2, 4 },
{ 2, 5 } };
// Function Call
printBinaryString(arr, N);
return 0;
}
Java
// Java program for the above approach
import java.io.*;
class GFG
{
// Function to find an N-length binary
// string having maximum sum of
// elements from all given ranges
static void printBinaryString(int arr[][], int N)
{
// Iterate over the range [1, N]
for (int i = 1; i <= N; i++) {
// If i is odd, then print 0
if (i % 2 == 1) {
System.out.print(0);
}
// Otherwise, print 1
else {
System.out.print(1);
}
}
}
// Driver Code
public static void main (String[] args) {
int N = 5, M = 3;
int arr[][] = { { 1, 3 },
{ 2, 4 },
{ 2, 5 } };
// Function Call
printBinaryString(arr, N);
}
}
// This code is contributed by Lokeshpotta20.
Python3
# Python program for the above approach
# Function to find an N-length binary
# string having maximum sum of
# elements from all given ranges
def printBinaryString(arr, N):
# Iterate over the range [1, N]
for i in range(1, N + 1):
# If i is odd, then print 0
if (i % 2):
print(0, end="");
# Otherwise, print 1
else:
print(1, end="");
# Driver Code
N = 5;
M = 3;
arr = [ [ 1, 3 ], [ 2, 4 ], [ 2, 5 ] ];
# Function Call
printBinaryString(arr, N);
# This code is contributed by _saurabh_jaiswal.
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG{
// Function to find an N-length binary
// string having maximum sum of
// elements from all given ranges
static void printBinaryString(int [,]arr, int N)
{
// Iterate over the range [1, N]
for (int i = 1; i <= N; i++) {
// If i is odd, then print 0
if (i % 2 == 1) {
Console.Write(0);
}
// Otherwise, print 1
else {
Console.Write(1);
}
}
}
// Driver Code
public static void Main()
{
int N = 5;
int [,]arr = { { 1, 3 },
{ 2, 4 },
{ 2, 5 } };
// Function Call
printBinaryString(arr, N);
}
}
// This code is contributed by ipg2016107.
JavaScript
<script>
// Javascript program for the above approach
// Function to find an N-length binary
// string having maximum sum of
// elements from all given ranges
function printBinaryString(arr, N)
{
// Iterate over the range [1, N]
for (let i = 1; i <= N; i++) {
// If i is odd, then print 0
if (i % 2) {
document.write(0);
}
// Otherwise, print 1
else {
document.write(1);
}
}
}
// Driver Code
let N = 5, M = 3;
let arr = [ [ 1, 3 ],
[ 2, 4 ],
[ 2, 5 ] ];
// Function Call
printBinaryString(arr, N);
// This code is contributed by subhammahato348.
</script>
Time Complexity: O(N)
Auxiliary Space: O(1)
Similar Reads
Maximum sum of lengths of a pair of strings with no common characters from a given array Given an array arr[] consisting of N strings, the task is to find the maximum sum of length of the strings arr[i] and arr[j] for all unique pairs (i, j), where the strings arr[i] and arr[j] contains no common characters. Examples: Input: arr[] = ["abcd", "cat", "lto", "car", "wxyz", "abcdef"]Output:
7 min read
Maximum difference of zeros and ones in binary string Given a binary string of 0s and 1s. The task is to find the length of the substring which is having a maximum difference between the number of 0s and the number of 1s (number of 0s - number of 1s). In case of all 1s print -1. Examples: Input : S = "11000010001"Output : 6From index 2 to index 9, ther
15 min read
Maximum range length such that A[i] is maximum in given range for all i from [1, N] Given an array arr[] consisting of N distinct integers. For each i (0 ? i < n), find a range [l, r] such that A[i] = max(A[l], A[l+1], ..., A[r]) and l ? i ? r and r-l is maximized. Examples: Input: arr[] = {1, 3, 2}Output: {0 0}, {0 2}, {2 2}Explanation: For i=0, 1 is maximum in range [0, 0] onl
8 min read
Maximize the Binary String value by replacing A[i]th elements from start or end Given a string S of size M consisting of only zeroes (and hence representing the integer 0). Also, given an array A[] of size N whose, each element is an integer in the range [1, M]. The task is to maximize the integer represented by the string by performing the following operation N times : In ith
6 min read
Maximum length of same indexed subarrays from two given arrays satisfying the given condition Given two arrays arr[] and brr[] and an integer C, the task is to find the maximum possible length, say K, of the same indexed subarrays such that the sum of the maximum element in the K-length subarray in brr[] with the product between K and sum of the K-length subarray in arr[] does not exceed C.
15+ min read
Maximize sum by splitting given binary strings based on given conditions Given two binary strings str1 and str2 each of length N, the task is to split the strings in such a way that the sum is maximum with given conditions. Split both strings at the same position into equal length substrings.If both the substrings have only 0's then the value of that substring to be adde
7 min read