Given an array of n integers. We need to count all values of ‘k’ such that
arr[0] % k = arr[1] % k = ....... = arr[n-1] % k 
 
Example 1:
Input:
N=3
arr[] = {38, 6, 34} 
Output: 3
Explanation:
We can have values of k as 
1, 2 and 4.  
Example 2:
Input:
N=2
arr[] = {3, 2} 
Output: 1
Your Task:
Since, this is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function printEqualModNumbers() that takes array arr and integer n as parameters and returns the desired output.
Note- If values of k are infinite return -1.
 
Expected Time Complexity: O(N^{3/2}).
Expected Auxiliary Space: O(N).
 
Constraints:
1 ≤ N ≤ 10^{5}

Source: geeksforgeeks | Difficulty: EASY
Tags: Data structures

Mathematics

Starter code: #User function Template for python3


class Solution:
    def printEqualModNumbers(self,arr, n):
        #code here.

Solutions:
class Solution:

	def fun(i, arr):
		k = arr[0] % i
		for j in arr:
			if j % i != k:
				return False
		return True

	def printEqualModNumbers(self, arr, n):
		if n == 1:
			return -1
		a = max(arr)
		res = 0
		for i in range(1, a + 1):
			if Solution.fun(i, arr):
				res += 1
		if res == 0:
			return -1
		else:
			return res


class Solution:

	def printEqualModNumbers(self, arr, n):
		minum = max(arr)
		count = 1
		tcount = 0
		if len(arr) == 1:
			return -1
		for i in range(1, minum + 1):
			prev = arr[0] % i
			for j in range(1, len(arr)):
				if arr[j] % i == prev:
					count += 1
				else:
					break
			if count == len(arr):
				tcount += 1
			count = 1
		return tcount


class Solution:

	def printEqualModNumbers(self, arr, n):
		arr.sort()
		count = 0
		d = arr[n - 1] - arr[0]
		if d == 0:
			return -1
		v = []
		i = 1
		while i * i <= d:
			if d % i == 0:
				v.append(i)
				if i != d / i:
					v.append(d / i)
			i += 1
		for i in range(len(v)):
			temp = arr[0] % v[i]
			j = 1
			while j < n:
				if arr[j] % v[i] != temp:
					break
				j += 1
			if j == n:
				count += 1
		return count