File tree Expand file tree Collapse file tree 1 file changed +35
-0
lines changed
CodingBlocks Training/Day14 Expand file tree Collapse file tree 1 file changed +35
-0
lines changed Original file line number Diff line number Diff line change 1+ package Lecture14 ;
2+
3+ public class maxHeightLadder {
4+
5+ public static void main (String [] args ) {
6+
7+ int totalBlocks = 20 ;
8+ System .out .println (maxHeight (totalBlocks ));
9+
10+ System .out .println (maxHeight (21 ));
11+ System .out .println (maxHeight (100 ));
12+ System .out .println (maxHeight (1 ));
13+ System .out .println (maxHeight (0 ));
14+ System .out .println (maxHeight (6 ));
15+ System .out .println (maxHeight (7 ));
16+ }
17+
18+ // make ladder of maximum height possible with given number of blocks
19+ public static int maxHeight (int n ) {
20+ if (n < 1 ) {
21+ return -1 ;
22+ }
23+ int temp = n ;
24+ int blocks = n * (n + 1 ) / 2 ;
25+
26+ while (blocks > temp ) {
27+ n = n / 2 ;
28+ blocks = n * (n + 1 ) / 2 ;
29+ }
30+ if (((n + 1 ) * (n + 2 ) / 2 ) <= temp ) {
31+ return n + 1 ;
32+ }
33+ return n ;
34+ }
35+ }
You can’t perform that action at this time.
0 commit comments