# Check if given number contains a digit which is the average of all other digits

Given an integer N, the task is to check whether N contains a digit D such that it is the average of all other digits present in N.

Examples:

Input: N = 132
Output: Yes
Explanation:
Since, (1 + 3)/2 = 2.

Input: N = 436
Output: No
Explanation:
No such digit exists which is the average of all other digits.

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach:
To solve the problem, follow the steps below:

• Store the sum and count of digits of N.
• Store the digits in a Set .
• If sum%count is 0 and the integer sum/count is present in the Set, that is sum/count is a digit of N, then print Yes. Otherwise print No.

Below is the implementation of the above approach:

## C++

 `// C++ Program to check whether a ` `// given number contains a digit ` `// which is the average of all ` `// other digits ` `#include ` `using` `namespace` `std; ` ` `  `// Function which checks if a ` `// digits exists in n which ` `// is the average of all other digits ` `void` `check(``int` `n) ` `{ ` `    ``set<``int``> digits; ` ` `  `    ``int` `temp = n; ` `    ``int` `sum = 0; ` `    ``int` `count = 0; ` `    ``while` `(temp > 0) { ` `        ``// Calculate sum of ` `        ``// digits in n ` `        ``sum += temp % 10; ` `        ``// Store the digits ` `        ``digits.insert(temp % 10); ` `        ``// Increase the count ` `        ``// of digits in n ` `        ``count++; ` `        ``temp = temp / 10; ` `    ``} ` ` `  `    ``// If average of all digits ` `    ``// is an integer ` `    ``if` `(sum % count == 0 ` `        ``// If the average is a digit ` `        ``// in n ` `        ``&& digits.find(sum / count) ` `               ``!= digits.end()) ` `        ``cout << ``"Yes"` `<< endl; ` `    ``// Otherwise ` `    ``else` `        ``cout << ``"No"` `<< endl; ` `} ` ` `  `// Driver Code ` `int` `main() ` `{ ` `    ``int` `n = 42644; ` `    ``check(n); ` `} `

## Java

 `// Java program to check whether a ` `// given number contains a digit ` `// which is the average of all ` `// other digits ` `import` `java.util.*; ` ` `  `class` `GFG{ ` ` `  `// Function which checks if a ` `// digits exists in n which ` `// is the average of all other digits ` `static` `void` `check(``int` `n) ` `{ ` `    ``HashSet digits = ``new` `HashSet(); ` ` `  `    ``int` `temp = n; ` `    ``int` `sum = ``0``; ` `    ``int` `count = ``0``; ` `    ``while` `(temp > ``0``) ` `    ``{ ` `         `  `        ``// Calculate sum of ` `        ``// digits in n ` `        ``sum += temp % ``10``; ` `         `  `        ``// Store the digits ` `        ``digits.add(temp % ``10``); ` `         `  `        ``// Increase the count ` `        ``// of digits in n ` `        ``count++; ` `        ``temp = temp / ``10``; ` `    ``} ` ` `  `    ``// If average of all digits ` `    ``// is an integer ` `    ``if` `(sum % count == ``0` `&& ` `        ``digits.contains(sum / count)) ` `        ``System.out.print(``"Yes"` `+ ``"\n"``); ` `         `  `    ``// Otherwise ` `    ``else` `        ``System.out.print(``"No"` `+ ``"\n"``); ` `} ` ` `  `// Driver Code ` `public` `static` `void` `main(String[] args) ` `{ ` `    ``int` `n = ``42644``; ` `     `  `    ``check(n); ` `} ` `} ` ` `  `// This code is contributed by Rajput-Ji `

## Python3

 `# Python3 program to check whether a given  ` `# number contains a digit which is ` `# the average of all other digits ` ` `  `# Function which checks if a  ` `# digits exists in n which is  ` `# the average of all other digits  ` `def` `check (n): ` ` `  `    ``digits ``=` `set``() ` ` `  `    ``temp ``=` `n ` `    ``Sum` `=` `0` `    ``count ``=` `0` `     `  `    ``while``(temp > ``0``): ` ` `  `        ``# Calculate sum of  ` `        ``# digits in n ` `        ``Sum` `+``=` `temp ``%` `10` ` `  `        ``# Store digits ` `        ``digits.add(temp ``%` `10``) ` ` `  `        ``# Increase the count of  ` `        ``# digits in n ` `        ``count ``+``=` `1` `        ``temp ``=` `temp ``/``/` `10` ` `  `    ``# If average of all digits is integer ` `    ``if` `((``Sum` `%` `count ``=``=` `0``) ``and`  `        ``((``int``)(``Sum` `/` `count) ``in` `digits)): ` `        ``print``(``"Yes"``) ` `    ``else``: ` `        ``print``(``"No"``) ` ` `  `# Driver code ` `n ``=` `42644` ` `  `# Function calling ` `check(n)  ` ` `  `# This code is contributed by himanshu77 `

## C#

 `// C# program to check whether a given  ` `// number contains a digit which is the  ` `// average of all other digits ` `using` `System; ` `using` `System.Collections.Generic; ` ` `  `class` `GFG{ ` ` `  `// Function which checks if a ` `// digits exists in n which ` `// is the average of all other digits ` `static` `void` `check(``int` `n) ` `{ ` `    ``HashSet<``int``> digits = ``new` `HashSet<``int``>(); ` ` `  `    ``int` `temp = n; ` `    ``int` `sum = 0; ` `    ``int` `count = 0; ` `    ``while` `(temp > 0) ` `    ``{ ` `         `  `        ``// Calculate sum of ` `        ``// digits in n ` `        ``sum += temp % 10; ` `         `  `        ``// Store the digits ` `        ``digits.Add(temp % 10); ` `         `  `        ``// Increase the count ` `        ``// of digits in n ` `        ``count++; ` `        ``temp = temp / 10; ` `    ``} ` ` `  `    ``// If average of all digits ` `    ``// is an integer ` `    ``if` `(sum % count == 0 && ` `        ``digits.Contains(sum / count)) ` `        ``Console.Write(``"Yes"` `+ ``"\n"``); ` `         `  `    ``// Otherwise ` `    ``else` `        ``Console.Write(``"No"` `+ ``"\n"``); ` `} ` ` `  `// Driver Code ` `public` `static` `void` `Main(String[] args) ` `{ ` `    ``int` `n = 42644; ` `     `  `    ``check(n); ` `} ` `} ` ` `  `// This code is contributed by Rajput-Ji `

Output:

```Yes
```

Time Complexity: O(log10N)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Improved By : Rajput-Ji, himanshu77

Article Tags :

1

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.