Skip to content

Commit

Permalink
Added codes for 11 March
Browse files Browse the repository at this point in the history
  • Loading branch information
Tanmay-312 committed Mar 11, 2024
1 parent d8f4143 commit 71d0beb
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 0 deletions.
89 changes: 89 additions & 0 deletions GeeksForGeeks/March/11-3-24/GFG.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
//{ Driver Code Starts
//Initial Template for Java

import java.io.*;
import java.util.*;

class GFG {
public static void main(String args[]) throws IOException {
BufferedReader read =
new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(read.readLine());
while (t-- > 0) {
String input[] = read.readLine().split(" ");

int n = Integer.parseInt(input[0]);
int x = Integer.parseInt(input[1]);

int mat1[][] = new int[n][n];

for(int i = 0;i<n;i++){
input = read.readLine().split(" ");
for(int j = 0;j<n;j++){
mat1[i][j] = Integer.parseInt(input[j]);
}
}

int mat2[][] = new int[n][n];

for(int i = 0;i<n;i++){
input = read.readLine().split(" ");
for(int j = 0;j<n;j++){
mat2[i][j] = Integer.parseInt(input[j]);
}
}



Solution ob = new Solution();
System.out.println(ob.countPairs(mat1,mat2,n,x));
}
}
}
// } Driver Code Ends


//User function Template for Java

class Solution
{
int countPairs(int mat1[][], int mat2[][], int n, int x)
{
// code here
int r1 = 0, c1 = 0;
int r2 = n - 1, c2 = n - 1;

int count = 0;
while ((r1 < n) && (r2 >= 0))
{
int val = mat1[r1][c1] + mat2[r2][c2];

if (val == x)
{
count++;
c1++;
c2--;
}

else if (val < x)
c1++;

else
c2--;

if (c1 == n)
{
c1 = 0;
r1++;
}

if (c2 == -1)
{
c2 = n - 1;
r2--;
}
}

return count;
}
}
2 changes: 2 additions & 0 deletions GeeksForGeeks/March/11-3-24/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Time complexity - O(n^2)
Space complexity - O(1)
2 changes: 2 additions & 0 deletions LeetCode/March/11-3-24/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Time complexity - O(|order| + |s|)
Space complexity - O(26) = O(1)
21 changes: 21 additions & 0 deletions LeetCode/March/11-3-24/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
class Solution
{
public String customSortString(String order, String s)
{
StringBuilder sb = new StringBuilder();
int[] count = new int[128];

for (char c : s.toCharArray())
++count[c];

for (char c : order.toCharArray())
while (count[c]-- > 0)
sb.append(c);

for (char c = 'a'; c <= 'z'; ++c)
while (count[c]-- > 0)
sb.append(c);

return sb.toString();
}
}

0 comments on commit 71d0beb

Please sign in to comment.