Generate a sequence such that float division of array elements is maximized

• Last Updated : 13 Jan, 2022

Given an array arr[] consisting of N integers, the task is to find the expression by using parenthesis ‘(‘ and ‘)’ and division operator ‘/’ to maximize the value of the expression of subsequent float division of array elements.

Examples:

Input: arr[] = {1000, 100, 10, 2}
Output: “1000/(100/10/2)”
Explanation:
The value of the expression 1000/(100/10/2) can be calculated as 1000/((100/10)/2) = 200.

Input: arr[] = {2, 3, 4}
Output: “2/(3/4)”

Approach: The idea is based on the observation that for every division, the result is maximum only when the denominator is minimum. Therefore, the task reduces to place the parentheses and operators in such a way that the denominator is minimum. Consider the following example to solve the problem:

Consider an expression 1000 / 100 / 10 / 2.
To make its value maximum, denominator needs to be minimized. Therefore, the denominators need to be in the sequence 100, 10, 2.
Now, consider the following cases:

• 100 / (10 / 2) = (100 × 2) / 10 = 20
• (100 / 10) / 2 = 10 / 2 = 5

Therefore, the minimized value for the expression is obtained for the second case. Therefore, 1000 / (100 / 10 / 2) is the required sequence.

Therefore, from the above example, it can be concluded that the parenthesis needs to be placed to the sequence after the first integer that makes the whole sequence from the second integer reduced to the minimum value possible.
Follow the steps below to solve the problem:

• Initialize a string S as “”, to store the final expression.
• If N is equal to 1, print the integer in string form.
• Otherwise, append arr and “/(“ in S, and then append all the remaining integers of arr[] separated by “/”.
• At last, append “)” in the string S and print the string S as the result.

Below is the implementation of the above approach:

C++14

 // C++ program for the above approach#include using namespace std; // Function to place the parenthesis// such that the result is maximizedvoid generateSequence(int arr[], int n){    // Store the required string    string ans;     // Add the first integer to string    ans = to_string(arr);     // If the size of array is 1    if (n == 1)        cout << ans;     // If the size of array is 2, print    // the 1st integer followed by / operator    // followed by the next integer    else if (n == 2) {        cout << ans + "/"             << to_string(arr);    }     // If size of array is exceeds two,    // print 1st integer concatenated    // with operators '/', '(' and next    // integers with the operator '/'    else {        ans += "/(" + to_string(arr);         for (int i = 2; i < n; i++) {            ans += "/" + to_string(arr[i]);        }         // Add parenthesis at the end        ans += ")";         // Print the final expression        cout << ans;    }} // Driver Codeint main(){    int arr[] = { 1000, 100, 10, 2 };    int N = sizeof(arr) / sizeof(arr);    generateSequence(arr, N);     return 0;}

Java

 // Java program for the above approachimport java.io.*;class GFG{ // Function to place the parenthesis// such that the result is maximizedstatic void generateSequence(int arr[], int n){       // Store the required string    String ans;     // Add the first integer to string    ans = Integer.toString(arr);     // If the size of array is 1    if (n == 1)        System.out.println(ans);     // If the size of array is 2, print    // the 1st integer followed by / operator    // followed by the next integer    else if (n == 2) {        System.out.println(ans + "/"            + Integer.toString(arr));    }     // If size of array is exceeds two,    // print 1st integer concatenated    // with operators '/', '(' and next    // integers with the operator '/'    else {        ans += "/(" + Integer.toString(arr);         for (int i = 2; i < n; i++) {            ans += "/" + Integer.toString(arr[i]);        }         // Add parenthesis at the end        ans += ")";         // Print the final expression        System.out.println(ans);    }}  // Driver Codepublic static void main(String[] args){    int arr[] = { 1000, 100, 10, 2 };    int N = arr.length;    generateSequence(arr, N);}} // This code is contributed by code_hunt.

Python3

 # Python3 program for the above approach # Function to place the parenthesis# such that the result is maximizeddef generateSequence(arr, n):         # Store the required string    ans = ""     # Add the first integer to string    ans = str(arr)     # If the size of array is 1    if (n == 1):        print(ans)     # If the size of array is 2, print    # the 1st integer followed by / operator    # followed by the next integer    elif (n == 2):        print(ans + "/"+str(arr))         # If size of array is exceeds two,    # pr1st integer concatenated    # with operators '/', '(' and next    # integers with the operator '/'    else:        ans += "/(" + str(arr)         for i in range(2, n):            ans += "/" + str(arr[i])         # Add parenthesis at the end        ans += ")"         # Print final expression        print(ans) # Driver Codeif __name__ == '__main__':    arr = [1000, 100, 10, 2]    N = len(arr)    generateSequence(arr, N)     # This code is contributed by mohit kumar 29.

C#

 // C# program for the above approachusing System;class GFG{ // Function to place the parenthesis// such that the result is maximizedstatic void generateSequence(int []arr, int n){       // Store the required string    string ans="";     // Add the first integer to string    ans = arr.ToString();     // If the size of array is 1    if (n == 1)        Console.WriteLine(ans);     // If the size of array is 2, print    // the 1st integer followed by / operator    // followed by the next integer    else if (n == 2) {        Console.WriteLine(ans + "/"            + arr.ToString());    }     // If size of array is exceeds two,    // print 1st integer concatenated    // with operators '/', '(' and next    // integers with the operator '/'    else {        ans += "/(" + arr.ToString();         for (int i = 2; i < n; i++) {            ans += "/" + arr[i].ToString();        }         // Add parenthesis at the end        ans += ")";         // Print the final expression        Console.WriteLine(ans);    }} // Driver Codepublic static void Main(string[] args){    int []arr = { 1000, 100, 10, 2 };    int N = arr.Length;    generateSequence(arr, N);}} // This code is contributed by chitranayal.

Javascript


Output:
1000/(100/10/2)

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

My Personal Notes arrow_drop_up