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.

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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


Output:

200

Time Complexity: O(N logN)

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.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.