Find array with k number of merge sort calls | C ++ program to find an array that can be sorted with k merge sort calls . ; We make two recursive calls , so reduce k by 2. ; Create an array with values in [ 1 , n ] ; calling unsort function ; Driver code
#include <iostream> NEW_LINE using namespace std ; void unsort ( int l , int r , int a [ ] , int & k ) { if ( k < 1 l + 1 == r ) return ; k -= 2 ; int mid = ( l + r ) / 2 ; swap ( a [ mid - 1 ] , a [ mid ] ) ; unsort ( l , mid , a , k ) ; unsort ( mid , r , a , k ) ; } void arrayWithKCalls ( int n , int k ) { if ( k % 2 == 0 ) { cout << " ▁ NO ▁ SOLUTION ▁ " ; return ; } int a [ n + 1 ] ; a [ 0 ] = 1 ; for ( int i = 1 ; i < n ; i ++ ) a [ i ] = i + 1 ; k -- ; unsort ( 0 , n , a , k ) ; for ( int i = 0 ; i < n ; ++ i ) cout << a [ i ] << ' ▁ ' ; } int main ( ) { int n = 10 , k = 17 ; arrayWithKCalls ( n , k ) ; return 0 ; }