A matrix is constructed of size n*n. such that M_{i}_{,j}= i+j. Count the number of cells having value q.
Note: Assume, the array is in 1-based indexing.
 
Example 1:
Input:
n = 4, q = 7
Output:
2
Explanation:
Matrix becomes
2 3 4 5 
3 4 5 6 
4 5 6 7
5 6 7 8
The count of 7 is 2.
Example 2:
Input:
n = 5, q = 4
Output:
3
Explanation:
Matrix becomes
2 3 4 5 6 
3 4 5 6 7 
4 5 6 7 8 
5 6 7 8 9 
6 7 8 9 10 
The count of 4 is 3.
 
Your Task:
You don't need to read input or print anything. Your task is to complete the function sumMatrix() which takes 2 Integers n, and q as input and returns the count of q in the n*n matrix.
 
Expected Time Complexity: O(1)
Expected Auxiliary Space: O(1)
 
Constraints:
1 ≤ n,q ≤ 10^{18}

Difficulty: EASY
Tags: Matrices

Number theory

Data structures

Mathematics
Skill types: Data structures
Expected Time Complexity: O(1)
Expected Auxiliary Space: O(1)

Starter code:
#User function Template for python3



class Solution:

    def sumMatrix(self, n, q):

        # code here 

Solutions:
class Solution:

	def sumMatrix(self, n, q):
		if q == 1:
			return 0
		elif q <= n:
			return q - 1
		elif q > 2 * n:
			return 0
		else:
			return 2 * n - q + 1


class Solution:

	def sumMatrix(self, n, q):
		if q == 2 or q == n * 2:
			return 1
		elif q < 2 or q > n * 2:
			return 0
		else:
			x = min(n * 2 - q, q - 2)
			return x + 1


class Solution:

	def sumMatrix(self, n, q):
		m = n * 2
		h = n + 1
		if q >= 2 and q <= m:
			if q == h:
				return n
			elif q < h:
				return q - 1
			else:
				return m - q + 1
		else:
			return 0


class Solution:

	def sumMatrix(self, n, q):
		diff = abs(n + 1 - q)
		if diff < n:
			self.out = n - diff
			return self.out
		else:
			return 0


class Solution:

	def sumMatrix(self, n, q):
		size = 2 * n
		if size < q:
			return 0
		if n >= q:
			ans = q - 1
		else:
			ans = 2 * n - q + 1
		return ans


class Solution:

	def sumMatrix(self, n, q):
		if q > 2 * n:
			return 0
		mid = n + 1
		return n - abs(mid - q)


class Solution:

	def sumMatrix(self, n, q):
		count = 0
		min_i = max(1, q - n)
		max_i = min(q - 1, n)
		count = max(0, max_i - min_i + 1)
		return count


import math

class Solution:

	def sumMatrix(self, n, q):
		if q > 2 * n or q < 2:
			return 0
		if q < n + 1:
			return q - 1
		else:
			return 2 * n + 1 - q


class Solution:

	def sumMatrix(self, n, q):
		max = 2 * n
		midValue = n + 1
		if q == midValue:
			return n
		elif q < midValue and q > 1:
			return n - (midValue - q)
		elif q > midValue and q < max + 1:
			return n + (midValue - q)
		else:
			return 0


class Solution:

	def sumMatrix(self, n, q):
		if q > 2 * n or q < 2:
			return 0
		elif q >= 2 and q <= n + 1:
			return q - 1
		elif q > n + 1 and q <= 2 * n:
			return 2 * n - q + 1


class Solution:

	def sumMatrix(self, n, q):
		ct = n
		top = n + 1
		if q == top:
			return ct
		else:
			diff = abs(top - q)
			ct -= diff
		if ct > 0:
			return ct
		else:
			return 0


class Solution:

	def sumMatrix(self, n, q):
		ct = 0
		if q > n + n:
			return ct
		else:
			temp = n + 1
			time = abs(q - temp)
			ct = n - time
			return ct


class Solution:

	def sumMatrix(self, n, q):
		return max(0, n - abs(q - (n + 1)))


class Solution:

	def sumMatrix(self, n, q):
		i = 2
		j = 2 * n
		if q > j:
			return 0
		mid = (i + j) // 2
		if mid >= q:
			return q - 1
		else:
			return j - q + 1


class Solution:

	def sumMatrix(self, n, q):
		a = 2
		b = n * 2
		c = (a + b) // 2
		if q == c:
			return n
		elif q > n * 2:
			return 0
		else:
			k = abs(c - q)
			return n - k


class Solution:

	def sumMatrix(self, n, q):
		mini = 2
		maxi = 2 * n
		if mini <= q <= maxi:
			return min(abs(q - mini), abs(q - maxi)) + 1
		else:
			return 0


class Solution:

	def sumMatrix(self, n, q):
		count = n - abs(q - (n + 1))
		if count < 0:
			return 0
		else:
			return count


class Solution:

	def sumMatrix(self, n, q):
		a = min(q - 1, 2 * n - q + 1)
		if a <= 0:
			return 0
		return a


class Solution:

	def sumMatrix(self, n, q):
		if q < 2 or q > 2 * n:
			return 0
		if q == n + 1:
			return n
		i = q - 2
		r = n - 1
		if i < r:
			return i + 1
		if i > r:
			return 2 * r - i + 1


class Solution:

	def sumMatrix(self, n, q):
		n1 = n + 1
		q1 = abs(n1 - q)
		if n - q1 > 0:
			return n - q1
		else:
			return 0


class Solution:

	def sumMatrix(self, n, q):
		count = None
		if q < 2 or q > 2 * n:
			count = 0
		elif q <= n:
			count = q - 1
		else:
			count = 2 * n - q + 1
		return count


class Solution:

	def sumMatrix(self, n, q):
		if q < 2 or q > n + n:
			return 0
		else:
			return n - abs(n - q + 1)


import math

class Solution:

	def sumMatrix(self, n, q):
		x = n - abs(q - (n + 1))
		if x < 0:
			return 0
		else:
			return x