给定一个充满自然数的无限矩阵,如下所示:
1 2 4 7 . . .
3 5 8 . . . .
6 9 . . . . .
10 . . . . .
. . . . . . .
此外,给定的整数N和任务是找到行和整数N的在给定的矩阵的列。
例子:
Input: N = 5
Output: 2 2
5 is present in the 2nd row
and the 2nd column.
Input: N = 3
Output: 2 1
方法:仔细观察问题,可以通过从N 中减去前x 个自然数,使得它们满足条件N – (x * (x + 1)) / 2 ≥ 1得到行号,结果值为所需的行号。
要获得相应的列,请将前x 个自然数与1相加,使它们满足条件1 + (y * (y + 1)) / 2 ≤ A 。从N 中减去这个结果值以获得基数和给定值之间的差距,并再次从y + 1 中减去差距。
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
using namespace std;
// Function to return the row and
// the column of the given integer
pair solve(int n)
{
int low = 1, high = 1e4, x = n, p = 0;
// Binary search for the row number
while (low <= high) {
int mid = (low + high) / 2;
int sum = (mid * (mid + 1)) / 2;
// Condition to get the maximum
// x that satisfies the criteria
if (x - sum >= 1) {
p = mid;
low = mid + 1;
}
else {
high = mid - 1;
}
}
int start = 1, end = 1e4, y = 1, q = 0;
// Binary search for the column number
while (start <= end) {
int mid = (start + end) / 2;
int sum = (mid * (mid + 1)) / 2;
// Condition to get the maximum
// y that satisfies the criteria
if (y + sum <= n) {
q = mid;
start = mid + 1;
}
else {
end = mid - 1;
}
}
// Get the row and the column number
x = x - (p * (p + 1)) / 2;
y = y + (q * (q + 1)) / 2;
int r = x;
int c = q + 1 - n + y;
// Return the pair
pair ans = { r, c };
return ans;
}
// Driver code
int main()
{
int n = 5;
pair p = solve(n);
cout << p.first << " " << p.second;
return 0;
}
Java
// Java implementation of the approach
class GFG
{
// Function to return the row and
// the column of the given integer
static int[] solve(int n)
{
int low = 1, high = (int)1e4, x = n, p = 0;
// Binary search for the row number
while (low <= high)
{
int mid = (low + high) / 2;
int sum = (mid * (mid + 1)) / 2;
// Condition to get the maximum
// x that satisfies the criteria
if (x - sum >= 1)
{
p = mid;
low = mid + 1;
}
else
{
high = mid - 1;
}
}
int start = 1, end = (int)1e4, y = 1, q = 0;
// Binary search for the column number
while (start <= end)
{
int mid = (start + end) / 2;
int sum = (mid * (mid + 1)) / 2;
// Condition to get the maximum
// y that satisfies the criteria
if (y + sum <= n)
{
q = mid;
start = mid + 1;
}
else
{
end = mid - 1;
}
}
// Get the row and the column number
x = x - (p * (p + 1)) / 2;
y = y + (q * (q + 1)) / 2;
int r = x;
int c = q + 1 - n + y;
// Return the pair
int ans[] = { r, c };
return ans;
}
// Driver code
public static void main (String[] args)
{
int n = 5;
int []p = solve(n);
System.out.println(p[0] + " " + p[1]);
}
}
// This code is contributed by AnkitRai01
Python3
# Python3 implementation of the approach
# Function to return the row and
# the column of the given integer
def solve(n):
low = 1
high = 10**4
x, p = n, 0
# Binary search for the row number
while (low <= high):
mid = (low + high) // 2
sum = (mid * (mid + 1)) // 2
# Condition to get the maximum
# x that satisfies the criteria
if (x - sum >= 1):
p = mid
low = mid + 1
else :
high = mid - 1
start, end, y, q = 1, 10**4, 1, 0
# Binary search for the column number
while (start <= end):
mid = (start + end) // 2
sum = (mid * (mid + 1)) // 2
# Condition to get the maximum
# y that satisfies the criteria
if (y + sum <= n):
q = mid
start = mid + 1
else:
end = mid - 1
# Get the row and the column number
x = x - (p * (p + 1)) // 2
y = y + (q * (q + 1)) // 2
r = x
c = q + 1 - n + y
# Return the pair
return r, c
# Driver code
n = 5
r, c = solve(n)
print(r, c)
# This code is contributed by Mohit Kumar
C#
// C# implementation of the approach
using System;
class GFG
{
// Function to return the row and
// the column of the given integer
static int[] solve(int n)
{
int low = 1, high = (int)1e4, x = n, p = 0;
// Binary search for the row number
while (low <= high)
{
int mid = (low + high) / 2;
int sum = (mid * (mid + 1)) / 2;
// Condition to get the maximum
// x that satisfies the criteria
if (x - sum >= 1)
{
p = mid;
low = mid + 1;
}
else
{
high = mid - 1;
}
}
int start = 1, end = (int)1e4, y = 1, q = 0;
// Binary search for the column number
while (start <= end)
{
int mid = (start + end) / 2;
int sum = (mid * (mid + 1)) / 2;
// Condition to get the maximum
// y that satisfies the criteria
if (y + sum <= n)
{
q = mid;
start = mid + 1;
}
else
{
end = mid - 1;
}
}
// Get the row and the column number
x = x - (p * (p + 1)) / 2;
y = y + (q * (q + 1)) / 2;
int r = x;
int c = q + 1 - n + y;
// Return the pair
int []ans = {r, c};
return ans;
}
// Driver code
public static void main (String[] args)
{
int n = 5;
int []p = solve(n);
Console.WriteLine(p[0] + " " + p[1]);
}
}
// This code is contributed by PrinciRaj1992
Javascript
输出:
2 2
时间复杂度: O(log(N))
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。