Python3 Program for Mean of range in array Last Updated : 05 Sep, 2024 Comments Improve Suggest changes Like Article Like Report Given an array of n integers. You are given q queries. Write a program to print the floor value of mean in range l to r for each query in a new line.Examples : Input : arr[] = {1, 2, 3, 4, 5} q = 3 0 2 1 3 0 4Output : 2 3 3Here for 0 to 2 (1 + 2 + 3) / 3 = 2Input : arr[] = {6, 7, 8, 10} q = 2 0 3 1 2Output : 7 7Naive Approach: We can run loop for each query l to r and find sum and number of elements in range. After this we can print floor of mean for each query. Python3 # Python 3 program to find floor value # of mean in range l to r import math # To find mean of range in l to r def findMean(arr, l, r): # Both sum and count are # initialize to 0 sum, count = 0, 0 # To calculate sum and number # of elements in range l to r for i in range(l, r + 1): sum += arr[i] count += 1 # Calculate floor value of mean mean = math.floor(sum / count) # Returns mean of array # in range l to r return mean # Driver Code arr = [ 1, 2, 3, 4, 5 ] print(findMean(arr, 0, 2)) print(findMean(arr, 1, 3)) print(findMean(arr, 0, 4)) # This code is contributed # by PrinciRaj1992 Output : 233Time complexity: O(n*q) where q is the number of queries and n is the size of the array. Here in the above code q is 3 as the findMean function is used 3 times.Auxiliary Space: O(1)Efficient Approach: We can find sum of numbers using numbers using prefix sum. The prefixSum[i] denotes the sum of first i elements. So sum of numbers in range l to r will be prefixSum[r] - prefixSum[l-1]. Number of elements in range l to r will be r - l + 1. So we can now print mean of range l to r in O(1). Python3 # Python3 program to find floor value # of mean in range l to r import math as mt MAX = 1000005 prefixSum = [0 for i in range(MAX)] # To calculate prefixSum of array def calculatePrefixSum(arr, n): # Calculate prefix sum of array prefixSum[0] = arr[0] for i in range(1,n): prefixSum[i] = prefixSum[i - 1] + arr[i] # To return floor of mean # in range l to r def findMean(l, r): if (l == 0): return mt.floor(prefixSum[r] / (r + 1)) # Sum of elements in range l to # r is prefixSum[r] - prefixSum[l-1] # Number of elements in range # l to r is r - l + 1 return (mt.floor((prefixSum[r] - prefixSum[l - 1]) / (r - l + 1))) # Driver Code arr = [1, 2, 3, 4, 5] n = len(arr) calculatePrefixSum(arr, n) print(findMean(0, 2)) print(findMean(1, 3)) print(findMean(0, 4)) # This code is contributed by Mohit Kumar Output: 233Time complexity: O(n+q) where q is the number of queries and n is the size of the array. Here in the above code q is 3 as the findMean function is used 3 times.Auxiliary Space: O(k) where k=1000005.Please refer complete article on Mean of range in array for more details! Comment More infoAdvertise with us Next Article Python3 Program for Mean of range in array kartik Follow Improve Article Tags : Python Python Programs DSA Arrays array-range-queries prefix-sum +2 More Practice Tags : Arraysprefix-sumpython Similar Reads Python3 Program to Find the subarray with least average Given an array arr[] of size n and integer k such that k <= n.Examples : Input: arr[] = {3, 7, 90, 20, 10, 50, 40}, k = 3Output: Subarray between indexes 3 and 5The subarray {20, 10, 50} has the least average among all subarrays of size 3.Input: arr[] = {3, 7, 5, 20, -10, 0, 12}, k = 2Output: Sub 3 min read Python | Harmonic Mean of List While working with Python, we can have a problem in which we need to find harmonic mean of a list cumulative. This problem is common in Data Science domain. Letâs discuss certain ways in which this problem can be solved. Method #1 : Using loop + formula The simpler manner to approach this problem is 5 min read Average of Float Numbers - Python The task of calculating the average of float numbers in Python involves summing all the numbers in a given list and dividing the total by the number of elements in the list. For example, given a list of float numbers a = [6.1, 7.2, 3.3, 9.4, 10.6, 15.7], the goal is to compute the sum of the numbers 2 min read Mean of Tuple List - Python We are give list of tuples we need to find the mean of each tuples. For example, a = [(1, 2, 3), (4, 5, 6), (7, 8, 9)] we need to find mean of each tuples so that output should be (4.0, 5.0, 6.0).Using sum() and len()We can find the mean of a tuple list by summing corresponding elements using sum() 2 min read Python - Get Random Range Average Given range and Size of elements, extract random numbers within a range, and perform average of it. Input : N = 3, strt_num = 10, end_num = 15 Output : 13.58 Explanation : Random elements extracted between 10 and 15, averaging out to 13.58. Input : N = 2, strt_num = 13, end_num = 18 Output : 15.82 E 5 min read Python - Mean deviation of Elements Given a list, the task is to write a Python program to compute how deviated are each of them from its list mean. Examples: Input : test_list = [7, 5, 1, 2, 10, 3] Output : [2.333333333333333, 0.33333333333333304, 3.666666666666667, 2.666666666666667, 5.333333333333333, 1.666666666666667] Explanation 2 min read Python - Inner Nested Value List Mean in Dictionary Sometimes, while working with Python Dictionaries, we can have a problem in which we need to extract the mean of nested value lists in dictionary. This problem can have application in many domains including web development and competitive programming. Lets discuss certain ways in which this task can 5 min read Python - Dictionary Values Mean Given a dictionary, find the mean of all the values present. Input : test_dict = {"Gfg" : 4, "is" : 4, "Best" : 4, "for" : 4, "Geeks" : 4} Output : 4.0 Explanation : (4 + 4 + 4 + 4 + 4) / 4 = 4.0, hence mean. Input : test_dict = {"Gfg" : 5, "is" : 10, "Best" : 15} Output : 10.0 Explanation : Mean of 4 min read Find Sum and Average of List in Python Our goal is to find sum and average of List in Python. The simplest way to do is by using a built-in method sum() and len(). For example, list of numbers is, [10, 20, 30, 40, 50] the sum is the total of all these numbers and the average is the sum divided by the number of elements in the list.Using 2 min read Python - Rear elements Average in List Sometimes, while working with data, we can have a problem in which we need to perform the mean of all the rear elements that come after K. This can be an application in Mathematics and Data Science domain. Let us discuss certain ways in which this task can be performed. Method #1 : Using sum() + li 6 min read Like