VOOZH about

URL: https://www.geeksforgeeks.org/dsa/lobb-number/

⇱ Lobb Number - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Lobb Number

Last Updated : 15 May, 2023

In combinatorial mathematics, the Lobb number Lm, n counts the number of ways that n + m open parentheses can be arranged to form the start of a valid sequence of balanced parentheses. 
The Lobb number are parameterized by two non-negative integers m and n with n >= m >= 0. It can be obtained by: 

Lobb Number is also used to count the number of ways in which n + m copies of the value +1 and n - m copies of the value -1 may be arranged into a sequence such that all of the partial sums of the sequence are non- negative.
Examples : 

Input : n = 3, m = 2
Output : 5

Input : n =5, m =3
Output :35

The idea is simple, we use a function that computes binomial coefficients for given values. Using this function and above formula, we can compute Lobb numbers. 


Output
35

Time Complexity: O(2*n*(m+n))
Auxiliary Space: O((2*n)*(m+n))
 
Efficient approach: Space optimization

In previous approach the current value dp[i][j] is only depend upon the current and previous row values of DP. So to optimize the space complexity we use a single 1D array to store the computations.

Implementation steps:

  • Create a 1D vector C of size K+1.
  • Set a base case by initializing the values of C.
  • Now iterate over subproblems by the help of nested loop and get the current value from previous computations.
  • At last return and print the final answer stored in C[K].

Implementation:


Output
35

Time Complexity: O(n^2)
Auxiliary Space: O(k)

Comment