Trends

Count of integer whose factorial of digit sum contains every digit of original number

Trending 1 year ago
beritaja.com

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

// Java algorithm for nan supra approach import java.util.*; class GFG { // Driver Code nationalist fixed void main(String[] args) { int n = 7; int[] arr = { 833, 3055, 8521, 360, 2202, 310, 2111 }; // Function Call System.out.println(funtionTOFindInt(arr, n)); } // Function to find number of elements nationalist fixed int funtionTOFindInt(int[] arr, int n) { // Factorial of nan digits from // 0 to 9 Set<Integer>[] factDigits = caller HashSet[10]; for (int one = 1; one < 10; i++) { int truth = factorial(i); factDigits[i] = caller HashSet<>(); while (fact != 0) { factDigits[i].add(fact % 10); truth /= 10; } } // Count integers int count = 0; for (int one = 0; one < n; i++) { int x = arr[i]; while (x >= 10) { x = getSum(x); } Set<Integer> digits = factDigits[x]; boolean emblem = true; while (arr[i] > 0) { int excavation = arr[i] % 10; if (!digits.contains(dig)) { emblem = false; break; } arr[i] /= 10; } if (flag) count++; } return count; } // Find nan factorial of nan number nationalist fixed int factorial(int n) { return (n == 1 || n == 0) ? 1 : n * factorial(n - 1); } // Find nan digit sum nationalist fixed int getSum(int n) { int sum = 0; while (n != 0) { sum = sum + n % 10; n = n / 10; } // Return nan sum return sum; } }

Time Complexity: O(n * log(n))
Auxiliary Space: 0(1)

Editor: Naga



Read other contents from Beritaja.com at
More Source



close