📌  相关文章
📜  找到一个 N 长度的二进制字符串,它具有给定范围内元素的最大总和

📅  最后修改于: 2021-10-26 06:59:29             🧑  作者: Mango

给定一个大小为M的数组range[]和一个整数N ,任务是找到一个长度为N的二进制字符串,使得来自给定范围的字符串元素的总和是最大可能的。

例子:

方法:可以通过在每个范围内找到尽可能小的0计数和1计数之间的绝对差异来解决给定的问题。因此,我们的想法是将01放在字符串几乎相等的频率。最好的方法是交替放置01

因此,根据上述观察,要生成结果字符串,想法是使用变量i在范围[1, N]上迭代,如果i 的值为奇数,则打印0,否则打印1

下面是上述方法的实现。

C++
// C++ program for the above approach
 
#include 
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


输出:
01010

时间复杂度: O(N)
辅助空间: O(1)