Skip to content

Commit e193568

Browse files
author
Prashant Jain
committed
Added Maths problems
1 parent f7d4865 commit e193568

4 files changed

Lines changed: 199 additions & 0 deletions

File tree

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package in.knowledgegate.dsa.math;
2+
3+
/**
4+
* Given two non-negative integers, num1 and num2
5+
* represented as string, return the sum of num1
6+
* and num2 as a string.
7+
*
8+
* You must solve the problem without using any
9+
* built-in library for handling large integers
10+
* (such as BigInteger). You must also not convert
11+
* the inputs to integers directly.
12+
*
13+
* Example 1:
14+
*
15+
* Input: num1 = "11", num2 = "123"
16+
* Output: "134"
17+
* Example 2:
18+
*
19+
* Input: num1 = "456", num2 = "77"
20+
* Output: "533"
21+
* Example 3:
22+
*
23+
* Input: num1 = "0", num2 = "0"
24+
* Output: "0"
25+
*
26+
*
27+
* Constraints:
28+
*
29+
* 1 <= num1.length, num2.length <= 104
30+
* num1 and num2 consist of only digits.
31+
* num1 and num2 don't have any leading zeros except for the zero itself.
32+
*/
33+
public class AddStrings {
34+
public static void main(String[] args) {
35+
AddStrings addStrings = new AddStrings();
36+
System.out.println("Sum is: "
37+
+ addStrings.addStrings("456765765",
38+
"777868"));
39+
}
40+
public String addStrings(String num1, String num2) {
41+
int carry = 0;
42+
StringBuilder result = new StringBuilder();
43+
int i = num1.length() - 1;
44+
int j = num2.length() - 1;
45+
while (i >= 0 || j >= 0 || carry > 0) {
46+
int x = i >= 0 ? num1.charAt(i--) - '0': 0;
47+
int y = j >= 0 ? num2.charAt(j--) - '0': 0;
48+
int rem = (x + y + carry) % 10;
49+
result.insert(0, rem);
50+
carry = (x + y + carry) / 10;
51+
}
52+
return result.toString();
53+
}
54+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package in.knowledgegate.dsa.math;
2+
3+
/**
4+
* Given an integer num, return a string representing
5+
* its hexadecimal representation. For negative
6+
* integers, two’s complement method is used.
7+
*
8+
* All the letters in the answer string should be
9+
* lowercase characters, and there should not be
10+
* any leading zeros in the answer except for the
11+
* zero itself.
12+
*
13+
* Note: You are not allowed to use any built-in
14+
* library method to directly solve this problem.
15+
*
16+
* Example 1:
17+
* Input: num = 26
18+
* Output: "1a"
19+
*
20+
* Example 2:
21+
* Input: num = -1
22+
* Output: "ffffffff"
23+
*
24+
* Constraints:
25+
* -231 <= num <= 231 - 1
26+
*/
27+
public class ConvertNumberToHexadecimal {
28+
public static void main(String[] args) {
29+
ConvertNumberToHexadecimal converter =
30+
new ConvertNumberToHexadecimal();
31+
System.out.println("Output is: "
32+
+ converter.toHex(156));
33+
}
34+
35+
public String toHex(int num) {
36+
char[] hex = new char[]{'0', '1', '2',
37+
'3', '4', '5', '6', '7', '8', '9', 'a'
38+
, 'b', 'c', 'd', 'e', 'f'};
39+
StringBuilder sb = new StringBuilder();
40+
while (num > 0) {
41+
int rem = num % 16;
42+
sb.insert(0, hex[rem]);
43+
num /= 16;
44+
}
45+
return sb.toString();
46+
}
47+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package in.knowledgegate.dsa.math;
2+
3+
/**
4+
* Given an integer n, return the number of prime
5+
* numbers that are strictly less than n.
6+
*
7+
* Example 1:
8+
* Input: n = 10
9+
* Output: 4
10+
* Explanation: There are 4 prime numbers less
11+
* than 10, they are 2, 3, 5, 7
12+
*
13+
* Example 2:
14+
* Input: n = 0
15+
* Output: 0
16+
*
17+
* Example 3:
18+
* Input: n = 1
19+
* Output: 0
20+
*
21+
* Constraints:
22+
* 0 <= n <= 5 * 106
23+
*/
24+
public class CountPrimes {
25+
public static void main(String[] args) {
26+
CountPrimes counter = new CountPrimes();
27+
System.out.println("No of prime are:"
28+
+ counter.countPrimes(100));
29+
}
30+
31+
public int countPrimes(int n) {
32+
boolean[] notPrime = new boolean[n];
33+
int countPrime = 0;
34+
for (int i = 2; i < n; i++) {
35+
if (!notPrime[i]) {
36+
countPrime++;
37+
for (int j = 2; i*j < n; j++) {
38+
notPrime[i*j] = true;
39+
}
40+
}
41+
}
42+
return countPrime;
43+
}
44+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package in.knowledgegate.dsa.math;
2+
3+
/**
4+
* Given a string columnTitle that represents the
5+
* column title as appears in an Excel sheet,
6+
* return its corresponding column number.
7+
*
8+
* For example:
9+
* A -> 1
10+
* B -> 2
11+
* C -> 3
12+
* ...
13+
* Z -> 26
14+
* AA -> 27
15+
* AB -> 28
16+
* ...
17+
*
18+
*
19+
* Example 1:
20+
* Input: columnTitle = "A"
21+
* Output: 1
22+
*
23+
* Example 2:
24+
* Input: columnTitle = "AB"
25+
* Output: 28
26+
*
27+
* Example 3:
28+
* Input: columnTitle = "ZY"
29+
* Output: 701
30+
*
31+
* Constraints:
32+
* 1 <= columnTitle.length <= 7
33+
* columnTitle consists only of uppercase English letters.
34+
* columnTitle is in the range ["A", "FXSHRXW"].
35+
*/
36+
public class ExcelSheetColumnNumber {
37+
38+
public static void main(String[] args) {
39+
ExcelSheetColumnNumber baseConversion =
40+
new ExcelSheetColumnNumber();
41+
System.out.println("Output is:" +
42+
baseConversion.titleToNumber("AB"));
43+
}
44+
45+
public int titleToNumber(String columnTitle) {
46+
int result = 0, pow = 0;
47+
for (int i = columnTitle.length() - 1; i >= 0; i--, pow++) {
48+
char c = columnTitle.charAt(i);
49+
int val = c - 'A' + 1;
50+
result += val * (int)Math.pow(26, pow);
51+
}
52+
return result;
53+
}
54+
}

0 commit comments

Comments
 (0)