Generate an Array in which count of even and odd sum sub-arrays are E and O respectively

Given three integers N, E and O. The task is to find an array of size N such that the number of sub-arrays of sum even and odd are E and O respectively.
Examples:Â
Â
Input: N = 3, E = 2, O = 4Â
Output: 0 1 0Â
There are total 6 sub-arrays: {0}, {1}, {0}, {0, 1}, {1, 0}, {0, 1, 0}.Â
Their sums are {0, 1, 0, 1, 1, 1} respectively.Â
2 of them are even and 4 of them are odd.
Input: N = 3, E = 0, O = 6Â
Output: -1Â
Â
Â
Naive approach: Use bitmasking to generate all combinations of 0’s and 1’s in the array. For every combination we calculate the number of even sum and odd sum sub-arrays. If they are equal to the given values then it is the right combination and we printÂ
the array.Â
For this approach to generate all the sets it would take and for each combination, we find number of sub-arrays costingÂ
.
Efficient approach: As we all know about PrefixSums of an array. So We will calculate the number of even PrefixSum and odd PrefixSum. If we somehow know the number of prefixSums having odd and even parity respectively, we can correspondingly create any valid array provided that total count of oddPrefixSums and evenPrefixSums is N + 1.
Example: If we have 3 evenPrefixSums and 2 oddPrefixSums, we can create an array [0, 0, 1, 0]. The trick is to place the only 1 after placing (evenPrefixSums – 1) zeros. All the remaining prefixSums will obviously be of odd parity.
The following equation holds true.Â
Â
evenPrefixSums + oddPrefixSums = N + 1Â
Â
Since, prefixSum_i – prefixSum_j contributes to sums of contiguous sub-arrays, both should be of different parity. Hence, number of contiguous sub-arrays having odd parity will be C(evenPrefixSums, 1) * C(oddPrefixSums, 1). This gives rise to another equation.Â
Â
evenPrefixSums * oddPrefixSums = OÂ
Â
We can form a quadratic equation and solve it to get the respective values. If you do not find any valid values, output -1.
Below is the implementation of the above approach:Â
Â
C++
// C++ implementation of the approach#include <algorithm>#include <iostream>using namespace std;Â
// Function to generate and print the required arrayvoid CreateArray(int N, int even, int odd){Â Â Â Â int temp = -1;Â Â Â Â int OddPreSums;Â
    // Find the number of odd prefix sums    for (int i = 0; i <= N + 1; i++) {        if (i * ((N + 1) - i) == odd) {            temp = 0;            OddPreSums = i;            break;        }    }Â
    // If no odd prefix sum found    if (temp == -1) {        cout << temp << endl;    }    else {Â
        // Calculating the number of even prefix sums        int EvenPreSums = (N + 1) - OddPreSums;        int e = 1;        int o = 0;Â
        // Stores the current prefix sum        int CurrSum = 0;        for (int i = 0; i < N; i++) {Â
            // If current prefix sum is even            if (CurrSum % 2 == 0) {Â
                // Print 0 until e = EvenPreSums - 1                if (e < EvenPreSums) {                    e++;                    cout << "0 ";                }                else {                    o++;Â
                    // Print 1 when e = EvenPreSums                    cout << "1 ";                    CurrSum++;                }            }            else {                if (e < EvenPreSums) {                    e++;                    cout << "1 ";                    CurrSum++;                }                else {                    o++;Â
                    // Print 0 for rest of the values                    cout << "0 ";                }            }        }        cout << endl;    }}Â
// Driver codeint main(){Â Â Â Â int N = 15;Â Â Â Â int even = 60, odd = 60;Â Â Â Â CreateArray(N, even, odd);Â
    return 0;} |
Java
// Java implementation of the approachimport java.io.*;Â
class GFG {Â
    // Function to generate and print the required array    static void CreateArray(int N, int even, int odd)    {        int EvenPreSums = 1;        int temp = -1;        int OddPreSums = 0;Â
        // Find the number of odd prefix sums        for (int i = 0; i <= N + 1; i++) {            if (i * ((N + 1) - i) == odd) {                temp = 0;                OddPreSums = i;                break;            }        }Â
        // If no odd prefix sum found        if (temp == -1) {            System.out.println(temp);        }        else {Â
            // Calculating the number of even prefix sumsÂ
            EvenPreSums = ((N + 1) - OddPreSums);            int e = 1;            int o = 0;Â
            // Stores the current prefix sum            int CurrSum = 0;            for (int i = 0; i < N; i++) {Â
                // If current prefix sum is even                if (CurrSum % 2 == 0) {Â
                    // Print 0 until e = EvenPreSums - 1                    if (e < EvenPreSums) {                        e++;                        System.out.print("0 ");                    }                    else {                        o++;Â
                        // Print 1 when e = EvenPreSums                        System.out.print("1 ");                        CurrSum++;                    }                }                else {                    if (e < EvenPreSums) {                        e++;                        System.out.print("1 ");                        CurrSum++;                    }                    else {                        o++;Â
                        // Print 0 for rest of the values                        System.out.print("0 ");                    }                }            }            System.out.println();        }    }Â
    // Driver code    public static void main(String[] args)    {Â
        int N = 15;        int even = 60, odd = 60;        CreateArray(N, even, odd);    }}Â
// This code is contributed by akt_mit |
Python3
# Python 3 implementation of the approachÂ
# Function to generate and print# the required arraydef CreateArray(N, even, odd):    temp = -1         # Find the number of odd prefix sums    for i in range(N + 2):        if (i * ((N + 1) - i) == odd):            temp = 0            OddPreSums = i            breakÂ
    # If no odd prefix sum found    if (temp == -1):        print(temp)    else:                 # Calculating the number         # of even prefix sums        EvenPreSums = (N + 1) - OddPreSums        e = 1        o = 0Â
        # Stores the current prefix sum        CurrSum = 0        for i in range(N):                         # If current prefix sum is even            if (CurrSum % 2 == 0):                                 # Print 0 until e = EvenPreSums - 1                if (e < EvenPreSums):                    e += 1                    print("0 ", end = "")                else:                    o += 1Â
                    # Print 1 when e = EvenPreSums                    print("1 ", end = "")                    CurrSum += 1                 else:                if (e < EvenPreSums):                    e += 1                    print("1 ")                    CurrSum += 1                else:                    o += 1Â
                    # Print 0 for rest of the values                    print("0 ", end = "")        print("\n", end = "")Â
# Driver codeif __name__ == '__main__':Â Â Â Â N = 15Â Â Â Â even = 60Â Â Â Â odd = 60Â Â Â Â CreateArray(N, even, odd)Â
# This code is contributed by# Surendra_Gangwar |
C#
// C# implementation of the approachusing System;Â
class GFG {Â
    // Function to generate and print the required array    static void CreateArray(int N, int even, int odd)    {        int EvenPreSums = 1;        int temp = -1;        int OddPreSums = 0;Â
        // Find the number of odd prefix sums        for (int i = 0; i <= N + 1; i++) {            if (i * ((N + 1) - i) == odd) {                temp = 0;                OddPreSums = i;                break;            }        }Â
        // If no odd prefix sum found        if (temp == -1) {            Console.WriteLine(temp);        }        else {Â
            // Calculating the number of even prefix sumsÂ
            EvenPreSums = ((N + 1) - OddPreSums);            int e = 1;            int o = 0;Â
            // Stores the current prefix sum            int CurrSum = 0;            for (int i = 0; i < N; i++) {Â
                // If current prefix sum is even                if (CurrSum % 2 == 0) {Â
                    // Print 0 until e = EvenPreSums - 1                    if (e < EvenPreSums) {                        e++;                        Console.Write("0 ");                    }                    else {                        o++;Â
                        // Print 1 when e = EvenPreSums                        Console.Write("1 ");                        CurrSum++;                    }                }                else {                    if (e < EvenPreSums) {                        e++;                        Console.Write("1 ");                        CurrSum++;                    }                    else {                        o++;Â
                        // Print 0 for rest of the values                        Console.Write("0 ");                    }                }            }            Console.WriteLine();        }    }Â
    // Driver code    static public void Main()    {        int N = 15;        int even = 60, odd = 60;        CreateArray(N, even, odd);    }}Â
// This code is contributed by Tushil |
PHP
<?php// PHP implementation of the approach Â
// Function to generate and print the required array function CreateArray($N, $even, $odd) { Â Â Â Â $temp = -1; Â Â Â Â $OddPreSums = 0; Â
    // Find the number of odd prefix sums     for ($i = 0; $i <= $N + 1; $i++)     {         if ($i * (($N + 1) - $i) == $odd)         {             $temp = 0;             $OddPreSums = $i;             break;         }     } Â
    // If no odd prefix sum found     if ($temp == -1)     {         echo temp ;     }     else    { Â
        // Calculating the number of even prefix sums         $EvenPreSums = ($N + 1) - $OddPreSums;         $e = 1;         $o = 0; Â
        // Stores the current prefix sum         $CurrSum = 0;         for ($i = 0; $i < $N; $i++)        { Â
            // If current prefix sum is even             if ($CurrSum % 2 == 0)            { Â
                // Print 0 until e = EvenPreSums - 1                 if ($e < $EvenPreSums)                 {                     $e++;                     echo "0 ";                 }                 else                {                     $o++; Â
                    // Print 1 when e = EvenPreSums                     echo "1 ";                     $CurrSum++;                 }             }             else            {                 if ($e < $EvenPreSums)                {                     $e++;                     echo "1 ";                     $CurrSum++;                 }                 else                {                     $o++; Â
                    // Print 0 for rest of the values                     echo "0 ";                 }             }         }         echo "\n";     } } Â
// Driver code $N = 15; $even = 60;$odd = 60; CreateArray($N, $even, $odd); Â
// This code is contributed by AnkitRai01?> |
Javascript
<script>    // Javascript implementation of the approach         // Function to generate and print the required array    function CreateArray(N, even, odd)    {        let EvenPreSums = 1;        let temp = -1;        let OddPreSums = 0;           // Find the number of odd prefix sums        for (let i = 0; i <= N + 1; i++) {            if (i * ((N + 1) - i) == odd) {                temp = 0;                OddPreSums = i;                break;            }        }           // If no odd prefix sum found        if (temp == -1) {            document.write(temp);        }        else {               // Calculating the number of even prefix sums               EvenPreSums = ((N + 1) - OddPreSums);            let e = 1;            let o = 0;               // Stores the current prefix sum            let CurrSum = 0;            for (let i = 0; i < N; i++) {                   // If current prefix sum is even                if (CurrSum % 2 == 0) {                       // Print 0 until e = EvenPreSums - 1                    if (e < EvenPreSums) {                        e++;                        document.write("0 ");                    }                    else {                        o++;                           // Print 1 when e = EvenPreSums                        document.write("1 ");                        CurrSum++;                    }                }                else {                    if (e < EvenPreSums) {                        e++;                        document.write("1 ");                        CurrSum++;                    }                    else {                        o++;                           // Print 0 for rest of the values                        document.write("0 ");                    }                }            }            document.write();        }    }         let N = 15;    let even = 60, odd = 60;    CreateArray(N, even, odd);     </script> |
0 0 0 0 0 0 0 0 0 1 0 0 0 0 0
Â
Time Complexity: O(N), to iterate over the array
Auxiliary Space: O(1), as no extra space is required
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!



