Skip to content

Commit

Permalink
Added codes for 28 Jan
Browse files Browse the repository at this point in the history
  • Loading branch information
Tanmay-312 committed Jan 28, 2024
1 parent bff8714 commit fc49db0
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 0 deletions.
92 changes: 92 additions & 0 deletions GeeksForGeeks/January/28-1-24/GFG.java
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[][][];

}
2 changes: 2 additions & 0 deletions GeeksForGeeks/January/28-1-24/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Time complexity - O(k * logn)
Space complexity - O(k * logn)
2 changes: 2 additions & 0 deletions LeetCode/January/28-1-24/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Time complexity - O(m * n^2)
Space complexity - O(n)
34 changes: 34 additions & 0 deletions LeetCode/January/28-1-24/Solution.java
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;
}
}

0 comments on commit fc49db0

Please sign in to comment.