Largest number with the given set of N digits that is divisible by 2 , 3 and 5 | C # implementation of the above approach ; Function to find the largest integer with the given set ; find sum of all the digits look if any 0 is present or not ; if 0 is not present , the resultant number won 't be divisible by 5 ; sort all the elements in a non - decreasing manner ; if there is just one element 0 ; find the remainder of the sum of digits when divided by 3 ; there can a remainder as 1 or 2 ; traverse from the end of the digits ; first element which has the same remainder remove it ; if there is no element which has a same remainder as y ; subtract it by 3 ( could be one or two ) ; delete two minimal digits which has a remainder as y ; print all the digits as a single integer ; Driver code ; initialize the number of set of digits ; initialize all the set of digits in a vector
using System ; using System . Collections ; class GFG { static int findLargest ( int n , ArrayList v ) { int flag = 0 ; long sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( ( int ) v [ i ] == 0 ) flag = 1 ; sum += ( int ) v [ i ] ; } if ( flag != 1 ) Console . WriteLine ( " Not ▁ possible " ) ; else { v . Sort ( ) ; v . Reverse ( ) ; if ( ( int ) v [ 0 ] == 0 ) { Console . WriteLine ( "0" ) ; return 0 ; } else { int flags = 0 ; int y = ( int ) ( sum % 3 ) ; if ( y != 0 ) { for ( int i = n - 1 ; i >= 0 ; i -- ) { if ( ( int ) v [ i ] % 3 == y ) { v . RemoveAt ( i ) ; flags = 1 ; break ; } } if ( flags == 0 ) { y = 3 - y ; int cnt = 0 ; for ( int i = n - 1 ; i >= 0 ; i -- ) { if ( ( int ) v [ i ] % 3 == y ) { v . RemoveAt ( i ) ; cnt ++ ; if ( cnt >= 2 ) break ; } } } } if ( ( int ) v [ 0 ] == 0 ) Console . WriteLine ( "0" ) ; else for ( int i = 0 ; i < v . Count ; i ++ ) { Console . Write ( v [ i ] ) ; } } } return int . MinValue ; } static void Main ( ) { int [ ] arr = { 3 , 9 , 9 , 6 , 4 , 3 , 6 , 4 , 9 , 6 , 0 } ; int n = 11 ; ArrayList v = new ArrayList ( ) ; for ( int i = 0 ; i < n ; i ++ ) v . Add ( arr [ i ] ) ; findLargest ( n , v ) ; } }