📜  用K最大化绝对指数差的乘积

📅  最后修改于: 2021-05-07 00:05:07             🧑  作者: Mango

给定一个由N个整数组成的数组A [] ,任务是找到K的最大可能值,使得K * | i – j | <= min(A i ,A j ) ,其中(0?i

例子:

天真的方法:
解决此问题的最简单方法是从给定数组生成所有可能的对,并为每个对找到K的值,并不断更新获得的最大K。最后,打印获得的K的最大值。
时间复杂度: O(N 2 )
辅助空间: O(1)

高效方法:
要优化上述方法,请使用“两个指针”技术。请按照以下步骤操作:

  • 初始化三个变量ijk 。最初设置i = 0k = 0
  • j = 1j = 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)