给定一个由N 个整数组成的数组A[] ,任务是找到K的最大可能值,使得K * |i – j| <= min(A i , A j ) ,其中 (0 ? i < j < N)。
Given expression, k * |i – j| <= min(Ai, Aj) can also be written as k = floor( min(Ai, Aj) / |i – j| )
例子:
Input: N = 5, A[ ] = {80, 10, 12, 15, 90}
Output: 20
Explanation:
For i = 0 and j = 4, the maximum possible value of K can be obtained.
Maximum k = min(A[0], A[4]) / |0 – 4| = min(80, 90) / |-4| = 80/4 = 20
Input: N = 5, A[ ] = {10, 5, 12, 15, 8}
Output: 12
Explanation:
For i = 2 and j = 3, the maximum possible value of K can be obtained.
Maximum k = min(A[2], A[3]) / |2 – 3| = min(12, 15) / |-1| = 12/1 = 12
天真的方法:
解决这个问题最简单的方法是从给定的数组中生成所有可能的对,对于每一对,找到 K 的值并不断更新获得的最大K。最后,打印得到的K的最大值。
时间复杂度: O(N 2 )
辅助空间: O(1)
有效的方法:
要优化上述方法,请使用两个指针技术。请按照以下步骤操作:
- 初始化三个变量i 、 j和k 。最初设置i = 0和k = 0 。
- 迭代数组,从j = 1到j = N-1 。
- 现在,对于每对A[i]和A[j] ,找到min(A[i], A[j]) / ( j – i )。如果它大于K ,则更新K 。
- 如果A[ j ] >= A[i] / ( j – i ) ,则将指针i更新为j ,以确保在直到i 的所有元素中, A[i]将导致最大K与所有即将到来的A[ j]
- 最后,遍历完数组后打印K的最大值。
下面是上述方法的实现:
C++
// C++ program to implement
// the above appraoch
#include
using namespace std;
// Function returns maximum
// possible value of k
int solve(int A[], int N)
{
// Pointer i make sure that
// A[i] will result in max k
int i = 0;
// Stores maximum possible k
int k = 0;
for (int j = 1; j < N; j++) {
// Possible value of k for
// current pair (A[i] and A[j])
int tempK = min(A[i], A[j])
/ (j - i);
// If current value exceeds k
if (tempK > k) {
// Update the value of k
k = tempK;
}
// Update pointer i
if (A[j] >= A[i] / (j - i))
i = j;
}
// Return the maxm possible k
return k;
}
// Driver Code
int main()
{
int A[] = { 10, 5, 12, 15, 8 };
int N = sizeof(A) / sizeof(A[0]);
cout << solve(A, N);
return 0;
}
Java
// Java program to implement
// the above appraoch
class GFG{
// Function returns maximum
// possible value of k
static int solve(int A[], int N)
{
// Pointer i make sure that
// A[i] will result in max k
int i = 0;
// Stores maximum possible k
int k = 0;
for(int j = 1; j < N; j++)
{
// Possible value of k for
// current pair (A[i] and A[j])
int tempK = Math.min(A[i], A[j]) /
(j - i);
// If current value exceeds k
if (tempK > k)
{
// Update the value of k
k = tempK;
}
// Update pointer i
if (A[j] >= A[i] / (j - i))
i = j;
}
// Return the maxm possible k
return k;
}
// Driver Code
public static void main(String[] args)
{
int A[] = { 10, 5, 12, 15, 8 };
int N = A.length;
System.out.println(solve(A, N));
}
}
// This code is contributed by rutvik_56
Python3
# Python3 program to implement
# the above appraoch
# Function returns maximum
# possible value of k
def solve(A, N):
# Pointer i make sure that
# A[i] will result in max k
i = 0
# Stores maximum possible k
k = 0
for j in range(1, N):
# Possible value of k for
# current pair (A[i] and A[j])
tempK = (min(A[i], A[j]) // (j - i))
# If current value exceeds k
if (tempK > k):
# Update the value of k
k = tempK
# Update pointer i
if (A[j] >= A[i] // (j - i)):
i = j
# Return the maxm possible k
return k
# Driver Code
if __name__ == "__main__":
A = [ 10, 5, 12, 15, 8 ]
N = len(A);
print(solve(A, N))
# This code is contributed by chitranayal
C#
// C# program to implement
// the above appraoch
using System;
class GFG{
// Function returns maximum
// possible value of k
static int solve(int[] A, int N)
{
// Pointer i make sure that
// A[i] will result in max k
int i = 0;
// Stores maximum possible k
int k = 0;
for(int j = 1; j < N; j++)
{
// Possible value of k for
// current pair (A[i] and A[j])
int tempK = Math.Min(A[i], A[j]) /
(j - i);
// If current value exceeds k
if (tempK > k)
{
// Update the value of k
k = tempK;
}
// Update pointer i
if (A[j] >= A[i] / (j - i))
i = j;
}
// Return the maxm possible k
return k;
}
// Driver Code
public static void Main(string[] args)
{
int[] A = { 10, 5, 12, 15, 8 };
int N = A.Length;
Console.Write(solve(A, N));
}
}
// This code is contributed by rock_cool
Javascript
12
时间复杂度: O(N)
辅助空间: O(1)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live