`{"id":"0f585e1a-16ae-4230-9fda-0a6e20e39d09","name":"Pairs With Given Sum In Two Sorted Matrices","description":"1. You are given a number N and two sorted matrices(A and B) of N*N dimensions.\r\n2. You are also given a number X.\r\n3. You have to find the count of all valid pairs from matrices whose sum is equal to X.\r\n4. A pair is called valid if one element of the pair is selected from A and the second element is selected from B.","inputFormat":"A number N\r\nA1\r\nA2.. N*N numbers\r\nB1\r\nB2.. N*N numbers","outputFormat":"A number representing the count of all valid pairs.","constraints":"1 &lt;= N &lt;= 10^3\r\n-10^3 &lt;= elements of matrices &lt;= 10^3","sampleCode":{"cpp":{"code":"#include <iostream>\r\n#include <vector>\r\n#include <unordered_map>\r\n\r\nusing namespace std;\r\n\r\nint solve(vector<vector<int>> &nums1, vector<vector<int>> &nums2, int k)\r\n{\r\n\r\n return 0;\r\n}\r\n\r\nvoid input2d(vector<vector<int>> &arr)\r\n{\r\n for (int i = 0; i < arr.size(); i++)\r\n for (int j = 0; j < arr.size(); j++)\r\n cin >> arr[i][j];\r\n}\r\n\r\n\r\nint main()\r\n{\r\n int n;\r\n cin >> n;\r\n\r\n vector<vector<int>> arr1(n, vector<int>(n, 0));\r\n input2d(arr1);\r\n\r\n vector<vector<int>> arr2(n, vector<int>(n, 0));\r\n input2d(arr2);\r\n\r\n int k;\r\n cin >> k;\r\n\r\n return 0;\r\n}\r\n"},"java":{"code":"import java.util.*;\r\n\r\npublic class Main {\r\n\t\r\n public static int solve(int[][] num1, int[][] num2, int k) {\r\n\t\t// write your code here\r\n\r\n\t\treturn 0;\r\n\t}\r\n\r\n\tpublic static void main(String[] args) {\r\n\t\tScanner sc = new Scanner(System.in);\r\n\t\tint N = sc.nextInt();\r\n\t\tint[][] mat1 = new int[N][N];\r\n\t\tfor (int i = 0; i < mat1.length; i++) {\r\n\t\t\tfor (int j = 0; j < mat1.length; j++) {\r\n\t\t\t\tmat1[i][j] = sc.nextInt();\r\n\t\t\t}\r\n\t\t}\r\n\r\n\t\tint[][] mat2 = new int[N][N];\r\n\t\tfor (int i = 0; i < mat2.length; i++) {\r\n\t\t\tfor (int j = 0; j < mat2.length; j++) {\r\n\t\t\t\tmat2[i][j] = sc.nextInt();\r\n\t\t\t}\r\n\t\t}\r\n\t\tint K = sc.nextInt();\r\n\t\tSystem.out.println(solve(mat1, mat2, K));\r\n\r\n\t}\r\n\r\n}\r\n"},"python":{"code":""}},"points":10,"difficulty":"easy","sampleInput":"3\r\n1 5 6\r\n8 10 11\r\n15 16 18\r\n2 4 7\r\n9 10 12\r\n13 16 20\r\n21","sampleOutput":"4\r\n","questionVideo":"","hints":[],"associated":[],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"ec6515dc-6811-4c65-912f-0134b65df120","name":"HashMap and Heap For Intermediate","slug":"hashmap-and-heap-for-intermediate-10002","type":0},{"id":"902a6a5f-63f4-414e-8b8b-2bc451f02d90","name":"Pairs With Given Sum In Two Sorted Matrices","slug":"pairs-with-given-sum-in-two-sorted-matrices","type":1}],"next":{"id":"96f0e81e-9797-4005-aa62-e09c3177ff30","name":"Pairs With Given Sum In Two Sorted Matrices MCQ","type":0,"slug":"pairs-with-given-sum-in-two-sorted-matrices-mcq"},"prev":{"id":"f0d65fb1-3113-4bc2-86e3-56c55975df37","name":"Task Completion","type":3,"slug":"task-completion"}}}`

# Pairs With Given Sum In Two Sorted Matrices

1. You are given a number N and two sorted matrices(A and B) of N*N dimensions. 2. You are also given a number X. 3. You have to find the count of all valid pairs from matrices whose sum is equal to X. 4. A pair is called valid if one element of the pair is selected from A and the second element is selected from B.

`{"id":"0f585e1a-16ae-4230-9fda-0a6e20e39d09","name":"Pairs With Given Sum In Two Sorted Matrices","description":"1. You are given a number N and two sorted matrices(A and B) of N*N dimensions.\r\n2. You are also given a number X.\r\n3. You have to find the count of all valid pairs from matrices whose sum is equal to X.\r\n4. A pair is called valid if one element of the pair is selected from A and the second element is selected from B.","inputFormat":"A number N\r\nA1\r\nA2.. N*N numbers\r\nB1\r\nB2.. N*N numbers","outputFormat":"A number representing the count of all valid pairs.","constraints":"1 &lt;= N &lt;= 10^3\r\n-10^3 &lt;= elements of matrices &lt;= 10^3","sampleCode":{"cpp":{"code":"#include <iostream>\r\n#include <vector>\r\n#include <unordered_map>\r\n\r\nusing namespace std;\r\n\r\nint solve(vector<vector<int>> &nums1, vector<vector<int>> &nums2, int k)\r\n{\r\n\r\n return 0;\r\n}\r\n\r\nvoid input2d(vector<vector<int>> &arr)\r\n{\r\n for (int i = 0; i < arr.size(); i++)\r\n for (int j = 0; j < arr.size(); j++)\r\n cin >> arr[i][j];\r\n}\r\n\r\n\r\nint main()\r\n{\r\n int n;\r\n cin >> n;\r\n\r\n vector<vector<int>> arr1(n, vector<int>(n, 0));\r\n input2d(arr1);\r\n\r\n vector<vector<int>> arr2(n, vector<int>(n, 0));\r\n input2d(arr2);\r\n\r\n int k;\r\n cin >> k;\r\n\r\n return 0;\r\n}\r\n"},"java":{"code":"import java.util.*;\r\n\r\npublic class Main {\r\n\t\r\n public static int solve(int[][] num1, int[][] num2, int k) {\r\n\t\t// write your code here\r\n\r\n\t\treturn 0;\r\n\t}\r\n\r\n\tpublic static void main(String[] args) {\r\n\t\tScanner sc = new Scanner(System.in);\r\n\t\tint N = sc.nextInt();\r\n\t\tint[][] mat1 = new int[N][N];\r\n\t\tfor (int i = 0; i < mat1.length; i++) {\r\n\t\t\tfor (int j = 0; j < mat1.length; j++) {\r\n\t\t\t\tmat1[i][j] = sc.nextInt();\r\n\t\t\t}\r\n\t\t}\r\n\r\n\t\tint[][] mat2 = new int[N][N];\r\n\t\tfor (int i = 0; i < mat2.length; i++) {\r\n\t\t\tfor (int j = 0; j < mat2.length; j++) {\r\n\t\t\t\tmat2[i][j] = sc.nextInt();\r\n\t\t\t}\r\n\t\t}\r\n\t\tint K = sc.nextInt();\r\n\t\tSystem.out.println(solve(mat1, mat2, K));\r\n\r\n\t}\r\n\r\n}\r\n"},"python":{"code":""}},"points":10,"difficulty":"easy","sampleInput":"3\r\n1 5 6\r\n8 10 11\r\n15 16 18\r\n2 4 7\r\n9 10 12\r\n13 16 20\r\n21","sampleOutput":"4\r\n","questionVideo":"","hints":[],"associated":[],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"ec6515dc-6811-4c65-912f-0134b65df120","name":"HashMap and Heap For Intermediate","slug":"hashmap-and-heap-for-intermediate-10002","type":0},{"id":"902a6a5f-63f4-414e-8b8b-2bc451f02d90","name":"Pairs With Given Sum In Two Sorted Matrices","slug":"pairs-with-given-sum-in-two-sorted-matrices","type":1}],"next":{"id":"96f0e81e-9797-4005-aa62-e09c3177ff30","name":"Pairs With Given Sum In Two Sorted Matrices MCQ","type":0,"slug":"pairs-with-given-sum-in-two-sorted-matrices-mcq"},"prev":{"id":"f0d65fb1-3113-4bc2-86e3-56c55975df37","name":"Task Completion","type":3,"slug":"task-completion"}}}` Editor

# Pairs With Given Sum In Two Sorted Matrices

easy

1. You are given a number N and two sorted matrices(A and B) of N*N dimensions. 2. You are also given a number X. 3. You have to find the count of all valid pairs from matrices whose sum is equal to X. 4. A pair is called valid if one element of the pair is selected from A and the second element is selected from B.

## Constraints

1 <= N <= 10^3 -10^3 <= elements of matrices <= 10^3

## Format

### Input

A number N A1 A2.. N*N numbers B1 B2.. N*N numbers

### Output

A number representing the count of all valid pairs.

## Example

Sample Input

```.css-23h8hz{color:inherit;font-size:0.875rem;line-height:1.125rem;letter-spacing:0.016rem;font-weight:var(--chakra-fontWeights-normal);white-space:pre-wrap;}3 1 5 6 8 10 11 15 16 18 2 4 7 9 10 12 13 16 20 21```

### Sample Output

```.css-3oaykw{color:var(--chakra-colors-active-primary);font-size:0.875rem;line-height:1.125rem;letter-spacing:0.016rem;font-weight:var(--chakra-fontWeights-normal);white-space:pre-wrap;font-family:Monospace;}4 ```

Discussions

Show Discussion

Related Resources 