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.
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 <bits/stdc++.h> 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<Integer> digits = new HashSet<Integer>(); 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 |
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.
Recommended Posts:
- Find the average of k digits from the beginning and l digits from the end of the given number
- Check whether product of digits at even places is divisible by sum of digits at odd place of a number
- Number of times a number can be replaced by the sum of its digits until it only contains one digit
- Find a N-digit number such that it is not divisible by any of its digits
- Smallest N digit number with none of its digits as its divisor
- Find a N-digit number such that it is not divisible by any of its digits
- Finding sum of digits of a number until sum becomes single digit
- Find last five digits of a given five digit number raised to power five
- Smallest N digit number divisible by all possible prime digits
- Maximum of sum and product of digits until number is reduced to a single digit
- Count total number of N digit numbers such that the difference between sum of even and odd digits is 1
- Check if the sum of digits of number is divisible by all of its digits
- Reduce number to a single digit by subtracting adjacent digits repeatedly
- Check if the product of digit sum and its reverse equals the number or not
- Check if the first and last digit of the smallest number forms a prime
- Smallest N digit number whose sum of square of digits is a Perfect Square
- Check whether a number can be expressed as a product of single digit numbers
- Check if the Xor of the frequency of all digits of a number N is zero or not
- Check if all digits of a number divide it
- Check if the frequency of all the digits in a number is same
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.

