Given a string of alphanumeric characters, the task is to check whether the average character of the string is present or not. Average character refers to the character corresponding to the ASCII value which is the floor of the average value of the ASCII values of all characters in the string. Examples:
Input: abcdef
Output: d
Yes
Explanation:
string = "abcdef"
ASCII values of a = 97, b=98,
c=99, d=100, e=101, f=101
Sum of these values is 597
Average is 99.5 ~ 100
Character of ASCII value 100 = d
Hence d is present in the string.
Input: MNFGH
Output: J
No
Approach: The approach to solve this problem is very simple. It can be solved in the following steps:
- Find the sum of ASCII values of all the characters from the given string.
- Find the average of the ASCII value as average = (sum / numberOfCharacters)
- Get the character of this average ASCII value. Print this character
- Check if this character is present in the string or not. Print Yes or No accordingly.
Below is the implementation of the above approach: Implementation:Â
C++
// CPP program to check if the average character// is present in the string or notÂ
#include <bits/stdc++.h>#include <math.h>using namespace std;Â
// Checks if the character is presentbool check_char(char* st, char ch){Â
    // Get the length of string    int l = strlen(st);Â
    // Iterate from i=0 to    // the length of the string    // to check if the character    // is present in the string    for (int i = 0; i < l; i++) {        if (st[i] == ch)            return true;    }    return false;}Â
// Finds the average character of the stringchar find_avg(char* st){Â Â Â Â int i, sm = 0;Â Â Â Â int l = strlen(st);Â Â Â Â char ch;Â
    for (i = 0; i < l; i++) {        ch = st[i];Â
        // Calculate the sum of ASCII        // values of each character        sm = sm + (int)(ch);    }Â
    // Calculate average of ascii values    int avg = (int)(floor(sm / l));Â
    // Convert the ASCII value to character    // and return it    return ((char)(avg));}Â
// Driver codeint main(){Â Â Â Â char st[] = "ag23sdfa";Â
    // Get the average character    char ch = find_avg(st);    cout << ch << endl;Â
    // Check if the average character    // is present in string or not    if (check_char(st, ch) == true)        cout << "Yes";    else        cout << "No";    return 0;} |
Java
// Java program to check if the average character// is present in the string or notÂ
import java.math.*;Â
class GFG {Â
    // Checks if the character is present    static boolean check_char(String st, char ch)    {Â
        // Get the length of string        int l = st.length();Â
        // Iterate from i=0 to        // the length of the string        // to check if the character        // is present in the string        for (int i = 0; i < l; i++) {            if (st.charAt(i) == ch)                return true;        }        return false;    }Â
    // Finds the average character of the string    static char find_avg(String st)    {        int i, sm = 0;        int l = st.length();        char ch;Â
        for (i = 0; i < l; i++) {            ch = st.charAt(i);Â
            // Calculate the sum of ASCII            // values of each character            sm = sm + (int)(ch);        }Â
        // Calculate the average of ASCII values        int avg = (int)(Math.floor(sm / l));Â
        // Convert the ASCII value to character        // and return it        return ((char)(avg));    }Â
    // Driver code    public static void main(String[] args)    {        String st = "ag23sdfa";Â
        // Get the average character        char ch = find_avg(st);        System.out.println(ch);Â
        // Check if the average character        // is present in string or not        if (check_char(st, ch) == true)            System.out.println("Yes");        else            System.out.println("No");                 }} |
Python3
# Python 3 program to check if the average # character is present in the string or notfrom math import floorÂ
# Checks if the character is presentdef check_char(st, ch):         # Get the length of string    l = len(st)Â
    # Iterate from i=0 to    # the length of the string    # to check if the character    # is present in the string    for i in range(l):        if (st[i] == ch):            return TrueÂ
    return FalseÂ
# Finds the average character# of the stringdef find_avg(st):Â Â Â Â sm = 0Â Â Â Â l = len(st)Â
    for i in range(l):        ch = st[i]Â
        # Calculate the sum of ASCII        # values of each character        sm = sm + ord(ch)Â
    # Calculate average of ascii values    avg = int(floor(sm / l))Â
    # Convert the ASCII value to character    # and return it    return (chr(avg))Â
# Driver codeif __name__ == '__main__':Â Â Â Â st = "ag23sdfa"Â
    # Get the average character    ch = find_avg(st)    print(ch)         # Check if the average character    # is present in string or not    if (check_char(st, ch) == True):        print("Yes")    else:        print("No")Â
# This code is contributed by# Surendra_Gangwar |
C#
// C# program to check if the average character// is present in the string or notusing System;Â
class GFG {Â
    // Checks if the character is present    static bool check_char(string st, char ch)    {Â
        // Get the length of string        int l = st.Length;Â
        // Iterate from i=0 to        // the length of the string        // to check if the character        // is present in the string        for (int i = 0; i < l; i++)         {            if (st[i] == ch)                return true;        }        return false;    }Â
    // Finds the average character of the string    static char find_avg(string st)    {        int i, sm = 0;        int l = st.Length;        char ch;Â
        for (i = 0; i < l; i++)        {            ch = st[i];Â
            // Calculate the sum of ASCII            // values of each character            sm = sm + (int)(ch);        }Â
        // Calculate the average of ASCII values        int avg = (int)(Math.Floor((double)(sm / l)));Â
        // Convert the ASCII value to character        // and return it        return ((char)(avg));    }Â
    // Driver code    public static void Main()    {        string st = "ag23sdfa";Â
        // Get the average character        char ch = find_avg(st);        Console.WriteLine(ch);Â
        // Check if the average character        // is present in string or not        if (check_char(st, ch) == true)            Console.WriteLine("Yes");        else            Console.WriteLine("No");                 }}Â
// This code is contributed by Akanksha Rai |
Javascript
<script>// Javascript program to check if the average character// is present in the string or notÂ
// Checks if the character is presentfunction check_char(st,ch){Â
    // Get the length of string    let l = st.length;Â
    // Iterate from i=0 to    // the length of the string    // to check if the character    // is present in the string    for (let i = 0; i < l; i++) {        if (st[i] == ch)            return true;    }    return false;}Â
// Finds the average character of the stringfunction find_avg(st){Â Â Â Â let i;Â Â Â Â let sm = 0;Â Â Â Â let l = st.length;Â Â Â Â let ch;Â
    for (i = 0; i < l; i++) {        ch = st[i];Â
        // Calculate the sum of ASCII        // values of each character        sm = sm + (ch.charCodeAt(0));    }Â
    // Calculate average of ascii values    let avg = parseInt(Math.floor(sm / l));Â
    // Convert the ASCII value to character    // and return it    return (String.fromCharCode(avg));}Â
    let st = "ag23sdfa";Â
    // Get the average character    let ch = (find_avg(st));    console.log(find_avg(st));         // Check if the average character    // is present in string or not    if (check_char(st, ch) == true)        console.log("Yes");    else        console.log("No") ;                 // This code is contributed by akashish__Â
</script> |
Y No
Time complexity: O(N) where N is size of given string
Auxiliary Space: O(1)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!
