📜  通过用给定数组的任何排列的模数替换相邻对来最大化模数

📅  最后修改于: 2021-10-26 05:53:51             🧑  作者: Mango

给定一个由不同元素组成的数组A[] ,任务是获得最大可能的模值,在重复替换相邻元素的模数后,从第一个元素开始,对于给定数组的任何可能排列。

例子:

朴素方法:解决问题的最简单方法是生成给定数组的所有排列,并为所有排列找到给定表达式的值。最后,打印得到的表达式的最大值。
时间复杂度: O(N * N!)
辅助空间: O(N)

高效的方法:要优化上述方法,需要进行以下观察:

因此,要解决问题,只需遍历数组并找到数组中存在的最小元素并将其打印为所需答案。

下面是上述方法的实现:

C++
// C++ Program to implement
// the above approach
#include 
using namespace std;
 
// Function to find the minimum
// of two numbers
int min(int a, int b)
{
    return (a > b) ? b : a;
}
 
// Function to find the maximum value
// possible of the given expression
// from all permutations of the array
int maximumModuloValue(int A[], int n)
{
    // Stores the minimum value
    // from the array
    int mn = INT_MAX;
    for (int i = 0; i < n; i++) {
        mn = min(A[i], mn);
    }
 
    // Return the answer
    return mn;
}
 
// Driver Code
int main()
{
    int A[] = { 7, 10, 12 };
 
    int n = (sizeof(A) / (sizeof(A[0])));
 
    cout << maximumModuloValue(A, n)
         << endl;
 
    return 0;
}


Java
// Java Program to implement
// the above approach
import java.io.*;
class GFG{
 
    // Function to find the maximum value
    // possible of the given expression
    // from all permutations of the array
    static int maximumModuloValue(int A[], int n)
    {
        // Stores the minimum value
        // from the array
        int mn = Integer.MAX_VALUE;
 
        for (int i = 0; i < n; i++)
        {
            mn = Math.min(A[i], mn);
        }
 
        // Return the answer
        return mn;
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        int A[] = {7, 10, 12};
        int n = A.length;
        System.out.println(maximumModuloValue(A, n));
    }
}
 
// This code is contributed by AnkitRai01


Python3
# Python3 program to implement
# the above approach
import sys
 
# Function to find the maximum value
# possible of the given expression
# from all permutations of the array
def maximumModuloValue(A, n):
 
    # Stores the minimum value
    # from the array
    mn = sys.maxsize
    for i in range(n):
        mn = min(A[i], mn)
 
    # Return the answer
    return mn
 
# Driver Code
 
# Given array arr[]
A = [ 7, 10, 12 ]
 
n = len(A)
 
# Function call
print(maximumModuloValue(A, n))
 
# This code is contributed by Shivam Singh


C#
// C# Program to implement
// the above approach
using System;
class GFG{
 
  // Function to find the maximum value
  // possible of the given expression
  // from all permutations of the array
  static int maximumModuloValue(int []A,
                                int n)
  {
    // Stores the minimum value
    // from the array
    int mn = int.MaxValue;
 
    for (int i = 0; i < n; i++)
    {
      mn = Math.Min(A[i], mn);
    }
 
    // Return the answer
    return mn;
  }
 
  // Driver Code
  public static void Main(String[] args)
  {
    int []A = {7, 10, 12};
    int n = A.Length;
    Console.WriteLine(maximumModuloValue(A, n));
  }
}
 
// This code is contributed by shikhasingrajput


Javascript


输出:
7

时间复杂度: O(N)
辅助空间: O(1)

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程