Rearrange an array such that product of every two consecutive elements is a multiple of 4 | C # program to rearray array elements such that the product of every two consecutive elements is a multiple of ; Function to rearrange array elements such that the every two consecutive elements is a multiple of 4 ; If element is odd ; Odd ; If element is divisible by 4 ; Divisible by 4 ; If element is not divisible by 4 ; Even but not divisible by 4 ; Condition for rearrangement to be possible ; Print ODD [ i ] and FOUR [ i ] consecutively ; Print the remaining FOUR [ i ] , if any ; Condition for rearrangement to be possible ; Print ODD [ i ] and FOUR [ i ] consecutively ; Print the remaining FOUR [ i ] , if any ; Print the NON_FOUR [ i ] elements at the end ; No possible configuration ; Driver Code
using System ; using System . Collections . Generic ; class GFG { public static void Permute ( List < int > arr , int n ) { int odd = 0 , four = 0 ; int non_four = 0 ; List < int > ODD = new List < int > ( ) ; List < int > FOUR = new List < int > ( n ) ; List < int > NON_FOUR = new List < int > ( n ) ; foreach ( int x in arr ) { if ( x % 2 != 0 ) { odd ++ ; ODD . Add ( x ) ; } else if ( x % 4 == 0 ) { four ++ ; FOUR . Add ( x ) ; } else { non_four ++ ; NON_FOUR . Add ( x ) ; } } if ( non_four == 0 && four >= odd - 1 ) { int x = ODD . Count ; int y = FOUR . Count ; int i ; for ( i = 0 ; i < x ; i ++ ) { Console . Write ( ODD [ i ] + " ▁ " ) ; if ( i < y ) Console . Write ( FOUR [ i ] + " ▁ " ) ; } while ( i < y ) Console . Write ( FOUR [ i ] + " ▁ " ) ; Console . WriteLine ( ) ; } else if ( non_four > 0 && four >= odd ) { int x = ODD . Count ; int y = FOUR . Count ; int i ; for ( i = 0 ; i < x ; i ++ ) { Console . Write ( ODD [ i ] + " ▁ " ) ; if ( i < y ) Console . Write ( FOUR [ i ] + " ▁ " ) ; } while ( i < y ) Console . Write ( FOUR [ i ] + " ▁ " ) ; for ( int j = 0 ; j < ( int ) NON_FOUR . Count ; j ++ ) Console . Write ( NON_FOUR [ j ] + " ▁ " ) ; Console . WriteLine ( ) ; } else Console . WriteLine ( " Not ▁ Possible " ) ; } public static void Main ( String [ ] args ) { List < int > arr = new List < int > ( ) ; arr . Add ( 2 ) ; arr . Add ( 7 ) ; arr . Add ( 1 ) ; arr . Add ( 8 ) ; arr . Add ( 2 ) ; arr . Add ( 8 ) ; Permute ( arr , arr . Count ) ; } }