-
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
d8f4143
commit 71d0beb
Showing
4 changed files
with
114 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,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; | ||
} | ||
} |
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(n^2) | ||
Space complexity - O(1) |
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(|order| + |s|) | ||
Space complexity - O(26) = O(1) |
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,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(); | ||
} | ||
} |