You are given an integer 'n' which denote the number of elements in an array a[ ] and an integer 'x'. You have to calculate the average of element a[i] and x and find out if that number exist in array or not. Do it for all the elements of array and store the count result in another array for each index i.
Note: Always take the floor value of the average.
Example 1:
Input : arr[ ] = {2, 4, 8, 6, 2} and X = 2
Output : 2 0 0 1 2
Explanation:
value of n is 5 and that of x is 2. 
The array is 2 4 8 6 2. We take x 
i.e. 2 and take average with a[0] 
whch is equal to 2. We found 2 resides 
in array at two positions (1st and 5th 
element) thus storing 2 in another 
array at 0th index. Similarly do for all 
elements and store the count in second 
array.
Example 2:
Input : arr[ ] = {9, 5, 2, 4, 0, 3} 
        and X = 3 
Output :  0 1 1 1 0 1 
 
Your Task:
This is a function problem. The input is already taken care of by the driver code. You only need to complete the function countArray() that takes an array (arr), sizeOfArray (n), an integer X, and return the resultant array in which you have to calculate the number of times the average of a[i] and x occurs in the array for each index i. The driver code takes care of the printing.
Expected Time Complexity: O(N^{2}).
Expected Auxiliary Space: O(1).
Constraints:
1 ≤ N ≤ 100
1 ≤ X ≤ 100
1 ≤ a[i] ≤ 100

Source: geeksforgeeks | Difficulty: EASY
Tags: Data structures

Mathematics

Starter code: #User function Template for python3



class Solution:

    def countArray (self, arr, n, x) : 

        #Complete the function



Solutions:
class Solution:

	def countArray(self, arr, n, x):
		k = []
		t = []
		for i in range(0, n):
			k.append(arr[i])
		for i in range(0, n):
			g = (k[i] + x) // 2
			if k.count(g) > 0:
				t.append(k.count(g))
			else:
				t.append(0)
		return t


class Solution:

	def countArray(self, arr, n, x):
		A = []
		for i in range(n):
			y = (arr[i] + x) // 2
			c = 0
			for j in range(n):
				if y == arr[j]:
					c += 1
			A.append(c)
		return A


class Solution:

	def countArray(self, arr, n, x):
		k = []
		for i in range(n):
			av = (x + arr[i]) // 2
			if av not in arr:
				k.append(0)
			else:
				k.append(arr.count(av))
		return k


class Solution:

	def countArray(self, arr, n, x):
		l = []
		for i in range(n):
			a = (arr[i] + x) // 2
			b = arr.count(a)
			l.append(b)
		return l


class Solution:

	def countArray(self, arr, n, x):
		st = []
		for i in range(n):
			count = 0
			y = (arr[i] + x) // 2
			if y in arr:
				count = arr.count(y)
			st.append(count)
		return st


class Solution:

	def countArray(self, arr, n, x):
		return [arr.count((i + x) // 2) for i in arr]


class Solution:

	def countArray(self, arr, n, x):
		l = []
		for i in arr:
			c = 0
			i = int(i)
			k = (i + x) // 2
			if k in arr:
				c = arr.count(k)
			l.append(c)
		return l


class Solution:

	def countArray(self, arr, n, x):
		avg = 0
		lst = []
		for i in arr:
			avg = (i + x) // 2
			y = arr.count(avg)
			lst.append(y)
		return lst


class Solution:

	def countArray(self, arr, n, x):
		cr = []
		for i in range(0, n):
			avg = (arr[i] + x) / 2
			c = 0
			for j in range(0, n):
				if int(avg) == arr[j]:
					c += 1
			cr.append(c)
		return cr


from collections import Counter

class Solution:

	def countArray(self, arr, n, x):
		store = Counter(arr)
		output = []
		for item in arr:
			output.append(store[(item + x) // 2])
		return output


import math

class Solution:

	def countArray(self, arr, n, x):
		ans = [0] * n
		for i in range(len(arr)):
			avg = (x + arr[i]) / 2
			avg = int(math.floor(avg))
			count = 0
			for j in arr:
				if avg == j:
					count += 1
			ans[i] = count
		return ans


class Solution:

	def countArray(self, arr, n, x):
		res = [-1] * n
		for i in range(n):
			avg = (arr[i] + x) // 2
			if avg not in arr:
				res[i] = 0
			else:
				res[i] = arr.count(avg)
		return res


class Solution:

	def countArray(self, arr, n, x):
		output = []
		for number in arr:
			import math
			average = math.floor((number + x) / 2)
			if average in arr:
				output.append(arr.count(average))
			else:
				output.append(0)
		return output


class Solution:

	def countArray(self, arr, n, x):
		freq = {}
		for i in arr:
			if i not in freq:
				freq[i] = 1
			else:
				freq[i] += 1
		res = [0] * n
		for i in range(n):
			temp = (arr[i] + x) // 2
			if temp in freq:
				res[i] = freq[temp]
		return res


class Solution:

	def countArray(self, arr, n, x):
		l = []
		for i in arr:
			a = (i + x) // 2
			c = 0
			for j in range(n):
				if a == arr[j]:
					c += 1
			l.append(c)
		return l


class Solution:

	def countArray(self, arr, n, x):
		ans = [0] * n
		for i in range(n):
			avg = (arr[i] + x) // 2
			count = 0
			for j in range(n):
				if avg == arr[j]:
					count += 1
			ans[i] = count
		return ans


class Solution:

	def countArray(self, arr, n, x):
		result = []
		for num in arr:
			result.append(arr.count((num + x) // 2))
		return result


class Solution:

	def countArray(self, arr, n, x):
		l = []
		for i in range(n):
			avg = (arr[i] + x) // 2
			if avg in arr:
				l.append(arr.count(avg))
			else:
				l.append(0)
		return l


import math

class Solution:

	def countArray(self, arr, n, x):
		l = []
		for i in range(len(arr)):
			if (arr[i] + x) // 2 in arr:
				l.append(arr.count((arr[i] + x) // 2))
			else:
				l.append('0')
		return l


from collections import defaultdict

class Solution:

	def countArray(self, arr, n, x):
		d = defaultdict()
		a = []
		for i in arr:
			if int(i) in d:
				d[int(i)] += 1
			else:
				d[int(i)] = 1
		for i in arr:
			av = (int(i) + int(x)) // 2
			if av in arr:
				a.append(d[av])
			else:
				a.append(0)
		return a


class Solution:

	def countArray(self, arr, n, x):
		result = []
		for i in range(n):
			k = 0
			p = (arr[i] + x) // 2
			for j in range(n):
				if p == arr[j]:
					k += 1
			result.append(k)
		return result


class Solution:

	def countArray(self, arr, n, x):
		a = []
		for i in arr:
			avg = (i + x) // 2
			count = 0
			for j in arr:
				if avg == j:
					count = count + 1
			a.append(count)
		return a


class Solution:

	def countArray(self, arr, n, x):
		l1 = []
		for i in range(n):
			l1.append(arr.count((arr[i] + x) // 2))
		return l1


class Solution:

	def countArray(self, arr, n, x):
		Arr = []
		for i in range(n):
			avg = (arr[i] + x) // 2
			Arr.append(arr.count(avg))
		return Arr


import math

class Solution:

	def countArray(self, arr, n, x):
		c = []
		for i in arr:
			a = math.floor((i + x) / 2)
			if a in arr:
				c.append(arr.count(a))
			else:
				c.append(0)
		return c


class Solution:

	def countArray(self, arr, n, x):
		result = []
		for i in range(n):
			s = (arr[i] + x) // 2
			result.append(arr.count(s))
		return result


from collections import Counter

class Solution:

	def countArray(self, arr, n, x):
		l = []
		for i in arr:
			avg = (i + x) // 2
			l.append(avg)
		c = Counter(arr)
		l1 = []
		for i in l:
			if i in arr:
				l1.append(c[i])
			else:
				l1.append(0)
		return l1


import math

class Solution:

	def countArray(self, arr, n, x):
		k = list()
		avg = 0
		for i in arr:
			avg = math.floor((i + x) / 2)
			k.append(arr.count(avg))
		return k


class Solution:

	def countArray(self, arr, n, x):
		count_arr = []
		for i in arr:
			avg = (i + x) // 2
			count_arr.append(arr.count(avg))
		return count_arr


class Solution:

	def countArray(self, arr, n, x):
		list = []
		for i in range(n):
			z = (arr[i] + x) // 2
			c = arr.count(z)
			list.append(c)
		return list


class Solution:

	def countArray(self, arr, n, x):
		d = {}
		for j in arr:
			if j in d:
				d[j] += 1
			else:
				d[j] = 1
		for j in range(n):
			a = (arr[j] + x) // 2
			if a in d:
				arr[j] = d[a]
			else:
				arr[j] = 0
		return arr


class Solution:

	def countArray(self, arr, n, x):
		d = {}
		for i in arr:
			if i in d:
				d[i] += 1
			else:
				d[i] = 1
		ans = []
		for i in arr:
			c = (i + x) // 2
			if c in d:
				ans.append(d[c])
			else:
				ans.append(0)
		return ans


class Solution:

	def countArray(self, arr, n, x):
		arr1 = []
		for i in range(len(arr)):
			add = arr[i] + x
			avg = add // 2
			c = 0
			for j in range(len(arr)):
				if arr[j] == avg:
					c += 1
			arr1.append(c)
		return arr1


class Solution:

	def countArray(self, arr, n, x):
		brr = [0] * n
		for i in range(n):
			brr[i] = arr.count((arr[i] + x) // 2)
		return brr


class Solution:

	def countArray(self, arr, n, x):
		return [arr.count(int((i + x) / 2)) if int((i + x) / 2) in arr else 0 for i in arr]


class Solution:

	def countArray(self, arr, n, x):
		lis = []
		for i in arr:
			rem = (i + x) // 2
			count = 0
			for j in arr:
				if j == rem:
					count += 1
			lis.append(count)
		return lis


class Solution:

	def countArray(self, arr, n, x):
		lis = [0 for i in range(n)]
		for i in range(n):
			d = (x + arr[i]) // 2
			c = 1
			for j in range(n):
				if d == arr[j]:
					lis[i] = c
					c += 1
		return lis


class Solution:

	def countArray(self, arr, n, x):
		l = []
		for i in range(len(arr)):
			t = (arr[i] + x) // 2
			c = arr.count(t)
			l.append(c)
		return l


import math

class Solution:

	def countArray(self, arr, n, x):
		l = []
		l1 = []
		for i in range(n):
			avg = math.floor((arr[i] + x) / 2)
			l.append(avg)
		for j in l:
			if j in arr:
				x = arr.count(j)
			else:
				x = 0
			l1.append(x)
		return l1


class Solution:

	def countArray(self, arr, n, x):
		a = []
		for i in arr:
			avg = (i + x) // 2
			c = arr.count(avg)
			a.append(str(c))
		return ''.join(a)


import math

class Solution:

	def countArray(self, arr, n, x):
		newlist = []
		for i in range(n):
			p = math.floor((arr[i] + x) / 2)
			if p in arr:
				newlist.append(arr.count(p))
			else:
				newlist.append(0)
		return ''.join(map(str, newlist))


class Solution:

	def countArray(self, arr, n, x):
		l = []
		for y in arr:
			a = (x + y) // 2
			if a in arr:
				l.append(arr.count(a))
			else:
				l.append(0)
		return l


class Solution:

	def countArray(self, arr, n, x):
		li = []
		for i in range(n):
			t = (arr[i] + x) // 2
			count = 0
			for j in range(n):
				if arr[j] == t:
					count += 1
			li.append(count)
		return li


class Solution:

	def countArray(self, arr, n, t):
		res = []
		dis = {}
		for x in arr:
			if x in dis:
				dis[x] += 1
			else:
				dis[x] = 1
		for x in arr:
			r = (x + t) // 2
			if r in arr:
				res.append(dis[r])
			else:
				res.append(0)
		return res


class Solution:

	def countArray(self, arr, n, x):
		ans = []
		for i in range(0, len(arr)):
			avg = (arr[i] + x) // 2
			if avg in arr:
				a = arr.count(avg)
				ans.append(a)
			else:
				ans.append(0)
		return ans


class Solution:

	def countArray(self, arr, n, x):
		dic = {}
		for i in arr:
			if i in dic:
				dic[i] += 1
			else:
				dic[i] = 1
		for (i, v) in enumerate(arr):
			val = v + (x - v) // 2
			if val in dic:
				arr[i] = dic[val]
			else:
				arr[i] = 0
		return arr


from collections import Counter

class Solution:

	def countArray(self, arr, n, x):
		freq = Counter(arr)
		result = []
		for i in arr:
			avg = int((x + i) / 2)
			if avg in freq:
				result.append(freq[avg])
			else:
				result.append(0)
		return result


class Solution:

	def countArray(self, arr, n, x):
		ans = []
		for i in range(n):
			ave = (arr[i] + x) // 2
			c = 0
			for j in arr:
				if ave == j:
					c += 1
			ans.append(c)
		return ans


class Solution:

	def countArray(self, arr, n, x):
		res = []
		count = [0] * 101
		for i in range(n):
			count[arr[i]] += 1
		for i in range(n):
			avg = (arr[i] + x) // 2
			res.append(count[avg])
		return res


class Solution:

	def countArray(self, arr, n, x):
		freq = {}
		for i in range(n):
			if arr[i] in freq:
				freq[arr[i]] += 1
			else:
				freq[arr[i]] = 1
		avg_arr = n * [0]
		for i in range(n):
			avg = (arr[i] + x) // 2
			if avg in freq:
				val = freq[avg]
				avg_arr[i] = val
		return avg_arr