Count of distinct N-digit odd integers that can be generated using given set of digits
Given an array arr[] of size N representing digits from 0 to 9, the task is to count the number of distinct odd N-digit integers that can be formed using the given digits in the array.
Examples:
Input: arr[] = {1, 0, 2, 4}
Output: 4
Explanation: The possible 4-digit odd integers that can be formed using the given digits are 2041, 2401, 4021 and 4201.
Input: arr[] = {2, 3, 4, 1, 2, 3}
Output: 90
Approach: The given problem can be solved using the following observations:
- For the number to be odd, its unit place (i.e, 1st digit) should have an odd digit i.e., {1, 3, 5, 7, 9}
- Since the integer should have N digits, the most significant digit (digit at Nth place) can not be equal to 0.
- All the digits other than the digit at 1st and Nth place can have any other digit.
- The total number of ways to arrange X digits are X! / ( freq[0]! * freq[1]! *…* freq[9]! ), where freq[i] denotes the frequency of digit i in the given X digits.
In order to solve the problem keep track of the number of odd digits in a variable odd and the number of digits that are equal to 0 in a variable zero. So according to the above observations, if i represents the Nth digit and j represents the 1st digit, iterate over all possible values of i and j, and for each valid (i, j), calculate the number of ways of arranging the remaining (N-2) digits.
Below is the implementation of the above approach:
C++14
#include <bits/stdc++.h>
using namespace std;
int countOddIntegers( int arr[], int N)
{
int Fact[N] = {};
Fact[0] = 1;
for ( int i = 1; i < N; i++) {
Fact[i] = i * Fact[i - 1];
}
int freq[10] = {};
for ( int i = 0; i < N; i++) {
freq[arr[i]]++;
}
int ans = 0;
for ( int i = 1; i <= 9; i += 2) {
if (!freq[i])
continue ;
freq[i]--;
for ( int j = 1; j <= 9; j++) {
int cur_ans = 0;
if (freq[j] == 0) {
continue ;
}
freq[j]--;
cur_ans = Fact[N - 2];
for ( int k = 0; k <= 9; k++) {
cur_ans = cur_ans / Fact[freq[k]];
}
ans += cur_ans;
freq[j]++;
}
freq[i]++;
}
return ans;
}
int main()
{
int A[] = { 2, 3, 4, 1, 2, 3 };
int N = sizeof (A) / sizeof ( int );
cout << countOddIntegers(A, N);
return 0;
}
|
Java
import java.util.*;
class GFG{
static int countOddIntegers( int arr[], int N)
{
int Fact[] = new int [N];
Fact[ 0 ] = 1 ;
for ( int i = 1 ; i < N; i++)
{
Fact[i] = i * Fact[i - 1 ];
}
int freq[] = new int [ 10 ];
for ( int i = 0 ; i < N; i++)
{
freq[arr[i]]++;
}
int ans = 0 ;
for ( int i = 1 ; i <= 9 ; i += 2 )
{
if (freq[i] == 0 )
continue ;
freq[i]--;
for ( int j = 1 ; j <= 9 ; j++)
{
int cur_ans = 0 ;
if (freq[j] == 0 )
{
continue ;
}
freq[j]--;
cur_ans = Fact[N - 2 ];
for ( int k = 0 ; k <= 9 ; k++)
{
cur_ans = cur_ans / Fact[freq[k]];
}
ans += cur_ans;
freq[j]++;
}
freq[i]++;
}
return ans;
}
public static void main(String[] args)
{
int A[] = { 2 , 3 , 4 , 1 , 2 , 3 };
int N = A.length;
System.out.print(countOddIntegers(A, N));
}
}
|
Python3
from array import *
from math import *
def countOddIntegers(arr, N):
Fact = [ 0 ] * N
Fact[ 0 ] = 1
for i in range ( 1 ,N):
Fact[i] = i * Fact[i - 1 ]
freq = [ 0 ] * 10
for i in range ( len (freq)):
freq[i] = 0 ;
for i in range (N):
freq[arr[i]] = freq[arr[i]] + 1 ;
ans = 0
for i in range ( 1 , 10 , 2 ) :
if (freq[i] = = 0 ):
continue
freq[i] = freq[i] - 1 ;
for j in range ( 1 , 10 , 1 ) :
cur_ans = 0
if (freq[j] = = 0 ) :
continue
freq[j] = freq[j] - 1 ;
cur_ans = Fact[N - 2 ]
for k in range ( 10 ):
cur_ans = cur_ans / Fact[freq[k]]
ans + = cur_ans
freq[j] = freq[j] + 1 ;
freq[i] = freq[i] + 1 ;
return ceil(ans)
if __name__ = = "__main__" :
A = [ 2 , 3 , 4 , 1 , 2 , 3 ]
N = len (A)
print (countOddIntegers(A, N))
|
C#
using System;
class GFG{
static int countOddIntegers( int []arr, int N)
{
int []Fact = new int [N];
Fact[0] = 1;
for ( int i = 1; i < N; i++)
{
Fact[i] = i * Fact[i - 1];
}
int []freq = new int [10];
for ( int i = 0; i < N; i++)
{
freq[arr[i]]++;
}
int ans = 0;
for ( int i = 1; i <= 9; i += 2)
{
if (freq[i] == 0)
continue ;
freq[i]--;
for ( int j = 1; j <= 9; j++)
{
int cur_ans = 0;
if (freq[j] == 0)
{
continue ;
}
freq[j]--;
cur_ans = Fact[N - 2];
for ( int k = 0; k <= 9; k++)
{
cur_ans = cur_ans / Fact[freq[k]];
}
ans += cur_ans;
freq[j]++;
}
freq[i]++;
}
return ans;
}
public static void Main(String[] args)
{
int []A = { 2, 3, 4, 1, 2, 3 };
int N = A.Length;
Console.Write(countOddIntegers(A, N));
}
}
|
Javascript
<script>
function countOddIntegers(arr, N)
{
let Fact = new Array(N);
Fact[0] = 1;
for (let i = 1; i < N; i++) {
Fact[i] = i * Fact[i - 1];
}
let freq = new Array(10).fill(0);
for (let i = 0; i < N; i++) {
freq[arr[i]]++;
}
let ans = 0;
for (let i = 1; i <= 9; i += 2) {
if (!freq[i]) {
continue ;
}
freq[i]--;
for (let j = 1; j <= 9; j++) {
let cur_ans = 0;
if (freq[j] == 0) {
continue ;
}
freq[j]--;
cur_ans = Fact[N - 2];
for (let k = 0; k <= 9; k++) {
cur_ans = Math.floor(cur_ans / Fact[freq[k]]);
}
ans = ans + cur_ans;
freq[j]++;
}
freq[i]++;
}
return ans;
}
let A = [2, 3, 4, 1, 2, 3];
let N = A.length;
document.write(countOddIntegers(A, N));
</script>
|
Time Complexity: O(N * 50)
Auxiliary Space: O(N) it is using extra space for array Fact
Last Updated :
03 Nov, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...