Given an array arr[] of N integers, the task is to find the maximum value possible remaining in the array by repeating the following two steps:
- Remove any two array elements.
- Insert the quotient of their division into the array.
Note: We are allowed to change the order of elements.
Examples:
Input: arr[] = {100, 1000, 10, 2}
Output: 200
Explanation:
- Remove 100 and 10 from the array. Insert 10 (= 100/10) back into the array.
- Remove 10 and 2 from the array. Insert 5 into the array.
- Remove 1000 and 5 from the array. Insert 200 into the array
Hence, the maximum result is 200.
Input: arr[] = {2, 100, 1}
Output: 50
Approach:
To solve the problem mentioned above, we can observe that we will get the maximum result when the elements are sorted in decreasing order and the division operations occur in the sequence arr[0] / ( arr[1] / arr[2] / arr[3]…..arr[n-1]) of the reversely sorted array. Hence, sort the array accordingly and calculate the appropriate result.
Below code is the implementation of the above approach:
C++
// C++ implementation to maximize the // result of division of the given // array elements #include <bits/stdc++.h> using namespace std; // Function to find the max result float maxDivision( int arr[], int n) { // Sort the array in descending order sort(arr, arr + n, greater< int >()); float mxdiv = arr[1]; // loop to divide in this order // arr[0] / ( arr[1] / arr[2] / .... // arr[n-2] / arr[n-1]) for ( int i = 2; i < n; ++i) mxdiv = mxdiv / arr[i]; // return the final result return arr[0] / mxdiv; } // Driver code int main() { int arr[] = { 100, 1000, 10, 2 }; int n = sizeof (arr) / sizeof (arr[0]); cout << maxDivision(arr, n); return 0; } |
Java
// Java implementation to maximize the // result of division of the given // array elements import java.util.*; class GFG{ // Function to find the max result static float maxDivision(Integer arr[], int n) { // Sort the array in descending order Arrays.sort(arr, Collections.reverseOrder()); float mxdiv = arr[ 1 ]; // Loop to divide in this order // arr[0] / ( arr[1] / arr[2] / .... // arr[n-2] / arr[n-1]) for ( int i = 2 ; i < n; ++i) mxdiv = mxdiv / arr[i]; // Return the final result return arr[ 0 ] / mxdiv; } // Driver code public static void main(String[] args) { Integer arr[] = { 100 , 1000 , 10 , 2 }; int n = arr.length; System.out.print(( int )maxDivision(arr, n)); } } // This code is contributed by amal kumar choubey |
Python3
# Python3 implementation to maximize # the result of division of the # given array elements # Function to find the max result def maxDivision(arr, n): # Sort the array in descending order arr.sort(reverse = True ) mxdiv = arr[ 1 ] # Loop to divide in this order # arr[0] / ( arr[1] / arr[2] / .... # arr[n-2] / arr[n-1]) for i in range ( 2 , n): mxdiv = mxdiv / arr[i] # Return the final result return arr[ 0 ] / mxdiv # Driver code arr = [ 100 , 1000 , 10 , 2 ] n = len (arr) print (maxDivision(arr, n)) # This code is contributed by ishayadav181 |
C#
// C# implementation to maximize the // result of division of the given // array elements using System; class GFG{ // Function to find the max result static float maxDivision( int []arr, int n) { // Sort the array in descending order Array.Sort(arr); Array.Reverse(arr); float mxdiv = arr[1]; // Loop to divide in this order // arr[0] / ( arr[1] / arr[2] / .... // arr[n-2] / arr[n-1]) for ( int i = 2; i < n; ++i) mxdiv = mxdiv / arr[i]; // Return the readonly result return arr[0] / mxdiv; } // Driver code public static void Main(String[] args) { int []arr = { 100, 1000, 10, 2 }; int n = arr.Length; Console.Write(( int )maxDivision(arr, n)); } } // This code is contributed by amal kumar choubey |
Javascript
<script> // Javascript implementation to maximize // the result of division of the given // array elements // Function to find the max result function maxDivision(arr, n) { // Sort the array in descending order arr.sort((a, b) => b - a); let mxdiv = arr[1]; // Loop to divide in this order // arr[0] / ( arr[1] / arr[2] / .... // arr[n-2] / arr[n-1]) for (let i = 2; i < n; ++i) mxdiv = mxdiv / arr[i]; // Return the final result return arr[0] / mxdiv; } // Driver Code let arr = [ 100, 1000, 10, 2 ]; let n = arr.length; document.write(maxDivision(arr, n)); // This code is contributed by susmitakundugoaldanga </script> |
200
Time Complexity: O(N log N)
Auxiliary space: O(1)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!