Skip to content

Commit f7d4865

Browse files
author
Prashant Jain
committed
Added more problems
1 parent 4c8cc3c commit f7d4865

2 files changed

Lines changed: 100 additions & 0 deletions

File tree

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package in.knowledgegate.dsa.binarysearch.problems;
2+
3+
/**
4+
* You have n coins and you want to build a staircase
5+
* with these coins. The staircase consists of k
6+
* rows where the ith row has exactly i coins.
7+
* The last row of the staircase may be incomplete.
8+
*
9+
* Given the integer n, return the number of
10+
* complete rows of the staircase you will build.
11+
*
12+
* Example 1:
13+
* Input: n = 5
14+
* Output: 2
15+
* Explanation: Because the 3rd row is incomplete,
16+
* we return 2.
17+
*
18+
* Example 2:
19+
* Input: n = 8
20+
* Output: 3
21+
* Explanation: Because the 4th row is incomplete,
22+
* we return 3.
23+
*
24+
* Constraints:
25+
* 1 <= n <= 231 - 1
26+
*/
27+
public class ArrangingCoins {
28+
public static void main(String[] args) {
29+
ArrangingCoins arrange =
30+
new ArrangingCoins();
31+
System.out.println("No of stairs:"
32+
+ arrange.arrangeCoins(5));
33+
}
34+
35+
private int arrangeCoins(int n) {
36+
int begin = 0;
37+
int end = n;
38+
while (begin <= end) {
39+
int mid = (begin + end)/2;
40+
int coinsUsed = (mid * (mid + 1)) / 2;
41+
if (coinsUsed == n) return mid;
42+
if (coinsUsed < n) {
43+
begin = mid + 1;
44+
} else {
45+
end = mid - 1;
46+
}
47+
}
48+
return end;
49+
}
50+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package in.knowledgegate.dsa.bitmanipulation;
2+
3+
/**
4+
* Given an integer num, return a string
5+
* representing its hexadecimal representation.
6+
* For negative integers, two’s complement method
7+
* is used.
8+
*
9+
* All the letters in the answer string should be
10+
* lowercase characters, and there should not be
11+
* any leading zeros in the answer except for the
12+
* zero itself.
13+
*
14+
* Note: You are not allowed to use any built-in
15+
* library method to directly solve this problem.
16+
*
17+
*
18+
* Example 1:
19+
* Input: num = 26
20+
* Output: "1a"
21+
*
22+
* Example 2:
23+
* Input: num = -1
24+
* Output: "ffffffff"
25+
*
26+
* Constraints:
27+
* -231 <= num <= 231 - 1
28+
*/
29+
public class ConvertNumberToHexadecimal {
30+
public static void main(String[] args) {
31+
ConvertNumberToHexadecimal converter =
32+
new ConvertNumberToHexadecimal();
33+
System.out.println("Hex num is: "
34+
+ converter.toHex(26));
35+
}
36+
37+
public String toHex(int num) {
38+
char[] mapping = new char[]{'0', '1', '2'
39+
, '3', '4', '5', '6', '7', '8', '9',
40+
'a', 'b', 'c', 'd', 'e', 'f'};
41+
if (num == 0) return "0";
42+
StringBuilder result = new StringBuilder();
43+
while (num > 0) {
44+
int endBits = num & 15;
45+
result.insert(0, mapping[endBits]);
46+
num = num >> 4;
47+
}
48+
return result.toString();
49+
}
50+
}

0 commit comments

Comments
 (0)