Related Articles

Related Articles

Count sequences of given length having non-negative prefix sums that can be generated by given values
  • Last Updated : 25 Oct, 2020

Given two integers M and X, the task is to find the number of sequences of length M that can be generated comprising of X and -X such that their respective counts are equal and the prefix sum upto each index of the resulting sequence is non-negative.

Examples:

Input: M = 4, X = 5
Output: 2
Explanation:
There are only 2 possible sequences that have all possible prefix sums non-negative: 

  1. {+5, +5, -5, -5}
  2. {+5, -5, +5, -5}

Input: M = 6, X = 2
Output: 5
Explanation:
There are only 5 possible sequences that have all possible prefix sums non-negative:

  1. {+2, +2, +2, -2, -2, -2}
  2. {+2, +2, -2, -2, +2, -2}
  3. {+2, -2, +2, -2, +2, -2}
  4. {+2, +2, -2, +2, -2, -2}
  5. {+2, -2, +2, +2, -2, -2}

Naive Approach: The simplest approach is to generate all possible arrangements of size M with the given integers +X and -X and find the prefix sum of each arrangement formed and count those sequence whose prefix sum array has only non-negative elements. Print the count of such sequence after the above steps. 



Time Complexity: O((M*(M!))/((M/2)!)2
Auxiliary Space: O(M)

Efficient Approach: The idea is to observe the pattern that for any sequence formed the number of positive X that has occurred at each index is always greater than or equal to the number of negative X occurred. This is similar to the pattern of Catalan Numbers. In this case, check that at any point the number of positive X occurred is always greater than or equal to the number of negative X occurred which is the pattern of Catalan numbers. So the task is to find Nth Catalan number where N = M/2.

K_{N} = \frac{\binom{2N}{N}}{N + 1}

where, KN is Nth the Catalan Number and 

\binom{2N}{N}

is the binomial coefficient. 
 

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program for the above approach
  
#include <bits/stdc++.h>
using namespace std;
  
// Function to find the Binomial
// Coefficient C(n, r)
unsigned long int binCoff(unsigned int n,
                          unsigned int r)
{
    // Stores the value C(n, r)
    unsigned long int val = 1;
    int i;
  
    // Update C(n, r) = C(n, n - r)
    if (r > (n - r))
        r = (n - r);
  
    // Find C(n, r) iteratively
    for (i = 0; i < r; i++) {
        val *= (n - i);
        val /= (i + 1);
    }
  
    // Return the final value
    return val;
}
  
// Function to find number of sequence
// whose prefix sum at each index is
// always non-negative
void findWays(int M)
{
    // Find n
    int n = M / 2;
  
    unsigned long int a, b, ans;
  
    // Value of C(2n, n)
    a = binCoff(2 * n, n);
  
    // Catalan number
    b = a / (n + 1);
  
    // Print the answer
    cout << b;
}
  
// Driver Code
int main()
{
    // Given M and X
    int M = 4, X = 5;
  
    // Function Call
    findWays(M);
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program for the above approach
import java.io.*;
  
class GFG{
  
// Function to find the Binomial
// Coefficient C(n, r)
static long binCoff(long n, long r)
{
      
    // Stores the value C(n, r)
    long val = 1;
    int i;
  
    // Update C(n, r) = C(n, n - r)
    if (r > (n - r))
        r = (n - r);
  
    // Find C(n, r) iteratively
    for(i = 0; i < r; i++)
    {
        val *= (n - i);
        val /= (i + 1);
    }
  
    // Return the final value
    return val;
}
  
// Function to find number of sequence
// whose prefix sum at each index is
// always non-negative
static void findWays(int M)
{
      
    // Find n
    int n = M / 2;
  
    long a, b, ans;
  
    // Value of C(2n, n)
    a = binCoff(2 * n, n);
  
    // Catalan number
    b = a / (n + 1);
  
    // Print the answer
    System.out.print(b);
}
  
// Driver Code
public static void main(String[] args)
{
  
    // Given M and X
    int M = 4, X = 5;
  
    // Function Call
    findWays(M);
}
}
  
// This code is contributed by akhilsaini

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program for the above approach
  
# Function to find the Binomial
# Coefficient C(n, r)
def binCoff(n, r):
  
    # Stores the value C(n, r)
    val = 1
  
    # Update C(n, r) = C(n, n - r)
    if (r > (n - r)):
        r = (n - r)
  
    # Find C(n, r) iteratively
    for i in range(0, r):
        val *= (n - i)
        val //= (i + 1)
  
    # Return the final value
    return val
  
# Function to find number of sequence
# whose prefix sum at each index is
# always non-negative
def findWays(M):
  
    # Find n
    n = M // 2
  
    # Value of C(2n, n)
    a = binCoff(2 * n, n)
  
    # Catalan number
    b = a // (n + 1)
  
    # Print the answer
    print(b)
  
# Driver Code
if __name__ == '__main__':
  
    # Given M and X
    M = 4
    X = 5
  
    # Function Call
    findWays(M)
  
# This code is contributed by akhilsaini

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program for the above approach
using System;
  
class GFG{
  
// Function to find the Binomial
// Coefficient C(n, r)
static long binCoff(long n, long r)
{
      
    // Stores the value C(n, r)
    long val = 1;
    int i;
  
    // Update C(n, r) = C(n, n - r)
    if (r > (n - r))
        r = (n - r);
  
    // Find C(n, r) iteratively
    for(i = 0; i < r; i++)
    {
        val *= (n - i);
        val /= (i + 1);
    }
  
    // Return the final value
    return val;
}
  
// Function to find number of sequence
// whose prefix sum at each index is
// always non-negative
static void findWays(int M, int X)
{
      
    // Find n
    int n = M / 2;
  
    long a, b;
  
    // Value of C(2n, n)
    a = binCoff(2 * n, n);
  
    // Catalan number
    b = a / (n + 1);
  
    // Print the answer
    Console.WriteLine(b);
}
  
// Driver Code
public static void Main()
{
      
    // Given M and X
    int M = 4;
    int X = 5;
  
    // Function Call
    findWays(M, X);
}
}
  
// This code is contributed by akhilsaini

chevron_right


Output: 

2



 

Time Complexity: O(M)
Auxiliary Space: O(1)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up
Recommended Articles
Page :