Minimum LCM of all pairs in a given array | C # program to find the pair having minimum LCM ; Function that return pair having minimum LCM ; Find max element in the array as the gcd of two elements from the array can 't greater than max element. ; Created a 2D array to store minimum two multiple of any particular i . ; We already found two smallest multiple ; Iterating over all gcd ; Iterating over its multiple ; If we already found the two smallest multiple of i ; Choosing smallest two multiple ; Calculating lcm ; Return readonly answer ; Driver code
using System ; using System . Collections . Generic ; class GFG { static int minLCM ( int [ ] arr , int n ) { int mx = 0 ; for ( int i = 0 ; i < n ; i ++ ) { mx = Math . Max ( mx , arr [ i ] ) ; } List < int > [ ] mul = new List < int > [ mx + 1 ] ; for ( int i = 0 ; i < mul . Length ; i ++ ) mul [ i ] = new List < int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( mul [ arr [ i ] ] . Count > 1 ) { continue ; } mul [ arr [ i ] ] . Add ( arr [ i ] ) ; } for ( int i = 1 ; i <= mx ; i ++ ) { for ( int j = i + i ; j <= mx ; j += i ) { if ( mul [ i ] . Count > 1 ) { break ; } foreach ( int k in mul [ j ] ) { if ( mul [ i ] . Count > 1 ) break ; mul [ i ] . Add ( k ) ; } } } int ans = int . MaxValue ; for ( int i = 1 ; i <= mx ; i ++ ) { if ( mul [ i ] . Count <= 1 ) continue ; int a = mul [ i ] [ 0 ] , b = mul [ i ] [ 1 ] ; int lcm = ( a * b ) / i ; ans = Math . Min ( ans , lcm ) ; } return ans ; } public static void Main ( String [ ] args ) { int [ ] arr = { 2 , 4 , 3 , 6 , 5 } ; int n = arr . Length ; Console . Write ( minLCM ( arr , n ) + " STRNEWLINE " ) ; } }