Check if concatenation of any permutation of given list of arrays generates the given array | C # program for the above approach ; Function to check if it is possible to obtain array by concatenating the arrays in list pieces [ ] ; Stores the index of element in the given array arr [ ] ; Traverse over the list pieces ; If item size is 1 and exists in map ; If item contains > 1 element then check order of element ; If end of the array ; Check the order of elements ; If order is same as the array elements ; Increment idx ; If order breaks ; Otherwise ; Return false if the first element doesn 't exist in m ; Return true ; Driver Code ; Given target list ; Given array of list ; Function call
using System ; using System . Collections ; using System . Collections . Generic ; using System . Linq ; class GFG { static bool check ( List < int > arr , List < List < int > > pieces ) { Dictionary < int , int > m = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < arr . Count ; i ++ ) m . Add ( arr [ i ] , i ) ; for ( int i = 0 ; i < pieces . Count ; i ++ ) { if ( pieces [ i ] . Count == 1 && m . ContainsKey ( pieces [ i ] [ 0 ] ) ) { continue ; } else if ( pieces [ i ] . Count > 1 && m . ContainsKey ( pieces [ i ] [ 0 ] ) ) { int idx = m [ pieces [ i ] [ 0 ] ] ; idx ++ ; if ( idx >= arr . Count ) return false ; for ( int j = 1 ; j < pieces [ i ] . Count ; j ++ ) { if ( arr [ idx ] == pieces [ i ] [ j ] ) { idx ++ ; if ( idx >= arr . Count && j < pieces [ i ] . Count - 1 ) return false ; } else { return false ; } } } else { return false ; } } return true ; } static public void Main ( ) { List < int > arr = new List < int > ( ) { 1 , 2 , 4 , 3 } ; List < List < int > > pieces = new List < List < int > > ( ) ; pieces . Add ( new List < int > ( ) { 1 } ) ; pieces . Add ( new List < int > ( ) { 4 , 3 } ) ; pieces . Add ( new List < int > ( ) { 2 } ) ; if ( check ( arr , pieces ) ) { Console . WriteLine ( " Yes " ) ; } else { Console . WriteLine ( " No " ) ; } } }