Find N in the given matrix that follows a pattern
Last Updated :
01 Mar, 2022
Given an infinite matrix filled with the natural numbers as shown below:
1 2 4 7 . . .
3 5 8 . . . .
6 9 . . . . .
10 . . . . .
. . . . . . .
Also, given an integer N and the task is to find the row and the column of the integer N in the given matrix.
Examples:
Input: N = 5
Output: 2 2
5 is present in the 2nd row
and the 2nd column.
Input: N = 3
Output: 2 1
Approach: On observing the problem carefully, the row number can be obtained by subtracting first x natural numbers from N such that they satisfy the condition N – (x * (x + 1)) / 2 ? 1 and the resultant value will be the required row number.
To get the corresponding column, add first x natural numbers to 1 such that they satisfy the condition 1 + (y * (y + 1)) / 2 ? A. Subtract this resultant value from N to get the gap between the base and the given value and again subtract the gap from y + 1.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
pair< int , int > solve( int n)
{
int low = 1, high = 1e4, x = n, p = 0;
while (low <= high) {
int mid = (low + high) / 2;
int sum = (mid * (mid + 1)) / 2;
if (x - sum >= 1) {
p = mid;
low = mid + 1;
}
else {
high = mid - 1;
}
}
int start = 1, end = 1e4, y = 1, q = 0;
while (start <= end) {
int mid = (start + end) / 2;
int sum = (mid * (mid + 1)) / 2;
if (y + sum <= n) {
q = mid;
start = mid + 1;
}
else {
end = mid - 1;
}
}
x = x - (p * (p + 1)) / 2;
y = y + (q * (q + 1)) / 2;
int r = x;
int c = q + 1 - n + y;
pair< int , int > ans = { r, c };
return ans;
}
int main()
{
int n = 5;
pair< int , int > p = solve(n);
cout << p.first << " " << p.second;
return 0;
}
|
Java
class GFG
{
static int [] solve( int n)
{
int low = 1 , high = ( int )1e4, x = n, p = 0 ;
while (low <= high)
{
int mid = (low + high) / 2 ;
int sum = (mid * (mid + 1 )) / 2 ;
if (x - sum >= 1 )
{
p = mid;
low = mid + 1 ;
}
else
{
high = mid - 1 ;
}
}
int start = 1 , end = ( int )1e4, y = 1 , q = 0 ;
while (start <= end)
{
int mid = (start + end) / 2 ;
int sum = (mid * (mid + 1 )) / 2 ;
if (y + sum <= n)
{
q = mid;
start = mid + 1 ;
}
else
{
end = mid - 1 ;
}
}
x = x - (p * (p + 1 )) / 2 ;
y = y + (q * (q + 1 )) / 2 ;
int r = x;
int c = q + 1 - n + y;
int ans[] = { r, c };
return ans;
}
public static void main (String[] args)
{
int n = 5 ;
int []p = solve(n);
System.out.println(p[ 0 ] + " " + p[ 1 ]);
}
}
|
Python3
def solve(n):
low = 1
high = 10 * * 4
x, p = n, 0
while (low < = high):
mid = (low + high) / / 2
sum = (mid * (mid + 1 )) / / 2
if (x - sum > = 1 ):
p = mid
low = mid + 1
else :
high = mid - 1
start, end, y, q = 1 , 10 * * 4 , 1 , 0
while (start < = end):
mid = (start + end) / / 2
sum = (mid * (mid + 1 )) / / 2
if (y + sum < = n):
q = mid
start = mid + 1
else :
end = mid - 1
x = x - (p * (p + 1 )) / / 2
y = y + (q * (q + 1 )) / / 2
r = x
c = q + 1 - n + y
return r, c
n = 5
r, c = solve(n)
print (r, c)
|
C#
using System;
class GFG
{
static int [] solve( int n)
{
int low = 1, high = ( int )1e4, x = n, p = 0;
while (low <= high)
{
int mid = (low + high) / 2;
int sum = (mid * (mid + 1)) / 2;
if (x - sum >= 1)
{
p = mid;
low = mid + 1;
}
else
{
high = mid - 1;
}
}
int start = 1, end = ( int )1e4, y = 1, q = 0;
while (start <= end)
{
int mid = (start + end) / 2;
int sum = (mid * (mid + 1)) / 2;
if (y + sum <= n)
{
q = mid;
start = mid + 1;
}
else
{
end = mid - 1;
}
}
x = x - (p * (p + 1)) / 2;
y = y + (q * (q + 1)) / 2;
int r = x;
int c = q + 1 - n + y;
int []ans = {r, c};
return ans;
}
public static void main (String[] args)
{
int n = 5;
int []p = solve(n);
Console.WriteLine(p[0] + " " + p[1]);
}
}
|
Javascript
<script>
function solve(n)
{
let low = 1, high = 1e4, x = n, p = 0;
while (low <= high)
{
let mid = Math.floor((low + high) / 2);
let sum = Math.floor((mid * (mid + 1)) / 2);
if (x - sum >= 1)
{
p = mid;
low = mid + 1;
}
else
{
high = mid - 1;
}
}
let start = 1, end = 1e4, y = 1, q = 0;
while (start <= end)
{
let mid = Math.floor((start + end) / 2);
let sum = Math.floor((mid * (mid + 1)) / 2);
if (y + sum <= n)
{
q = mid;
start = mid + 1;
}
else
{
end = mid - 1;
}
}
x = x - Math.floor((p * (p + 1)) / 2);
y = y + Math.floor((q * (q + 1)) / 2);
let r = x;
let c = q + 1 - n + y;
let ans = [ r, c ];
return ans;
}
let n = 5;
let p = solve(n);
document.write(p[0] + " " + p[1]);
</script>
|
Time Complexity: O(log(N))
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...