Find four factors of N with maximum product and sum equal to N | Set | C # program to find four factors of N with maximum product and sum equal to N ; Function to find factors and to print those four factors ; push all the factors in the container ; number of factors ; Initial maximum ; hash - array to mark the pairs ; form all the pair sums ; if the pair sum is less than n ; push in another container ; mark the sum with the elements formed ; mark in the map that v [ i ] + v [ j ] is present ; new size of all the pair sums ; iterate for all pair sum ; the required part ; if the required part is also present in pair sum ; find the elements with which the first pair is formed ; find the elements with which the second pair is formed ; check for previous maximum ; Driver code
using System ; using System . Collections . Generic ; class GFG { static void findfactors ( int n ) { Dictionary < int , int > mpp = new Dictionary < int , int > ( ) ; List < int > v = new List < int > ( ) , v1 = new List < int > ( ) ; for ( int i = 1 ; i <= ( int ) Math . Sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) { v . Add ( i ) ; if ( i != ( n / i ) && i != 1 ) v . Add ( n / i ) ; } } int s = v . Count ; int maxi = - 1 ; int [ ] mp1_first = new int [ n + 5 ] ; int [ ] mp1_second = new int [ n + 5 ] ; for ( int i = 0 ; i < s ; i ++ ) { for ( int j = i ; j < s ; j ++ ) { if ( v [ i ] + v [ j ] < n ) { v1 . Add ( v [ i ] + v [ j ] ) ; mp1_first [ v [ i ] + v [ j ] ] = v [ i ] ; mp1_second [ v [ i ] + v [ j ] ] = v [ j ] ; mpp . Add ( v [ i ] + v [ j ] , 1 ) ; } } } s = v1 . Count ; for ( int i = 0 ; i < s ; i ++ ) { int el = n - ( v1 [ i ] ) ; if ( mpp . ContainsKey ( el ) ) { int a = mp1_first [ v1 [ i ] ] ; int b = mp1_second [ v1 [ i ] ] ; int c = mp1_first [ n - v1 [ i ] ] ; int d = mp1_second [ n - v1 [ i ] ] ; maxi = Math . Max ( a * b * c * d , maxi ) ; } } if ( maxi == - 1 ) Console . WriteLine ( " Not ▁ Possible " ) ; else { Console . WriteLine ( " The ▁ maximum ▁ product " + " ▁ is ▁ " + maxi ) ; } } public static void Main ( String [ ] args ) { int n = 50 ; findfactors ( n ) ; } }