Given two integers **R** and **C**, the task is to find the element at the R^{th} row and C^{th} column.

**Pattern:**

- First Element of i
^{th}row =- Every element is a Arthmetic progression increasing difference where common difference is 1.
- Intial Difference Term =

**Examples:**

Input:R = 4, C = 4

Output:25

Explanation:

Pattern of size 4 * 4 is –

1 3 6 10

2 5 9 14

4 8 13 19

7 12 18 25

Therefore, Element at Pat[4][4] = 25

Input:R = 3, C = 3

Output:13

Explanation:

Pattern of size 3 * 3 is –

1 3 6

2 5 9

4 8 13

Therefore, element at Pat[3][3] = 13

**Naive Approach:** A simple solution is to generate the pattern matrix of size R * C and then finally return the element at the R^{th} row and C^{th} column.

**Time Complexity:** O(R*C)

**Auxiliary Space:** O(R*C)

**Efficient Approach:** The idea is to find the first term of the R^{th} row using the formulae and then finally compute the C^{th} term of that column using the help of loop.

Below is the implementation of the above approach:

## C++

`// C++ implementation to compute the ` `// R'th row and C'th column of the ` `// given pattern ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to compute the ` `// R'th row and C'th column of the ` `// given pattern ` `int` `findValue(` `int` `R, ` `int` `C) ` `{ ` ` ` ` ` `// First element of a given row ` ` ` `int` `k = (R * (R - 1)) / 2 + 1; ` ` ` ` ` `int` `diff = R + 1; ` ` ` ` ` `// Element in the given column ` ` ` `for` `(` `int` `i = 1; i < C; i++) { ` ` ` `k = (k + diff); ` ` ` `diff++; ` ` ` `} ` ` ` ` ` `return` `k; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `int` `R = 4; ` ` ` `int` `C = 4; ` ` ` ` ` `// Function call ` ` ` `int` `k = findValue(R, C); ` ` ` ` ` `cout << k; ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation to compute the ` `// R'th row and C'th column of the ` `// given pattern ` `import` `java.io.*; ` ` ` `class` `GFG{ ` ` ` `// Function to compute the R'th ` `// row and C'th column of the ` `// given pattern ` `static` `int` `findValue(` `int` `R, ` `int` `C) ` `{ ` ` ` ` ` `// First element of a given row ` ` ` `int` `k = (R * (R - ` `1` `)) / ` `2` `+ ` `1` `; ` ` ` ` ` `int` `diff = R + ` `1` `; ` ` ` ` ` `// Element in the given column ` ` ` `for` `(` `int` `i = ` `1` `; i < C; i++) ` ` ` `{ ` ` ` `k = (k + diff); ` ` ` `diff++; ` ` ` `} ` ` ` `return` `k; ` `} ` ` ` `// Driver code ` `public` `static` `void` `main (String[] args) ` `{ ` ` ` `int` `R = ` `4` `; ` ` ` `int` `C = ` `4` `; ` ` ` ` ` `// Function call ` ` ` `int` `k = findValue(R, C); ` ` ` ` ` `System.out.println(k); ` `} ` `} ` ` ` `// This code is contributed by mohit kumar 29 ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation to find the ` `# R'th row and C'th column value in ` `# the given pattern ` ` ` `# Function to find the ` `# R'th row and C'th column value in ` `# the given pattern ` `def` `findValue(R, C): ` ` ` ` ` `# First element of a given row ` ` ` `k ` `=` `(R` `*` `(R` `-` `1` `))` `/` `/` `2` `+` `1` ` ` ` ` `diff ` `=` `R ` `+` `1` ` ` ` ` `# Element in the given column ` ` ` `for` `i ` `in` `range` `(` `1` `, C): ` ` ` `k ` `=` `(k ` `+` `diff) ` ` ` `diff` `+` `=` `1` ` ` ` ` `return` `k ` ` ` `# Driver Code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` `R ` `=` `4` ` ` `C ` `=` `4` ` ` ` ` `k ` `=` `findValue(R, C) ` ` ` `print` `(k) ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation to compute the ` `// R'th row and C'th column of the ` `// given pattern ` `using` `System; ` `class` `GFG{ ` ` ` `// Function to compute the R'th ` `// row and C'th column of the ` `// given pattern ` `static` `int` `findValue(` `int` `R, ` `int` `C) ` `{ ` ` ` ` ` `// First element of a given row ` ` ` `int` `k = (R * (R - 1)) / 2 + 1; ` ` ` ` ` `int` `diff = R + 1; ` ` ` ` ` `// Element in the given column ` ` ` `for` `(` `int` `i = 1; i < C; i++) ` ` ` `{ ` ` ` `k = (k + diff); ` ` ` `diff++; ` ` ` `} ` ` ` `return` `k; ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main() ` `{ ` ` ` `int` `R = 4; ` ` ` `int` `C = 4; ` ` ` ` ` `// Function call ` ` ` `int` `k = findValue(R, C); ` ` ` ` ` `Console.Write(k); ` `} ` `} ` ` ` `// This code is contributed by Code_Mech ` |

*chevron_right*

*filter_none*

**Output:**

25

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Find the original matrix when largest element in a row and a column are given
- Construct a matrix such that union of ith row and ith column contains every element from 1 to 2N-1
- Kth smallest element in a row-wise and column-wise sorted 2D array | Set 1
- Find if a binary matrix exists with given row and column sums
- Find trace of matrix formed by adding Row-major and Column-major order of same matrix
- Find an N x N grid whose xor of every row and column is equal
- Find a Square Matrix such that sum of elements in every row and column is K
- Find all matrix elements which are minimum in their row and maximum in their column
- Count zeros in a row wise and column wise sorted matrix
- Number of positions with Same address in row major and column major order
- Count of ways to generate a Matrix with product of each row and column as 1 or -1
- Maximum sum of any submatrix of a Matrix which is sorted row-wise and column-wise
- Check if it is possible to create a matrix such that every row has A 1s and every column has B 1s
- Count positions in Binary Matrix having equal count of set bits in corresponding row and column
- Count 1s in binary matrix having remaining indices of its row and column filled with 0s
- Search in a row wise and column wise sorted matrix
- Ways to place 4 items in n^2 positions such that no row/column contains more than one
- Leftmost Column with atleast one 1 in a row-wise sorted binary matrix
- Leftmost Column with atleast one 1 in a row-wise sorted binary matrix | Set 2
- Find maximum element of each row in a matrix

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.