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