Given some rules to generate an **N * N** matrix **mat[][]** and two integers **R** and **C**, the task is to find the element at the **R ^{th}** row and

**C**column. The rules are as follows:

^{th}- First row is an AP series starting with
**1**and**d = 1**(Common Difference). - For all the element at (i, j) such that
**i > j**, their value is**0**. - Rest of the elements follow,
**Element(i, j) = Element(i – 1, j) + Element(i – 1, j – 1)**.

**Examples:**

Input:N = 4, R = 3, C = 4

Output:12

mat[][] =

{1, 2, 3, 4},

{0, 3, 5, 7},

{0, 0, 8, 12},

{0, 0, 0, 20}

and the element in the third row and fourth column is 12.

Input:N = 2, R = 2, C = 2

Output:3

**Approach:**

- Basic Key is to observe the pattern, first observation is that every row will have an AP after the element at
**(i, i)**. Common difference for row number**j**is**pow(2, j – 1)**. - Now we need to find the first term of each row to find any element
**(R, C)**. If we consider only starting elements in each row (i.e element at diagonal), we can observe it is equal to**(R + 1) * pow(2, R – 2)**for every row**R**from**2**to**N**. - So, If
**R > C**then element is**0**else**C – R**is the position of required element in AP which is present in the**R**row for which we already know the starting term and the common difference. So, we can find element as^{th}**start + d * (C – R)**.

Below is the implementation of the above approach:

## C++

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to return the element in the rth row ` `// and cth column from the required matrix ` `int` `getElement(` `int` `N, ` `int` `r, ` `int` `c) ` `{ ` ` ` ` ` `// Condition for lower half of matrix ` ` ` `if` `(r > c) ` ` ` `return` `0; ` ` ` ` ` `// Condition if element is in first row ` ` ` `if` `(r == 1) { ` ` ` `return` `c; ` ` ` `} ` ` ` ` ` `// Starting element of AP in row r ` ` ` `int` `a = (r + 1) * ` `pow` `(2, r - 2); ` ` ` ` ` `// Common difference of AP in row r ` ` ` `int` `d = ` `pow` `(2, r - 1); ` ` ` ` ` `// Position of element to find ` ` ` `// in AP in row r ` ` ` `c = c - r; ` ` ` ` ` `int` `element = a + d * c; ` ` ` ` ` `return` `element; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `int` `N = 4, R = 3, C = 4; ` ` ` ` ` `cout << getElement(N, R, C); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the above approach ` `import` `java.io.*; ` `import` `java.util.*; ` ` ` `class` `GFG ` `{ ` ` ` `// Function to return the element ` `// in the rth row and cth column ` `// from the required matrix ` `static` `int` `getElement(` `int` `N, ` `int` `r, ` `int` `c) ` `{ ` ` ` ` ` `// Condition for lower half of matrix ` ` ` `if` `(r > c) ` ` ` `return` `0` `; ` ` ` ` ` `// Condition if element is in first row ` ` ` `if` `(r == ` `1` `) ` ` ` `{ ` ` ` `return` `c; ` ` ` `} ` ` ` ` ` `// Starting element of AP in row r ` ` ` `int` `a = (r + ` `1` `) * (` `int` `)(Math.pow(` `2` `,(r - ` `2` `))); ` ` ` ` ` `// Common difference of AP in row r ` ` ` `int` `d = (` `int` `)(Math.pow(` `2` `,(r - ` `1` `))); ` ` ` ` ` `// Position of element to find ` ` ` `// in AP in row r ` ` ` `c = c - r; ` ` ` ` ` `int` `element = a + d * c; ` ` ` ` ` `return` `element; ` `} ` ` ` `// Driver Code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `int` `N = ` `4` `, R = ` `3` `, C = ` `4` `; ` ` ` ` ` `System.out.println(getElement(N, R, C)); ` `} ` `} ` ` ` `// This code is contributed by Krikti ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of the approach ` ` ` `# Function to return the element in the rth row ` `# and cth column from the required matrix ` `def` `getElement(N, r, c) : ` ` ` ` ` `# Condition for lower half of matrix ` ` ` `if` `(r > c) : ` ` ` `return` `0` `; ` ` ` ` ` `# Condition if element is in first row ` ` ` `if` `(r ` `=` `=` `1` `) : ` ` ` `return` `c; ` ` ` ` ` ` ` `# Starting element of AP in row r ` ` ` `a ` `=` `(r ` `+` `1` `) ` `*` `pow` `(` `2` `, r ` `-` `2` `); ` ` ` ` ` `# Common difference of AP in row r ` ` ` `d ` `=` `pow` `(` `2` `, r ` `-` `1` `); ` ` ` ` ` `# Position of element to find ` ` ` `# in AP in row r ` ` ` `c ` `=` `c ` `-` `r; ` ` ` ` ` `element ` `=` `a ` `+` `d ` `*` `c; ` ` ` ` ` `return` `element; ` ` ` ` ` `# Driver Code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `N ` `=` `4` `; R ` `=` `3` `; C ` `=` `4` `; ` ` ` ` ` `print` `(getElement(N, R, C)); ` ` ` `# This Code is contributed by AnkitRai01 ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the above approach ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` `// Function to return the element ` `// in the rth row and cth column ` `// from the required matrix ` `static` `int` `getElement(` `int` `N, ` `int` `r, ` `int` `c) ` `{ ` ` ` ` ` `// Condition for lower half of matrix ` ` ` `if` `(r > c) ` ` ` `return` `0; ` ` ` ` ` `// Condition if element is in first row ` ` ` `if` `(r == 1) ` ` ` `{ ` ` ` `return` `c; ` ` ` `} ` ` ` ` ` `// Starting element of AP in row r ` ` ` `int` `a = (r + 1) * (` `int` `)(Math.Pow(2,(r - 2))); ` ` ` ` ` `// Common difference of AP in row r ` ` ` `int` `d = (` `int` `)(Math.Pow(2,(r - 1))); ` ` ` ` ` `// Position of element to find ` ` ` `// in AP in row r ` ` ` `c = c - r; ` ` ` ` ` `int` `element = a + d * c; ` ` ` ` ` `return` `element; ` `} ` ` ` `// Driver Code ` `public` `static` `void` `Main(String[] args) ` `{ ` ` ` `int` `N = 4, R = 3, C = 4; ` ` ` ` ` `Console.WriteLine(getElement(N, R, C)); ` `} ` `} ` ` ` `// This code is contributed by Princi Singh ` |

*chevron_right*

*filter_none*

**Output:**

12

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.