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

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:**

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the **Essential Maths for CP Course** at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more, please refer **Complete Interview Preparation Course****.**

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[0]**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 <bits/stdc++.h>` `using` `namespace` `std;` `// Function to place the parenthesis` `// such that the result is maximized` `void` `generateSequence(` `int` `arr[], ` `int` `n)` `{` ` ` `// Store the required string` ` ` `string ans;` ` ` `// Add the first integer to string` ` ` `ans = to_string(arr[0]);` ` ` `// 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[1]);` ` ` `}` ` ` `// If size of array is exceeds two,` ` ` `// print 1st integer concatenated` ` ` `// with operators '/', '(' and next` ` ` `// integers with the operator '/'` ` ` `else` `{` ` ` `ans += ` `"/("` `+ to_string(arr[1]);` ` ` `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 Code` `int` `main()` `{` ` ` `int` `arr[] = { 1000, 100, 10, 2 };` ` ` `int` `N = ` `sizeof` `(arr) / ` `sizeof` `(arr[0]);` ` ` `generateSequence(arr, N);` ` ` `return` `0;` `}` |

## Java

`// Java program for the above approach` `import` `java.io.*;` `class` `GFG` `{` `// Function to place the parenthesis` `// such that the result is maximized` `static` `void` `generateSequence(` `int` `arr[], ` `int` `n)` `{` ` ` ` ` `// Store the required string` ` ` `String ans;` ` ` `// Add the first integer to string` ` ` `ans = Integer.toString(arr[` `0` `]);` ` ` `// 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[` `1` `]));` ` ` `}` ` ` `// If size of array is exceeds two,` ` ` `// print 1st integer concatenated` ` ` `// with operators '/', '(' and next` ` ` `// integers with the operator '/'` ` ` `else` `{` ` ` `ans += ` `"/("` `+ Integer.toString(arr[` `1` `]);` ` ` `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 Code` `public` `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 maximized` `def` `generateSequence(arr, n):` ` ` ` ` `# Store the required string` ` ` `ans ` `=` `""` ` ` `# Add the first integer to string` ` ` `ans ` `=` `str` `(arr[` `0` `])` ` ` `# 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[` `1` `]))` ` ` ` ` `# If size of array is exceeds two,` ` ` `# pr1st integer concatenated` ` ` `# with operators '/', '(' and next` ` ` `# integers with the operator '/'` ` ` `else` `:` ` ` `ans ` `+` `=` `"/("` `+` `str` `(arr[` `1` `])` ` ` `for` `i ` `in` `range` `(` `2` `, n):` ` ` `ans ` `+` `=` `"/"` `+` `str` `(arr[i])` ` ` `# Add parenthesis at the end` ` ` `ans ` `+` `=` `")"` ` ` `# Prthe final expression` ` ` `print` `(ans)` `# Driver Code` `if` `__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 approach` `using` `System;` `class` `GFG` `{` `// Function to place the parenthesis` `// such that the result is maximized` `static` `void` `generateSequence(` `int` `[]arr, ` `int` `n)` `{` ` ` ` ` `// Store the required string` ` ` `string` `ans=` `""` `;` ` ` `// Add the first integer to string` ` ` `ans = arr[0].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[1].ToString());` ` ` `}` ` ` `// If size of array is exceeds two,` ` ` `// print 1st integer concatenated` ` ` `// with operators '/', '(' and next` ` ` `// integers with the operator '/'` ` ` `else` `{` ` ` `ans += ` `"/("` `+ arr[1].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 Code` `public` `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

`<script>` `// Javascript program for the above approach` `// Function to place the parenthesis` `// such that the result is maximized` `function` `generateSequence(arr, n)` `{` ` ` `// Store the required string` ` ` `var` `ans;` ` ` `// Add the first integer to string` ` ` `ans = (arr[0].toString());` ` ` `// If the size of array is 1` ` ` `if` `(n == 1)` ` ` `document.write( ans);` ` ` `// If the size of array is 2, print` ` ` `// the 1st integer followed by / operator` ` ` `// followed by the next integer` ` ` `else` `if` `(n == 2) {` ` ` `document.write( ans + ` `"/"` ` ` `+ (arr[1].toString()));` ` ` `}` ` ` `// If size of array is exceeds two,` ` ` `// print 1st integer concatenated` ` ` `// with operators '/', '(' and next` ` ` `// integers with the operator '/'` ` ` `else` `{` ` ` `ans += ` `"/("` `+ (arr[1].toString());` ` ` `for` `(` `var` `i = 2; i < n; i++) {` ` ` `ans += ` `"/"` `+ (arr[i].toString());` ` ` `}` ` ` `// Add parenthesis at the end` ` ` `ans += ` `")"` `;` ` ` `// Print the final expression` ` ` `document.write( ans);` ` ` `}` `}` `// Driver Code` `var` `arr = [1000, 100, 10, 2];` `var` `N = arr.length;` `generateSequence(arr, N);` `// This code is contributed by noob2000.` `</script>` |

**Output:**

1000/(100/10/2)

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