Number of ways such that only K bars are visible from the left | C ++ program for the above approach ; Function to calculate the number of bars that are visible from the left for a particular arrangement ; If current element is greater than the last greater element , it is visible ; Function to calculate the number of rearrangements where K bars are visiblef from the left ; Vector to store current permutation ; Check for all permutations ; If current permutation meets the conditions , increment answer ; Driver code ; Input ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int noOfbarsVisibleFromLeft ( vector < int > v ) { int last = 0 , ans = 0 ; for ( auto u : v ) if ( last < u ) { ans ++ ; last = u ; } return ans ; } int KvisibleFromLeft ( int N , int K ) { vector < int > v ( N ) ; for ( int i = 0 ; i < N ; i ++ ) v [ i ] = i + 1 ; int ans = 0 ; do { if ( noOfbarsVisibleFromLeft ( v ) == K ) ans ++ ; } while ( next_permutation ( v . begin ( ) , v . end ( ) ) ) ; return ans ; } int main ( ) { int N = 5 , K = 2 ; cout << KvisibleFromLeft ( N , K ) << endl ; return 0 ; }