给定一个由N个整数组成的数组A [] ,任务是找到K的最大可能值,使得K * | i – j | <= min(A i ,A j ) ,其中(0?i 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} Input: N = 5, A[ ] = {10, 5, 12, 15, 8} 天真的方法: 高效方法: 下面是上述方法的实现: 时间复杂度: O(N)
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
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)
要优化上述方法,请使用“两个指针”技术。请按照以下步骤操作:
C++
// C++ program to implement
// the above appraoch
#include
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(1)