Given an array **A[]** consisting of distinct elements, the task is to obtain the largest possible modulus value that remains after repeatedly replacing adjacent elements by their modulus, starting from the first element, for any possible permutations of the given array.

(…(( A[1] mod A[2]) mod A[3]) …. ) mod A[N])

**Examples:**

Input:A[] = {7, 10, 12}Output:7Explanation:All possible values of the given expression across all permutations of the given array are as follows:

{7, 10, 12} = ((7 % 10) % 12) = 7

{10, 12 7} = ((10 % 12) % 7) = 3

{7, 12, 10} =((7 % 12) % 10) = 7

{10, 7, 12} = ((10 % 7) % 12) = 3

{12, 7, 10} = ((12 % 7) % 10) = 5

{12, 10, 7} = ((12 % 10) % 7) = 2

Therefore, the maximum possible value is 7.

Input:A[] = {20, 30}Output:20Explanation:

The maximum possible value from all the permutations of the given array is 20.

**Naive Approach:** The simplest approach to solve the problem is to generate all permutations of the given array and find the value of the given expression for all permutations. Finally, print the maximum value of the expression obtained. **Time Complexity:** O(N * N!) **Auxiliary Space: **O(N)

**Efficient Approach: **To optimize the above approach, the following observations need to be made:

- For any permutation
A, the value of the expression always lies in the range_{1}…..A_{N}[0, min(A._{2}…..A_{n})-1]- Considering
Kto be the smallest element in the array, the value of the expression will always be K for the permutations having K as the first element.- For all other permutations, the value of the expression will always be less than
K, as shown in the examples above. Therefore,Kis the maximum possible value of the expression for any permutation of the array.- Therefore, the maximum possible value will always be equal to the smallest element of the array.

Therefore, to solve the problem, simply traverse the array and find the minimum element present in the array and print it as the required answer.

Below is the implementation of the above approach:

## C++

`// C++ Program to implement` `// the above approach` `#include <bits/stdc++.h>` `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;` `}` |

*chevron_right*

*filter_none*

## 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` |

*chevron_right*

*filter_none*

## 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` |

*chevron_right*

*filter_none*

## 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` |

*chevron_right*

*filter_none*

**Output:**

7

**Time Complexity: **O(N)**Auxiliary Space: **O(1)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Maximize array sum by replacing equal adjacent pairs by their sum and X respectively
- Minimize array length by repeatedly replacing pairs of unequal adjacent array elements by their sum
- Smallest array that can be obtained by replacing adjacent pairs with their products
- Maximize count of equal numbers in Array of numbers upto N by replacing pairs with their sum
- Maximize sum of squares of array elements possible by replacing pairs with their Bitwise AND and Bitwise OR
- Minimum number of adjacent swaps required to convert a permutation to another permutation by given condition
- Minimize length of string by replacing K pairs of distinct adjacent characters
- Maximize product of array by replacing array elements with its sum or product with element from another array
- Maximize minimum distance between repetitions from any permutation of the given Array
- Maximize Bitwise AND of first element with complement of remaining elements for any permutation of given Array
- Maximize removal of adjacent array elements based on their absolute value
- Maximize the given number by replacing a segment of digits with the alternate digits given
- Maximize the sum of modulus with every Array element
- Check if any permutation of array contains sum of every adjacent pair not divisible by 3
- Count of pairs in a given range with sum of their product and sum equal to their concatenated number
- Array value by repeatedly replacing max 2 elements with their absolute difference
- Possible values of Q such that, for any value of R, their product is equal to X times their sum
- Maximum sum of Array formed by replacing each element with sum of adjacent elements
- Minimize swaps required to maximize the count of elements replacing a greater element in an Array
- Maximize length of increasing subsequence possible by replacing array element by nearest primes

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.