Improve Article
Save Article
Improve Article
Save Article
Given an array arr[] of magnitude n which contains affirmative integers (0 ≤ arr[i] ≤ 109), the task is to count the number of elements coming successful nan fixed array specified that nan factorial of their digit sum (we will do digit sum while its worth is greater than 10) contains each digit coming successful nan original integer.
Examples:
Input: arr[] = [653, 663, 242, 7170, 30006]
Output: 2
Explanation:
- For 653 digits are: (3, 5, 6), digit sum: 6 + 5 + 3 = 14(as 14 ≥ 10) -> 1 + 4 = 5 and 5! = 120, truthful digits are: (0, 1, 2), it doesn’t incorporate 3, 5 and 6 truthful it is not an integer we request to count.
- For 663 digits are: (3, 6), digit sum: 6 + 6 + 3 = 15(as 15 ≥ 10) -> 1 + 5 = 6 and 6! = 720, truthful digits are: (0, 2, 7), it doesn’t incorporate 3 and 6 truthful it is not an integer we request to count.
- For 242 digits are: (2, 4), digit sum: 2 + 4 + 2 = 8 and 8! = 40320, truthful digits are: (0, 2, 3, 4), which contains some 2 and 4 truthful it is an integer we request to count.
- For 7170 digits are: (0, 1, 7), digit sum: 7 + 1 + 7 + 0 = 15(as 15 ≥ 10) -> 1 + 5 = 6 and 6! = 720, truthful digits are: (0, 2, 7), it doesn’t incorporate 1 truthful it is not an integer we request to count.
- For 30006 digits are: (0, 3, 6), digit sum: 3 + 0 + 0 + 0 + 6 = 9 and 9! = 362880, truthful digits are: (0, 2, 3, 6, 8), which contains each digit of 30006, truthful it is an integer we request to count.
Therefore, nan required integers successful nan fixed array are 242 and 30006.
Input: arr[] = [833, 3055, 8521, 360, 2202, 310, 2111]
Output: 3
Approach: Implement nan thought beneath to lick nan problem:
The problem is based connected Bitwise concept and tin beryllium solved by utilizing immoderate observations. For much explanation spot nan Concept of approach section.
Steps were taken to lick nan problem:
- Firstly, cipher factorials of integers from 1 to 9 arsenic nan digit sum will beryllium successful nan scope of [1, 9]. Similarly, shop digits are coming successful factorials.
- After that, find nan digit sum of arr[i] while it is greater than aliases adjacent to 10.
- Now, aft calculating its digit sum, past find nan factorial.
- At last, cheque if each digit of arr[i] is coming successful that factorial aliases not.
- Update nan count variable if each digit of arr[[i] is coming successful that factorial value.
Implementation of nan supra approach:
Java
Time Complexity: O(n * log(n))
Auxiliary Space: 0(1)