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

📅  最后修改于: 2021-09-03 14:36:24             🧑  作者: Mango

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

例子:

天真的方法:
解决这个问题最简单的方法是从给定的数组中生成所有可能的对,对于每一对,找到 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)

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live