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.
Input: N = 123456, K = 2, L = 3
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 = 3
Input: N = 456966, K = 1, L = 1
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:
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.
- Check if given number contains a digit which is the average of all other digits
- Find smallest number with given number of digits and sum of digits under given constraints
- Find the Largest number with given number of digits and sum of digits
- Find temperature of missing days using given sum and average
- Find the ratio of number of elements in two Arrays from their individual and combined average
- Numbers of Length N having digits A and B and whose sum of digits contain only digits A and B
- Find the deleted value from the array when average of original elements is given
- Average of odd numbers till a given odd number
- Average of even numbers till a given even number
- Count of numbers from range [L, R] that end with any of the given digits
- Maximize the given number by replacing a segment of digits with the alternate digits given
- Number of digits in the nth number made of given four digits
- Find the subarray with least average
- Find the average of first N natural numbers
- Program to find simple moving average
- Find average of two numbers using bit operation
- Find the Batting Average of a batsman
- Count of integers in a range which have even number of odd digits and odd number of even digits
- Number formed by deleting digits such that sum of the digits becomes even and the number odd
- Minimum digits to be removed to make either all digits or alternating digits 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 firstname.lastname@example.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.