You are given an integer N. Consider an array arr having N elements where arr[i] = 2*i+1. (The array is 0-indexed)
You are allowed to perform the given operation on the array any number of times:
1) Select two indices i and j and increase arr[i] by 1 and decrease arr[j] by 1.
Your task is to find the minimum number of such operations required to make all the elements of the array equal.
Example 1:
Input:
N = 3
Output:
2
Explanation:
Initially the array is {1, 3, 5}. If we perform
the operation once on the indices 0 and 2, the 
resulting array will be {2, 3, 4}. If we again 
perform the operation once on the indices 0
and 2, the resulting array will be {3, 3, 3}.
Hence, the minimum operations required is 2
in this case. 
Example 2:
Input: 
N = 2
Output:
1
Explanation: 
The array initially is {1, 3}. After performing 
an operation the array will be {2, 2}. Hence,
the answer is 1 in this case.
Your Task:
Complete the function minOperations() which takes the integer N as the input parameter, and returns the minimum operations required to make all the array elements equal.
Expected Time Complexity: O(1)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{9}

Source: geeksforgeeks | Difficulty: EASY
Tags: Data structures

Mathematics

Starter code: #User function Template for python3



class Solution:

    def minOperations(self, N):

        # Code here

Solutions:
class Solution:

	def minOperations(self, N):
		a = N - 1
		mid = int(N / 2)
		if a % 2 == 0:
			count = int(mid * (mid + 1))
		else:
			count = int(mid * mid)
		return count


class Solution:

	def minOperations(self, N):
		temp = int(N / 2)
		if N % 2 == 0:
			return temp ** 2
		else:
			return temp ** 2 + temp


class Solution:

	def minOperations(self, N):
		if N == 1:
			return 1
		if N % 2 == 0:
			mid = N // 2
			res = mid * mid
		else:
			mid = N // 2
			res = mid * (mid + 1)
		return res


class Solution:

	def minOperations(self, n):
		return n ** 2 // 4


class Solution:

	def minOperations(self, N):
		n = N // 2
		if N % 2 != 0:
			v = n * (n + 1)
		else:
			v = n * n
		return v


class Solution:

	def minOperations(self, N):
		return (N + 1) // 2 * (N // 2)


class Solution:

	def minOperations(self, N):
		return N * N // 4


class Solution:

	def minOperations(self, N):
		M = N
		if M % 2 == 0:
			M //= 2
			return M * M
		else:
			M //= 2
			M *= M + 1
			return M


class Solution:

	def minOperations(self, N):
		operations = 0
		mid = N // 2
		if N % 2 != 0:
			n = N - mid - 1
			operations = n * (n + 1)
		else:
			n = N - mid
			operations = n ** 2
		return operations


class Solution:

	def minOperations(self, N):
		last = 2 * (N - 1) + 1
		mid = (last + 1) // 2
		if mid % 2 == 0:
			return int(mid * mid // 4)
		else:
			return int((mid - 1) * (mid + 1) // 4)


class Solution:

	def minOperations(self, N):
		k = N // 2
		if N % 2 == 0:
			return k * k
		return k * (k + 1)


class Solution:

	def minOperations(self, N):
		i = N // 2
		return (N - i) * i


class Solution:

	def minOperations(self, N):
		if N % 2 == 1:
			return int(int((N - 1) / 2) * int((N + 1) / 2))
		else:
			n = int(N / 2 - 1)
			return int(1 + int(N / 2 - 1) * int(N / 2 + 1))


class Solution:

	def minOperations(self, n: int) -> int:
		return n * n // 4


class Solution:

	def minOperations(self, N):
		return N * N // 4
		if N % 2 == 0:
			return N * N // 4
		else:
			return N * N // 4


class Solution:

	def minOperations(self, N):
		halfoddlength = N // 2
		midelement = N * halfoddlength
		sumofhalfodd = halfoddlength * halfoddlength
		return midelement - sumofhalfodd


class Solution:

	def minOperations(self, N):
		n = N // 2
		s = n * (2 + (n - 1) * 2) // 2
		ans = abs(s - N * n)
		return ans


class Solution:

	def minOperations(self, N):
		if N < 2:
			return 1
		m = N // 2
		return m * N - m * m


class Solution:

	def minOperations(self, N):
		self.number = N
		return self.number * (self.number // 2) - (self.number // 2) ** 2


class Solution:

	def minOperations(self, N):
		last = (N - 1) * 2 + 1
		total_level = last - N
		two_time_i = total_level // 2
		sum_even = two_time_i * (two_time_i + 1)
		if N % 2 == 0:
			sum_even += two_time_i + 1
		return sum_even


class Solution:

	def minOperations(self, N):
		mid = N // 2
		return mid * N - mid * mid


class Solution:

	def minOperations(self, N):
		a = N // 2
		if N % 2 == 0:
			return a * a
		return a * (a + 1)


class Solution:

	def minOperations(self, n):
		return (n + 1) // 2 * (n // 2)
		a = []
		c = 0
		l = 0
		for i in range(n):
			a.append(2 * i + 1)
		if n % 2 == 0:
			c = n
		elif n % 2 == 1:
			c = a[n // 2]
		for i in range(n):
			l += abs(c - a[i])
		return l // 2


class Solution:

	def minOperations(self, n):
		if n & 1:
			cur = n // 2 * (1 + n // 2)
			return cur
		else:
			cur = n // 2 * (n // 2)
			return cur


class Solution:

	def minOperations(self, N):
		a = N // 2
		p = N * a - a ** 2
		return p


class Solution:

	def minOperations(self, n):
		last = 2 * n - 1
		m = (1 + last) // 2
		if m % 2 == 0:
			return m * m // 4
		return (m - 1) * (m + 1) // 4


class Solution:

	def minOperations(self, N):
		x = N // 2
		r = N % 2
		return x * (x + r)


class Solution:

	def minOperations(self, N):
		if N & 1 == 0:
			return (N >> 1) * (N >> 1)
		return (N >> 1) * ((N >> 1) + 1)


class Solution:

	def minOperations(self, N):
		if N % 2 == 0:
			a = 1
		else:
			a = 2
		x = int(N // 2)
		y = int((2 * a + (x - 1) * 2) / 2)
		ans = x * y
		return int(ans)