Given an array of N integers. Write a program to check whether an arithmetic progression can be formed using all the given elements. 
 
Example 1:
Input:
N=4
arr[] = { 0,12,4,8 }
Output: YES
Explanation: Rearrange given array as
{0, 4, 8, 12}  which forms an
arithmetic progression.
Example 2:
Input:
N=4
arr[] = {12, 40, 11, 20}
Output: NO
 
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 checkIsAP() that takes array arr and integer N as parameters and return true for "Yes" and false for "No".
 
Expected Time Complexity: O(NLogN).
Expected Auxiliary Space: O(1).
 
Constraints:
2 ≤ N ≤ 10^{5}

Source: geeksforgeeks | Difficulty: EASY
Tags: String algorithms

Data structures

Starter code: #User function Template for python3


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

Solutions:
class Solution:

	def checkIsAP(self, arr, n):
		arr.sort()
		cnt = 0
		if n <= 2:
			return True
		for i in range(1, n - 1):
			if abs(arr[i - 1] - arr[i]) == abs(arr[i] - arr[i + 1]):
				cnt += 1
			if cnt == n - 2:
				return True
		return False


class Solution:

	def checkIsAP(self, arr, n):
		arr.sort()
		d = arr[1] - arr[0]
		a = arr[0]
		for i in range(2, n):
			if arr[i] - arr[i - 1] != d:
				return False
		return True


class Solution:

	def checkIsAP(self, arr, n):
		arr.sort()
		d = arr[1] - arr[0]
		for i in range(1, n):
			if arr[i] - arr[i - 1] == d:
				pass
			else:
				return False
		return True


class Solution:

	def checkIsAP(self, arr, n):
		arr = sorted(arr)
		d = arr[1] - arr[0]
		if n == 2:
			return True
		else:
			for i in range(1, n - 1):
				if arr[i + 1] - arr[i] != d:
					return False
				if i == n - 2 and arr[i + 1] - arr[i] == d:
					return True


class Solution:

	def checkIsAP(self, arr, n):
		arr.sort()
		diff = arr[1] - arr[0]
		for i in range(n - 1):
			if arr[i + 1] - arr[i] != diff:
				return False
		return True


class Solution:

	def checkIsAP(self, arr, n):
		arr.sort()
		if n < 2:
			return False
		if n == 2:
			return True
		for i in range(n - 2):
			if arr[i] + arr[i + 2] == 2 * arr[i + 1]:
				continue
			else:
				return False
		return True


class Solution:

	def checkIsAP(self, arr, n):
		arr.sort()
		d = arr[1] - arr[0]
		for i in range(n - 1):
			if arr[i + 1] - arr[i] == d:
				f = 1
			else:
				return 0
		return f


class Solution:

	def checkIsAP(self, arr, n):
		arr.sort()
		sub = arr[0] - arr[1]
		for i in range(1, n - 1, 1):
			if sub != arr[i] - arr[i + 1]:
				return 0
				break
		return 1


class Solution:

	def checkIsAP(self, arr, n):
		arr = sorted(arr)
		d = arr[1] - arr[0]
		for i in range(2, n):
			if arr[i] - arr[i - 1] != d:
				return 0
		return 1


class Solution:

	def checkIsAP(self, arr, n):
		arr.sort()
		diff = arr[1] - arr[0]
		for i in range(len(arr) - 1):
			summ = arr[i + 1] - arr[i]
			if summ != diff:
				return 0
		return 1


class Solution:

	def checkIsAP(self, arr, n):
		first = min(arr)
		last = max(arr)
		d = (last - first) // (n - 1)
		setArr = set(arr)
		if d == 0:
			return True
		curr = first
		while curr <= last:
			if curr not in setArr:
				return False
			curr += d
		return True


class Solution:

	def checkIsAP(self, arr, n):
		arr.sort()
		d = arr[1] - arr[0]
		c = 0
		for j in range(1, n, 1):
			if arr[j] - arr[j - 1] == d:
				c += 1
		if c == n - 1:
			return True
		else:
			return False


class Solution:

	def checkIsAP(self, arr, n):
		A = sorted(arr)
		H = {}
		for i in range(0, len(A) - 1):
			H[A[i]] = A[i] + A[len(A) - i - 1]
		num = H[A[0]]
		for (key, v) in H.items():
			if v != num:
				return False
		return True


class Solution:

	def checkIsAP(self, arr, n):
		arr.sort()
		k = 0
		for i in range(n - 2):
			if arr[i + 1] - arr[i] != arr[i + 2] - arr[i + 1]:
				k = 2
				return 0
		return 1


class Solution:

	def checkIsAP(self, arr, n):
		arr.sort()
		k = 0
		for i in range(n - 2):
			if arr[i + 1] - arr[i] != arr[i + 2] - arr[i + 1]:
				k = 2
				break
		if k == 2:
			return False
		else:
			return True


class Solution:

	def checkIsAP(self, arr, n):
		h = {}
		smallest = float('inf')
		second_smallest = float('inf')
		for i in range(n):
			if arr[i] < smallest:
				second_smallest = smallest
				smallest = arr[i]
			elif arr[i] != smallest and arr[i] < second_smallest:
				second_smallest = arr[i]
			if arr[i] not in h:
				h[arr[i]] = 1
			else:
				h[arr[i]] += 1
		for i in h:
			if h[i] == n:
				return True
		diff = second_smallest - smallest
		for i in range(n - 1):
			if second_smallest not in h:
				return False
			second_smallest += diff
		return True


class Solution:

	def checkIsAP(self, arr, n):
		arr.sort()
		dif = arr[1] - arr[0]
		return all([arr[i + 1] - arr[i] == dif for i in range(n - 1)])


class Solution:

	def checkIsAP(self, arr, n):
		arr.sort()
		res = set()
		for i in range(1, n):
			res.add(arr[i] - arr[i - 1])
		if len(res) == 1:
			return True
		else:
			return False


class Solution:

	def checkIsAP(self, arr, n):
		arr.sort()
		x = int(n * (arr[0] + arr[n - 1]) / 2)
		if sum(arr) == x:
			return 1
		else:
			return 0


class Solution:

	def checkIsAP(self, arr, n):
		arr = sorted(arr)
		b = arr[1] - arr[0]
		for i in range(n - 1):
			if arr[i + 1] != arr[i] + b:
				return False
		return True


class Solution:

	def checkIsAP(self, arr, n):
		arr = sorted(arr)
		m = arr[1] - arr[0]
		for i in range(1, n):
			if arr[i] - arr[i - 1] != m:
				return 0
		return 1


class Solution:

	def checkIsAP(self, arr, n):
		arr = sorted(arr)
		a = arr[0]
		d = arr[1] - arr[0]
		for i in range(len(arr)):
			if arr[i] == a + i * d:
				continue
			else:
				return False
		return True


class Solution:

	def checkIsAP(self, arr, n):
		flag = True
		arr.sort()
		d = arr[1] - arr[0]
		for i in range(1, n):
			if arr[i] - arr[i - 1] != d:
				flag = False
				break
		if flag == False:
			return False
		return True


class Solution:

	def checkIsAP(self, arr, n):
		arr = sorted(arr)
		t = arr[1] - arr[0]
		for i in range(1, len(arr)):
			if arr[i] - arr[i - 1] != t:
				return False
		else:
			return True


class Solution:

	def checkIsAP(self, arr, n):
		c = 0
		arr.sort()
		d = arr[1] - arr[0]
		a = arr[0]
		for i in range(n):
			if arr[i] == a + i * d:
				c += 1
			else:
				break
		if c == n:
			return True
		else:
			return False


class Solution:

	def checkIsAP(self, arr, n):
		arr.sort()
		min = arr[1] - arr[0]
		l = len(arr)
		flag = 0
		for i in range(1, l - 1):
			if arr[i + 1] - arr[i] != min:
				flag = 1
				break
		if flag == 0:
			return True
		else:
			return False


class Solution:

	def checkIsAP(self, arr, n):
		l = []
		arr = sorted(arr)
		for i in range(1, n):
			d = arr[i] - arr[i - 1]
			l.append(d)
		return len(set(l)) == 1


class Solution:

	def checkIsAP(self, arr, n):
		arr.sort()
		if len(arr) == 2:
			return True
		else:
			c = arr[1] - arr[0]
			for i in range(1, len(arr) - 1):
				if arr[i + 1] - arr[i] != c:
					return False
			return True


class Solution:

	def checkIsAP(self, arr, n):
		arr.sort()
		d = arr[1] - arr[0]
		s = n / 2 * (2 * arr[0] + (n - 1) * d)
		ans = sum(arr)
		if s == ans:
			return True
		else:
			return False