Ninja And The Triangle Problem Statement

Ninja is provided with 'N' stars and the task is to construct a triangle such that the 'i'th level of the triangle uses 'i' number of stars. The goal is to make the triangle as tall as possible using the given stars.

Example:

Input:
N = 6
Output:
3
Explanation:

With 6 stars, Ninja can build a triangle with a maximum height of 3 levels: 1+2+3 = 6 stars.

Constraints:

  • 1 <= T <= 100
  • 1 <= N <= 10^8
Input:
The first line contains an integer 'T', the number of test cases. Each of the next 'T' lines contains a single integer 'N', the number of stars available.
Output:
For each test case, output a single integer representing the maximum height of the triangle possible with the given stars. Output each result on a separate line.

Note:

No need to print anything. Implement the function to solve the problem.

AnswerBot
4mo

Given 'N' stars, construct a triangle with maximum height using 'i' stars in 'i'th level.

  • Calculate the maximum height of the triangle using the formula: height = floor((-1 + sqrt(1 + 8 * N)) / 2)

  • Itera...read more

Help your peers!
Select
Add answer anonymously...

Top Software Developer Interview Questions Asked at Blackrock

Q. Validate Binary Search Tree Problem Statement Your task is to determine if a giv...read more
Q. Bridge in Graph Problem Statement Given an undirected graph with V vertices and ...read more
Q. Shortest Distance in a Binary Search Tree Your task is to determine the shortest...read more
Software Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+

Reviews

10L+

Interviews

4 Cr+

Salaries

1.5 Cr+

Users

Contribute to help millions

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

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits