-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday34.java
More file actions
59 lines (44 loc) · 1.37 KB
/
day34.java
File metadata and controls
59 lines (44 loc) · 1.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
//ques1:1922. Count Good Numbers
//link:https://leetcode.com/problems/count-good-numbers/description/
class Solution {
private static final long MOD = 1_000_000_007;
public int countGoodNumbers(long n) {
long evenCount = (n + 1) / 2;
long oddCount = n / 2;
long evenChoices = modPow(5, evenCount, MOD);
long oddChoices = modPow(4, oddCount, MOD);
return (int)((evenChoices * oddChoices) % MOD);
}
private long modPow(long base, long exponent, long mod) {
long result = 1;
base %= mod;
while (exponent > 0) {
if ((exponent & 1) == 1)
result = (result * base) % mod;
base = (base * base) % mod;
exponent >>= 1;
}
return result;
}
}
//ques2:Sort a stack
//link:https://www.geeksforgeeks.org/problems/sort-a-stack/1
class GfG {
public Stack<Integer> sort(Stack<Integer> s) {
if (!s.isEmpty()) {
int temp = s.pop();
sort(s);
sortedInsert(s, temp);
}
return s;
}
private void sortedInsert(Stack<Integer> stack, int element) {
if (stack.isEmpty() || element >= stack.peek()) {
stack.push(element);
} else {
int temp = stack.pop();
sortedInsert(stack, element);
stack.push(temp);
}
}
}