Skip to content
Related Articles

Related Articles

Check if given number contains a digit which is the average of all other digits
  • Last Updated : 28 Apr, 2021

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

Javascript




<script>
 
// Javascript 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
function check(n)
{
    var digits = new Set();
 
    var temp = n;
    var sum = 0;
    var 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 = parseInt(temp / 10);
    }
 
    // If average of all digits
    // is an integer
    if (sum % count == 0
        // If the average is a digit
        // in n
        && digits.has(sum / count))
        document.write( "Yes" );
    // Otherwise
    else
        document.write( "No" );
}
 
// Driver Code
var n = 42644;
check(n);
 
</script>
Output: 
Yes

 

Time Complexity: O(log10N)
 

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :