-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bff8714
commit fc49db0
Showing
4 changed files
with
130 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
//{ Driver Code Starts | ||
import java.io.*; | ||
import java.util.*; | ||
public class Main{ | ||
|
||
public static void main(String args[]) throws IOException{ | ||
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); | ||
PrintWriter ot = new PrintWriter(System.out); | ||
|
||
int t = Integer.parseInt(br.readLine().trim()); | ||
Solution soln = new Solution(); | ||
while(t-- > 0){ | ||
String s[] = br.readLine().trim().split(" "); | ||
int n = Integer.parseInt(s[0]); | ||
int k = Integer.parseInt(s[1]); | ||
ot.println(soln.findNthNumer(n, k)); | ||
} | ||
ot.close(); | ||
} | ||
|
||
} | ||
// } Driver Code Ends | ||
|
||
|
||
class Solution | ||
{ | ||
public long findNthNumer(int n, int k) | ||
{ | ||
// Code Here. | ||
long low = 0, high = (long)(1e18); | ||
dp = new Long[2][65][65]; | ||
|
||
while(low <= high) | ||
{ | ||
long mid = low + (high - low) / 2; | ||
long count = find(mid, k); | ||
if(count >= n) | ||
high = mid - 1; | ||
else | ||
low = mid + 1; | ||
} | ||
return low; | ||
} | ||
|
||
|
||
private long find(long n, int k) | ||
{ | ||
char s[] = Long.toBinaryString(n).toCharArray(); | ||
reset(); | ||
return dp(s, s.length, 1, k); | ||
} | ||
|
||
private long dp(char s[], int n, int tight, int k) | ||
{ | ||
if(k < 0) | ||
return 0; | ||
if(n == 0) | ||
return 1; | ||
|
||
if(dp[tight][k][n] != null) | ||
return dp[tight][k][n]; | ||
|
||
int ub = (tight == 1 ? (int)(s[s.length - n] - '0') : 1); | ||
|
||
long ans = 0; | ||
|
||
for(int dig = 0; dig <= ub; dig++) | ||
{ | ||
if(dig == ub) | ||
ans += dp(s, n - 1, tight, k - dig); | ||
else | ||
ans += dp(s, n - 1, 0, k - dig); | ||
} | ||
|
||
return dp[tight][k][n] = ans; | ||
} | ||
|
||
|
||
private void reset() | ||
{ | ||
for(int i = 0; i < 65; i++) | ||
{ | ||
Arrays.fill(dp[0][i], null); | ||
Arrays.fill(dp[1][i], null); | ||
} | ||
} | ||
|
||
|
||
|
||
private Long dp[][][]; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Time complexity - O(k * logn) | ||
Space complexity - O(k * logn) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Time complexity - O(m * n^2) | ||
Space complexity - O(n) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
class Solution | ||
{ | ||
public int numSubmatrixSumTarget(int[][] matrix, int target) | ||
{ | ||
final int m = matrix.length; | ||
final int n = matrix[0].length; | ||
int ans = 0; | ||
|
||
// Transfer each row in the matrix to the prefix sum. | ||
for (int[] row : matrix) | ||
for (int i = 1; i < n; ++i) | ||
row[i] += row[i - 1]; | ||
|
||
for (int baseCol = 0; baseCol < n; ++baseCol) | ||
for (int j = baseCol; j < n; ++j) | ||
{ | ||
Map<Integer, Integer> prefixCount = new HashMap<>(); | ||
prefixCount.put(0, 1); | ||
int sum = 0; | ||
|
||
for (int i = 0; i < m; ++i) | ||
{ | ||
if (baseCol > 0) | ||
sum -= matrix[i][baseCol - 1]; | ||
|
||
sum += matrix[i][j]; | ||
ans += prefixCount.getOrDefault(sum - target, 0); | ||
prefixCount.merge(sum, 1, Integer::sum); | ||
} | ||
} | ||
|
||
return ans; | ||
} | ||
} |