Geeksforgeeks

Error

×

Leaderboard

Showing:

Handle | Score |
---|---|

@Ibrahim Nash | 5725 |

@blackshadows | 5685 |

@akhayrutdinov | 5111 |

@mb1973 | 4989 |

@Quandray | 4908 |

@saiujwal13083 | 4156 |

@sanjay05 | 3762 |

@marius_valentin_dragoi | 3511 |

@sushant_a | 3453 |

@KshamaGupta | 3318 |

@the_coder95 | 3302 |

Complete Leaderboard | |

Handle | Score |

@avaiesshaik782 | 662 |

@arpitsatnalika | 629 |

@code_it_ | 591 |

@prakharxyz | 583 |

@tujhekya | 542 |

@Rahul Jadhav 1 | 458 |

@sivanandh03 | 448 |

@satyam0460 | 429 |

@iamronak | 424 |

@imnm | 424 |

@18eumc107 | 421 |

Complete Leaderboard |

Given two sorted matrices **mat1** and **mat2** of size **N x N** of elements. Given a value **x**. The problem is to count all pairs from both matrices whose sum is equal to **x**.

**Note:** The pair has an element from each matrix. Matrices are strictly sorted which means that matrices are sorted in a way such that all elements in a row are sorted in increasing order and for row ‘i’, where 1 <= i <= n-1, the first element of row 'i' is greater than the last element of row 'i-1'.

**Example 1:**

**Input:**
N = 3, x = 21
mat1 = { {1 5 6}
{8 10 11}
{15 16 18} }
mat2 = { {2 4 7}
{9 10 12}
{13 16 20} }
**OUTPUT: **4
**Explanation: **The pairs are (1, 20),
(5, 16), (8, 13), (11, 10).

**Example 2:**

**Input:
**N = 2, x = 10
mat1 = { {1 2}
{3 4} }
mat2 = { {4 5}
{6 7}
**Output: **2
**Explanation: **The pairs are (4, 6), (3, 7)

**User Task:**

Your task is to complete the function `countPairs()`

** **which takes 4 arguments **mat1**, **mat2**, **N**, **X**, and returns the count of pairs. You don't need to take any input or print anything.

**Expected Time Complexity:** O(N^{2})

**Expected Auxiliary Space:** O(1)

**Constraints:**

1 ≤ T ≤ 100

1 ≤ N ≤ 100

1 ≤ X ≤ 10^{5}

1 ≤ mat1[i][j] , mat2[i][j] ≤ 10^{5}

Login to report an issue on this page.

We strongly recommend solving this problem on your own before viewing its editorial. Do you still want to view the editorial?

Yes
Count pairs Sum in matrices

...

Please choose 'ReadOnlyMode' if you needn't to 'Edit' the problem e.g. viewing OJ's solution, TestCase Files (TCFs), TimeLimit etc.