Arrangement of the characters of a word such that all vowels are at odd places | C ++ program to find the number of ways in which the characters of the word can be arranged such that the vowels occupy only the odd positions ; Function to return the factorial of a number ; calculating nPr ; Function to find the number of ways in which the characters of the word can be arranged such that the vowels occupy only the odd positions ; Get total even positions ; Get total odd positions ; Store frequency of each character of the string ; Count total number of vowels ; Count total number of consonants ; Calculate the total number of ways ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int fact ( int n ) { int f = 1 ; for ( int i = 2 ; i <= n ; i ++ ) { f = f * i ; } return f ; } int npr ( int n , int r ) { return fact ( n ) / fact ( n - r ) ; } int countPermutations ( string str ) { int even = floor ( str . length ( ) / 2 ) ; int odd = str . length ( ) - even ; int ways = 0 ; int freq [ 26 ] = { 0 } ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { ++ freq [ str [ i ] - ' a ' ] ; } int nvowels = freq [ 0 ] + freq [ 4 ] + freq [ 8 ] + freq [ 14 ] + freq [ 20 ] ; int nconsonants = str . length ( ) - nvowels ; ways = npr ( odd , nvowels ) * npr ( nconsonants , nconsonants ) ; return ways ; } int main ( ) { string str = " geeks " ; cout << countPermutations ( str ) ; return 0 ; }