Find the average of k digits from the beginning and l digits from the end of the given number - GeeksforGeeks

26 Mar, 2021
Given three integers N, K and L. The task is to find the average of the first K digits and the last L digits of the given number N without any digit overlapping.Examples:  
Input: N = 123456, K = 2, L = 3 Output: 3.0 Sum of first K digits will be 1 + 2 = 3 Sum of last L digits will be 4 + 5 + 6 = 15 Average = (3 + 15) / (2 + 3) = 18 / 5 = 3Input: N = 456966, K = 1, L = 1 Output: 5.0  
 
Approach: If the count of digits in n is less than (K + L) then it isn’t possible to find the average without digits overlapping and print -1 in that case. If that’s not the case, find the sum of the last L digits of N and store it in a variable say sum1 then find the sum of the first K digits of N and store it in sum2. Now, print the average as (sum1 + sum2) / (K + L).Below is the implementation of the above approach:  
C++
Java
Python3
C#
Javascript
// implementation of the approach#include <bits/stdc++.h>using namespace std; // Function to return the count// of digits in numint countDigits(int num){    int cnt = 0;    while (num > 0)    {        cnt++;        num /= 10;    }    return cnt;} // Function to return the sum// of first n digits of numint sumFromStart(int num, int n, int rem){     // Remove the unnecessary digits    num /= ((int)pow(10, rem));     int sum = 0;    while (num > 0)    {        sum += (num % 10);        num /= 10;    }    return sum;} // Function to return the sum// of the last n digits of numint sumFromEnd(int num, int n){    int sum = 0;    for (int i = 0; i < n; i++)    {        sum += (num % 10);        num /= 10;    }    return sum;} float getAverage(int n, int k, int l){     // If the average can't be calculated without    // using the same digit more than once    int totalDigits = countDigits(n);    if (totalDigits < (k + l))        return -1;     // Sum of the last l digits of n    int sum1 = sumFromEnd(n, l);     // Sum of the first k digits of n    // (totalDigits - k) must be removed from the    // end of the number to get the remaining    // k digits from the beginning    int sum2 = sumFromStart(n, k, totalDigits - k);     // Return the average    return ((float)(sum1 + sum2) /             (float)(k + l));} // Driver codeint main(){    int n = 123456, k = 2, l = 3;    cout << getAverage(n, k, l);     return 0;} // This code is contributed by PrinciRaj1992
// Java implementation of the approachclass GFG {     // Function to return the count    // of digits in num    public static int countDigits(int num)    {        int cnt = 0;        while (num > 0) {            cnt++;            num /= 10;        }        return cnt;    }     // Function to return the sum    // of first n digits of num    public static int sumFromStart(int num, int n, int rem)    {         // Remove the unnecessary digits        num /= ((int)Math.pow(10, rem));         int sum = 0;        while (num > 0) {            sum += (num % 10);            num /= 10;        }        return sum;    }     // Function to return the sum    // of the last n digits of num    public static int sumFromEnd(int num, int n)    {        int sum = 0;        for (int i = 0; i < n; i++) {            sum += (num % 10);            num /= 10;        }        return sum;    }     public static float getAverage(int n, int k, int l)    {         // If the average can't be calculated without        // using the same digit more than once        int totalDigits = countDigits(n);        if (totalDigits < (k + l))            return -1;         // Sum of the last l digits of n        int sum1 = sumFromEnd(n, l);         // Sum of the first k digits of n        // (totalDigits - k) must be removed from the        // end of the number to get the remaining        // k digits from the beginning        int sum2 = sumFromStart(n, k, totalDigits - k);         // Return the average        return ((float)(sum1 + sum2) / (float)(k + l));    }     // Driver code    public static void main(String args[])    {        int n = 123456, k = 2, l = 3;        System.out.print(getAverage(n, k, l));    }}
# implementation of the approachfrom math import pow # Function to return the count# of digits in numdef countDigits(num):    cnt = 0    while (num > 0):        cnt += 1        num //= 10    return cnt # Function to return the sum# of first n digits of numdef sumFromStart(num, n, rem):         # Remove the unnecessary digits    num //= pow(10, rem)     sum = 0    while (num > 0):        sum += (num % 10)        num //= 10    return sum # Function to return the sum# of the last n digits of numdef sumFromEnd(num, n):    sum = 0    for i in range(n):        sum += (num % 10)        num //= 10         return sum def getAverage(n, k, l):         # If the average can't be calculated without    # using the same digit more than once    totalDigits = countDigits(n)    if (totalDigits < (k + l)):        return -1     # Sum of the last l digits of n    sum1 = sumFromEnd(n, l)     # Sum of the first k digits of n    # (totalDigits - k) must be removed from the    # end of the number to get the remaining    # k digits from the beginning    sum2 = sumFromStart(n, k, totalDigits - k)     # Return the average    return (sum1 + sum2) / (k + l) # Driver codeif __name__ == '__main__':    n = 123456    k = 2    l = 3    print(getAverage(n, k, l)) # This code is contributed by# Surendra_Gangwar
// C# implementation of the approachusing System; class GFG{     // Function to return the count    // of digits in num    public static int countDigits(int num)    {        int cnt = 0;        while (num > 0)        {            cnt++;            num /= 10;        }        return cnt;    }     // Function to return the sum    // of first n digits of num    public static int sumFromStart(int num,                                   int n, int rem)    {         // Remove the unnecessary digits        num /= ((int)Math.Pow(10, rem));         int sum = 0;        while (num > 0)        {            sum += (num % 10);            num /= 10;        }        return sum;    }     // Function to return the sum    // of the last n digits of num    public static int sumFromEnd(int num, int n)    {        int sum = 0;        for (int i = 0; i < n; i++)        {            sum += (num % 10);            num /= 10;        }        return sum;    }     public static float getAverage(int n, int k, int l)    {         // If the average can't be calculated without        // using the same digit more than once        int totalDigits = countDigits(n);        if (totalDigits < (k + l))            return -1;         // Sum of the last l digits of n        int sum1 = sumFromEnd(n, l);         // Sum of the first k digits of n        // (totalDigits - k) must be removed from the        // end of the number to get the remaining        // k digits from the beginning        int sum2 = sumFromStart(n, k, totalDigits - k);         // Return the average        return ((float)(sum1 + sum2) /                (float)(k + l));    }     // Driver code    public static void Main(String []args)    {        int n = 123456, k = 2, l = 3;        Console.WriteLine(getAverage(n, k, l));    }} // This code is contributed by Princi Singh
<script>// javascript implementation of the approach        // Function to return the count    // of digits in num    function countDigits(num)    {        var cnt = 0;        while (num > 0)        {            cnt++;            num = parseInt(num/10);        }        return cnt;    }     // Function to return the sum    // of first n digits of num    function sumFromStart(num, n, rem)    {         // Remove the unnecessary digits        num = (parseInt( num/Math.pow(10, rem)));         var sum = 0;        while (num > 0)        {            sum += (num % 10);            num = parseInt(num/10);        }        return sum;    }     // Function to return the sum    // of the last n digits of num    function sumFromEnd(num , n)    {        var sum = 0;        for (i = 0; i < n; i++)        {            sum += (num % 10);            num = parseInt(num/10);        }        return sum;    }     function getAverage(n , k , l) {         // If the average can't be calculated without        // using the same digit more than once        var totalDigits = countDigits(n);        if (totalDigits < (k + l))            return -1;         // Sum of the last l digits of n        var sum1 = sumFromEnd(n, l);         // Sum of the first k digits of n        // (totalDigits - k) must be removed from the        // end of the number to get the remaining        // k digits from the beginning        var sum2 = sumFromStart(n, k, totalDigits - k);         // Return the average        return ( (sum1 + sum2) /  (k + l));    }     // Driver code    var n = 123456, k = 2, l = 3;    document.write(getAverage(n, k, l)); // This code is contributed by Rajput-Ji</script>
3.6
 
princi singh
princiraj1992
SURENDRA_GANGWAR
Rajput-Ji
Numbers
Mathematical
Mathematical
Numbers
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Algorithm to solve Rubik's Cube
Program to print prime numbers from 1 to N.
Fizz Buzz Implementation
Program to multiply two matrices
Modular multiplicative inverse
Check if a number is Palindrome
Find first and last digits of a number
Count ways to reach the n'th stair
Program to convert a given number to words
Find Union and Intersection of two unsorted arrays