diff --git a/GeeksForGeeks/January/28-1-24/GFG.java b/GeeksForGeeks/January/28-1-24/GFG.java new file mode 100644 index 0000000..fdbc822 --- /dev/null +++ b/GeeksForGeeks/January/28-1-24/GFG.java @@ -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[][][]; + +} diff --git a/GeeksForGeeks/January/28-1-24/README.md b/GeeksForGeeks/January/28-1-24/README.md new file mode 100644 index 0000000..5cdd53a --- /dev/null +++ b/GeeksForGeeks/January/28-1-24/README.md @@ -0,0 +1,2 @@ +Time complexity - O(k * logn) +Space complexity - O(k * logn) diff --git a/LeetCode/January/28-1-24/README.md b/LeetCode/January/28-1-24/README.md new file mode 100644 index 0000000..ca0a60f --- /dev/null +++ b/LeetCode/January/28-1-24/README.md @@ -0,0 +1,2 @@ +Time complexity - O(m * n^2) +Space complexity - O(n) diff --git a/LeetCode/January/28-1-24/Solution.java b/LeetCode/January/28-1-24/Solution.java new file mode 100644 index 0000000..e6cf26c --- /dev/null +++ b/LeetCode/January/28-1-24/Solution.java @@ -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 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; + } +}