Jeny love Sweets so much. Now she is at famous restaurant and wants to eat M pieces of a particular sweet. Cost of nth sweet can only be determined by the determinant of matrix of order n x n, where n = 1 to M. The (i, j)th term of matrix is given as: A[i][j]= minimum(i, j) *(-1)^{((i-1)*n + (j-1))}.
Matrix indexes starts with 1. The task is to find the cost of M sweets.
Example 1:
Input: M = 1
Output: 1 
Explanation: Matrix of 1*1 is: |1|.
Determinant of matrix 1x1 = 1.
Cost of 1 sweet = 1.
Example 2:
Input: M = 2
Output: 0
Explanation: Matrix of 2*2 is:  |1 -1|
                                |1 -2|.
Determinant of matrix 2x2= -1.
Cost of 2 sweets  = 
Cost of 1st sweet + Cost of 2nd sweet = 1 + -1 = 0.
Your Task:  
You dont need to read input or print anything. Complete the function costOfSweets() which takes M as input parameter and returns the cost of M sweets.
Expected Time Complexity: O(1)
Expected Auxiliary Space: O(1)
Constraints:
1<= M <=10^{6}

Source: geeksforgeeks | Difficulty: MEDIUM
Tags: Mathematics

Starter code: #User function Template for python3

class Solution:

    def costOfSweets (self, M):

        # code here 

Solutions:
class Solution:

	def costOfSweets(self, M):
		return M - (M + 2) // 4 * 2


class Solution:

	def costOfSweets(self, M):
		return M - (M // 2 - M // 4) * 2
		return (2 * M + 2) // 4 if M % 4 in {0, 1} else (2 * M - 2) // 4


class Solution:

	def costOfSweets(self, n):
		if n & 1:
			n = n + 1
			if n % 4 == 0:
				return n // 2 - 1
			else:
				return n // 2
		elif n % 4 == 0:
			return n // 2
		else:
			return (n - 2) // 2


class Solution:

	def costOfSweets(self, M):
		if M % 4 == 1 or M % 4 == 0:
			return (M + (M + 2)) // 4
		return (M + (M - 2)) // 4