Maximize the division result of Array using given operations
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.
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. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.
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 resultfloat 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 codeint 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 elementsimport java.util.*;class GFG{// Function to find the max resultstatic 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 codepublic 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 resultdef 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 codearr = [ 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 elementsusing System;class GFG{// Function to find the max resultstatic 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 codepublic 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 resultfunction 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 Codelet 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 logN)


