Skip to content

Commit

Permalink
max height ladder possible from given no. of blocks - O(logn)
Browse files Browse the repository at this point in the history
  • Loading branch information
NirmalSilwal committed Aug 30, 2020
1 parent 5a62b0c commit c37a067
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions CodingBlocks Training/Day14/maxHeightLadder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package Lecture14;

public class maxHeightLadder {

public static void main(String[] args) {

int totalBlocks = 20;
System.out.println(maxHeight(totalBlocks));

System.out.println(maxHeight(21));
System.out.println(maxHeight(100));
System.out.println(maxHeight(1));
System.out.println(maxHeight(0));
System.out.println(maxHeight(6));
System.out.println(maxHeight(7));
}

// make ladder of maximum height possible with given number of blocks
public static int maxHeight(int n) {
if (n < 1) {
return -1;
}
int temp = n;
int blocks = n * (n + 1) / 2;

while (blocks > temp) {
n = n / 2;
blocks = n * (n + 1) / 2;
}
if (((n + 1) * (n + 2) / 2) <= temp) {
return n + 1;
}
return n;
}
}

0 comments on commit c37a067

Please sign in to comment.