Check if elements of an array can be arranged satisfying the given condition | C # implementation of the approach ; Function to return true if the elements can be arranged in the desired order ; If 2 * x is not found to pair ; Remove an occurrence of x and an occurrence of 2 * x ; Driver code ; Function call to print required answer
using System ; using System . Collections . Generic ; class GFG { static String canReorder ( int [ ] A , int n ) { Dictionary < int , int > m = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( m . ContainsKey ( A [ i ] ) ) m [ A [ i ] ] = m [ A [ i ] ] + 1 ; else m . Add ( A [ i ] , 1 ) ; } Array . Sort ( A ) ; int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( m [ A [ i ] ] == 0 ) continue ; if ( m . ContainsKey ( 2 * A [ i ] ) ) { count += 2 ; m [ A [ i ] ] = m [ A [ i ] ] - 1 ; if ( m . ContainsKey ( 2 * A [ i ] ) ) m [ 2 * A [ i ] ] = m [ 2 * A [ i ] ] - 1 ; else m . Add ( 2 * A [ i ] , m [ 2 * A [ i ] ] - 1 ) ; } } if ( count == n ) return " True " ; else return " False " ; } public static void Main ( String [ ] args ) { int [ ] A = { 4 , - 2 , 2 , - 4 } ; int n = A . Length ; Console . WriteLine ( canReorder ( A , n ) ) ; } }