Structurally Unique Binary Trees of Dragon Balls

Goku has ‘N’ Dragon Balls, where each Dragon Ball is unique. The ith Dragon Ball has ‘i’ stars on it, meaning the first Dragon Ball has 1 star, the second has 2 stars, and so on.

Gohan needs to arrange these ‘N’ Dragon Balls in a binary tree format, ensuring the following conditions are met:

  • The left subtree of any Dragon Ball ‘D’ should only have Dragon Balls with fewer stars than ‘D’.
  • The right subtree of any Dragon Ball ‘D’ should only have Dragon Balls with more stars than ‘D’.

Objective:

Determine how many structurally unique binary trees can be constructed following the conditions provided. Return the count of these trees modulo 109 + 7.

Input:
First line contains an integer 'T', the number of test cases.
Then, for each test case, a single integer ‘N’ is given, denoting the number of Dragon Balls.
Output:
Output the number of structurally unique binary trees for each test case on a new line.
Example:
Input:
2
2
3
Output:
2
5

Constraints:

  • 1 <= T <= 100
  • 1 <= N <= 5000
  • Time Limit: 1 second
Note:

You do not need to print anything; it has been managed for you. Implement the function provided.

AnswerBot
2d

Count the number of structurally unique binary trees that can be constructed with given Dragon Balls.

  • Use dynamic programming to solve this problem efficiently.

  • The number of structurally unique binary ...read more

Help your peers!
Add answer anonymously...
Atlassian Software Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter