A Professor conducts a Computer Science paper for N students. He had strictly instructed all students to sit according to their roll numbers. However when he started checking the papers, he found out that all the papers were randomly ordered because the students had sat randomly during the exam instead of sitting according to their roll numbers. The order is given in list of integers roll[ ]. The professor became very angry and he wanted to teach the students a lesson.
He decided to sort the papers according to roll numbers by Bubble Sort and count the number of swaps required for each and every student and deduct as many marks of a student as were the number of swaps required for that student. The marks of every student is given in list of integers marks[ ] in the order in which they were sitting. However he also has to maintain the class average greater than or equal to a set minimum avg, else he may lose his job. The Professor wants to know whether he should punish the students or save his job.
Example 1:
Input:
N = 5 
avg = 68
roll[] = {3, 2, 4, 1, 5}
marks[] = {50, 67, 89, 79, 58}
Output: 0
Explanation: Overall number of swaps required 
is 8. Hence the class average would become
(50 + 67 + 89 + 79 + 58 - 8) / 5 = 67
Since 67 is less than the required minimum (68),
he will not punish the class to save his Job.
Example 2:
Input:
N = 6 
avg = 67.5
roll[] = {5, 4, 1, 3, 2, 6}
marks[] = {78, 98, 86, 67, 78, 79}
Output: 1
Explanation: Overall number of swaps required 
is 16. Hence the class average would become
(78 + 98 + 86 + 67 + 78 + 79 - 16) / 6 = 78.33.
Since 78.33 is greater than the required 
minimum (67.5), he will punish the class and 
deduct the marks.
Your Task:
You don't need to read input or print anything. Your task is to complete the function shouldPunish() which takes the array roll[] describing the order of students while sitting for the exam, the array marks[] describing the marks of the students, an integer N denoting the number of students and an avg denoting the required minimum average that is to be maintained as input parameters and returns 1 if the professor will punish the students or 0 otherwise.
Expected Time Complexity: O(N^{2}).
Expected Auxiliary Space: O(1).
Constraints:
1 ≤ N ≤ 1000

Source: geeksforgeeks | Difficulty: EASY
Tags: Sorting

Starter code: #User function Template for python3



class Solution:

    def shouldPunish (self, roll, marks, n, avg):

        #  your code here

Solutions:
class Solution:

	def shouldPunish(self, roll, marks, n, avg):
		swap = 0
		for i in range(n):
			for j in range(n - i - 1):
				if roll[j] > roll[j + 1]:
					(roll[j], roll[j + 1]) = (roll[j + 1], roll[j])
					swap += 2
		Sum = sum(marks)
		Sum -= swap
		if Sum / n >= avg:
			return 1
		else:
			return 0


class Solution:

	def shouldPunish(self, roll, marks, n, avg):
		x = 0
		for i in range(0, n):
			for j in range(0, n - i - 1):
				if roll[j] > roll[j + 1]:
					(roll[j], roll[j + 1]) = (roll[j + 1], roll[j])
					x = x + 1
		x = x * 2
		a = (sum(marks) - x) / n
		if a >= avg:
			return 1
		else:
			return 0


class Solution:

	def shouldPunish(self, roll, marks, n, avg):
		c = 0
		for i in range(n):
			for j in range(n - i - 1):
				if roll[j] > roll[j + 1]:
					(roll[j], roll[j + 1]) = (roll[j + 1], roll[j])
					c += 2
		s = sum(marks)
		k = (s - c) / n
		if k > avg:
			return 1
		return 0


class Solution:

	def shouldPunish(self, arr, marks, n, avg):
		count = 0
		for i in range(n):
			for j in range(n - i - 1):
				if arr[j] > arr[j + 1]:
					(arr[j], arr[j + 1]) = (arr[j + 1], arr[j])
					count += 2
		s = sum(marks)
		k = (s - count) / n
		if k > avg:
			return 1
		return 0


class Solution:

	def shouldPunish(self, roll, marks, n, avg):
		count = 0
		for i in range(n):
			for j in range(1, n - i):
				if roll[j] < roll[j - 1]:
					(roll[j], roll[j - 1]) = (roll[j - 1], roll[j])
					count += 1
		k = (sum(marks) - count) // n
		if k >= avg:
			return 1
		return 0


class Solution:

	def shouldPunish(self, roll, marks, n, avg):
		count = 0
		for i in range(n - 1):
			for j in range(n - i - 1):
				if roll[j] > roll[j + 1]:
					(roll[j], roll[j + 1]) = (roll[j + 1], roll[j])
					count += 2
		s = sum(marks)
		avg1 = (s - count) / len(roll)
		return 1 if avg <= avg1 else 0


class Solution:

	def shouldPunish(self, roll, marks, n, avg):
		swap = 0
		for i in range(n):
			for j in range(1, n - i):
				if roll[j] < roll[j - 1]:
					swap += 1
					(roll[j], roll[j - 1]) = (roll[j - 1], roll[j])
		total_marks = sum(marks)
		if (total_marks - swap) // n >= avg:
			return 1
		return 0


class Solution:

	def shouldPunish(self, roll, marks, n, avg):
		totalswap = 0
		j = 0
		issorted = False
		while issorted == False:
			swap = 0
			j += 1
			for i in range(0, n - j):
				if roll[i] > roll[i + 1]:
					temp = roll[i]
					roll[i] = roll[i + 1]
					roll[i + 1] = temp
					swap += 1
					totalswap += 1
			if swap == 0 or j == n - 2:
				issorted = True
		if (sum(marks) - totalswap * 2) / n >= avg:
			return 1
		else:
			return 0


class Solution:

	def shouldPunish(self, roll, marks, n, avg):
		swaps = 0
		for i in range(n):
			for j in range(i + 1, n):
				if roll[i] > roll[j]:
					(roll[i], roll[j]) = (roll[j], roll[i])
					swaps = swaps + 1
		summ = 0
		for i in marks:
			summ = summ + i
		average = (summ - 2 * swaps) / n
		if average >= avg:
			return 1
		else:
			return 0


class Solution:

	def shouldPunish(self, roll, marks, n, avg):
		count = 0
		for i in range(n):
			for j in range(n - i - 1):
				if roll[i] > roll[j + 1]:
					count += 1
		ans = (sum(marks) - count * 2) / n
		if ans <= avg:
			return 0
		else:
			return 1


class Solution:

	def shouldPunish(self, roll, marks, n, avg):
		num_of_swaps = 0
		no_swaps = False
		for j in range(n - 1):
			no_swaps = True
			for i in range(0, n - j - 1):
				if roll[i] > roll[i + 1]:
					num_of_swaps += 2
					(roll[i], roll[i + 1]) = (roll[i + 1], roll[i])
					no_swaps = False
			if no_swaps:
				break
		if (sum(marks) - num_of_swaps) / n > avg:
			return 1
		else:
			return 0


class Solution:

	def bubbleSort(self, arr):
		count = 0
		for i in range(n - 1):
			for j in range(n - 1 - i):
				if arr[j] > arr[j + 1]:
					(arr[j], arr[j + 1]) = (arr[j + 1], arr[j])
					count += 2
		return count

	def shouldPunish(self, roll, marks, n, avg):
		swaps = self.bubbleSort(roll)
		sum = 0
		for mark in marks:
			sum += mark
		deducted_marks = (sum - swaps) / n
		if deducted_marks >= avg:
			return 1
		else:
			return 0


class Solution:

	def shouldPunish(self, roll, marks, n, avg):
		time = 0
		for i in range(n):
			for j in range(n - i - 1):
				if marks[j] > marks[j + 1]:
					(marks[j], marks[j + 1]) = (marks[j + 1], marks[j])
					time = time + 1
		s = (sum(marks) - 2 * time) / n
		if s > avg:
			return 1
		else:
			return 0


class Solution:

	def shouldPunish(self, roll, marks, n, avg):
		count = 0
		for i in range(1, n):
			for j in range(n - i):
				if roll[j + 1] < roll[j]:
					count += 1
					temp = roll[j]
					roll[j] = roll[j + 1]
					roll[j + 1] = temp
		sum = 0
		for k in marks:
			sum += k
		new_avg = (sum - 2 * count) / n
		if new_avg >= avg:
			return 1
		else:
			return 0


class Solution:

	def shouldPunish(self, roll, marks, n, avg):
		counter = 0
		for i in range(n):
			for j in range(n - i - 1):
				if roll[j] > roll[j + 1]:
					(roll[j], roll[j + 1]) = (roll[j + 1], roll[j])
					counter += 1
		ans = (sum(marks) - counter) // n
		if ans >= avg:
			return 1
		return 0


class Solution:

	def shouldPunish(self, roll, marks, n, avg):
		S = 0
		for i in range(n):
			for j in range(n - i - 1):
				if roll[j] > roll[j + 1]:
					temp = roll[j]
					roll[j] = roll[j + 1]
					roll[j + 1] = temp
					S = S + 1
		new_avg = (sum(marks) - S) // n
		if new_avg >= avg:
			return 1
		else:
			return 0


class Solution:

	def shouldPunish(self, roll, marks, n, avg):
		no_swaps = 0
		for i in range(len(marks) - 1):
			for j in range(i + 1, len(marks)):
				if roll[i] > roll[j]:
					(roll[i], roll[j]) = (roll[j], roll[i])
					no_swaps += 1
		if avg > (sum(marks) - no_swaps) // n:
			return 0
		else:
			return 1


class Solution:

	def shouldPunish(self, roll, marks, n, avg):
		arr1 = roll
		arr2 = marks
		count1 = 0
		count2 = 0
		for k in range(0, len(arr2)):
			count2 = count2 + arr2[k]
		for i in range(n):
			for j in range(0, n - i - 1):
				if arr1[j] > arr1[j + 1]:
					(arr1[j], arr1[j + 1]) = (arr1[j + 1], arr1[j])
					count1 = count1 + 2
		a = count2 - count1
		if a / n > avg:
			return 1
		else:
			return 0


class Solution:

	def shouldPunish(self, roll, marks, n, avg):
		swap_count = 0
		for i in range(n):
			swapped = False
			for j in range(n - i - 1):
				if roll[j] > roll[j + 1]:
					(roll[j], roll[j + 1]) = (roll[j + 1], roll[j])
					swap_count += 1
					swapped = True
			if swapped == False:
				break
		if (sum(marks) - swap_count) // n < avg:
			return 0
		return 1


class Solution:

	def shouldPunish(self, roll, marks, n, avg):
		swap = 0
		for i in range(len(roll)):
			for j in range(len(roll) - i - 1):
				if roll[j] > roll[j + 1]:
					swap += 2
					(roll[j + 1], roll[j]) = (roll[j], roll[j + 1])
			if swap == 0:
				break
		s = 0
		for i in marks:
			s = s + i
		if (s - swap) / n >= avg:
			return 1
		else:
			return 0


class Solution:

	def shouldPunish(self, roll, marks, n, avg):
		count = 0
		for i in range(n):
			swap = False
			for j in range(n - i - 1):
				if roll[j] > roll[j + 1]:
					(roll[j], roll[j + 1]) = (roll[j + 1], roll[j])
					count += 1
					swap = True
		if count == 0:
			return 0
		cavg = (sum(marks) - count * 2) / n
		if avg > cavg:
			return 0
		return 1


class Solution:

	def shouldPunish(self, roll, marks, n, avg):
		c = 0
		for i in range(n - 1):
			for j in range(n - 1 - i):
				if roll[j] > roll[j + 1]:
					(roll[j + 1], roll[j]) = (roll[j], roll[j + 1])
					c += 2
		if (sum(marks) - c) / n > avg:
			return 1
		else:
			return 0
t = int(input())
for tc in range(t):
	(n, avg) = input().split()
	n = int(n)
	avg = float(avg)
	roll = list(map(int, input().split()))
	marks = list(map(int, input().split()))
	ob = Solution()
	print(ob.shouldPunish(roll, marks, n, avg))


class Solution:

	def shouldPunish(self, roll, marks, n, avg):
		swap = 0
		sum = 0
		for i in range(n):
			for j in range(0, n - i - 1):
				if roll[j] > roll[j + 1]:
					temp = roll[j]
					roll[j] = roll[j + 1]
					roll[j + 1] = temp
					swap += 1
		for i in marks:
			sum = sum + i
			sw = swap * 2
		av = (sum - sw) / n
		if av > avg:
			return 1
		return 0
t = int(input())
for tc in range(t):
	(n, avg) = input().split()
	n = int(n)
	avg = float(avg)
	roll = list(map(int, input().split()))
	marks = list(map(int, input().split()))
	ob = Solution()
	print(ob.shouldPunish(roll, marks, n, avg))


class Solution:

	def shouldPunish(self, roll, marks, n, avg):
		swap_count = 0
		for iter_num in range(n):
			for curr in range(n - 1 - iter_num):
				if roll[curr] > roll[curr + 1]:
					(roll[curr], roll[curr + 1]) = (roll[curr + 1], roll[curr])
					swap_count = swap_count + 1
		if (sum(marks) - swap_count) // n < avg:
			return 0
		else:
			return 1
t = int(input())
for tc in range(t):
	(n, avg) = input().split()
	n = int(n)
	avg = float(avg)
	roll = list(map(int, input().split()))
	marks = list(map(int, input().split()))
	ob = Solution()
	print(ob.shouldPunish(roll, marks, n, avg))


class Solution:

	def shouldPunish(self, roll, marks, n, avg):
		swaps = 0
		swapped = False
		for i in range(n - 1):
			for j in range(0, n - i - 1):
				if roll[j] > roll[j + 1]:
					swaps += 2
					swapped = True
					(roll[j], roll[j + 1]) = (roll[j + 1], roll[j])
			if not swapped:
				break
		if (sum(marks) - swaps) / len(marks) >= avg:
			return 1
		else:
			return 0
t = int(input())
for tc in range(t):
	(n, avg) = input().split()
	n = int(n)
	avg = float(avg)
	roll = list(map(int, input().split()))
	marks = list(map(int, input().split()))
	ob = Solution()
	print(ob.shouldPunish(roll, marks, n, avg))


class Solution:

	def shouldPunish(self, roll, marks, n, avg):
		average = sum(marks) / len(marks)
		m = 0
		if average > avg:
			for i in range(len(roll)):
				count = 0
				for j in range(len(roll) - i - 1):
					if roll[j] > roll[j + 1]:
						(roll[j], roll[j + 1]) = (roll[j + 1], roll[j])
						count += 1
						m += 2
				if count == 0:
					break
			if (sum(marks) - m) / len(marks) > avg:
				return 1
			else:
				return 0
		else:
			return 0
t = int(input())
for tc in range(t):
	(n, avg) = input().split()
	n = int(n)
	avg = float(avg)
	roll = list(map(int, input().split()))
	marks = list(map(int, input().split()))
	ob = Solution()
	print(ob.shouldPunish(roll, marks, n, avg))


class Solution:

	def shouldPunish(self, arr, marks, n, x):
		count = 0
		for i in range(n):
			for j in range(n - i - 1):
				if arr[j] > arr[j + 1]:
					(arr[j], arr[j + 1]) = (arr[j + 1], arr[j])
					count += 1
		ans = 0
		for i in range(n):
			ans += marks[i]
		avr2 = (ans - count) / n
		if int(avr2) > int(x):
			return 1
		else:
			return 0
t = int(input())
for tc in range(t):
	(n, avg) = input().split()
	n = int(n)
	avg = float(avg)
	roll = list(map(int, input().split()))
	marks = list(map(int, input().split()))
	ob = Solution()
	print(ob.shouldPunish(roll, marks, n, avg))


class Solution:

	def shouldPunish(self, roll, marks, n, avg):
		count = 0
		class_avg = 0
		sum = 0
		for i in range(0, n - 1):
			for j in range(0, n - i - 1):
				if roll[j] > roll[j + 1]:
					(roll[j], roll[j + 1]) = (roll[j + 1], roll[j])
					count += 1
				else:
					continue
		for i in range(len(marks)):
			sum += marks[i]
		class_avg = (sum - count) // n
		if class_avg > avg:
			return 1
		else:
			return 0
t = int(input())
for tc in range(t):
	(n, avg) = input().split()
	n = int(n)
	avg = float(avg)
	roll = list(map(int, input().split()))
	marks = list(map(int, input().split()))
	ob = Solution()
	print(ob.shouldPunish(roll, marks, n, avg))


class Solution:

	def shouldPunish(self, roll, marks, n, avg):
		swaps = 0
		for i in range(len(roll) - 1):
			for j in range(i + 1, len(roll)):
				if roll[i] > roll[j]:
					swaps += 1
		sum1 = 0
		swaps = swaps * 2
		for i in marks:
			sum1 += i
		aver = (sum1 - swaps) / len(roll)
		if avg > aver:
			return 0
		else:
			return 1
t = int(input())
for tc in range(t):
	(n, avg) = input().split()
	n = int(n)
	avg = float(avg)
	roll = list(map(int, input().split()))
	marks = list(map(int, input().split()))
	ob = Solution()
	print(ob.shouldPunish(roll, marks, n, avg))


class Solution:

	def shouldPunish(self, roll, marks, n, avg):
		swap = 0
		for p in range(n - 1, -1, -1):
			for i in range(p):
				if roll[i] > roll[i + 1]:
					swap += 2
					temp = roll[i]
					roll[i] = roll[i + 1]
					roll[i + 1] = temp
		m = (sum(marks) - swap) / n
		if m > avg:
			return 1
		else:
			return 0
t = int(input())
for tc in range(t):
	(n, avg) = input().split()
	n = int(n)
	avg = float(avg)
	roll = list(map(int, input().split()))
	marks = list(map(int, input().split()))
	ob = Solution()
	print(ob.shouldPunish(roll, marks, n, avg))


class Solution:

	def shouldPunish(self, roll, marks, n, avg):
		count = 0
		for i in range(n):
			swapped = False
			for j in range(0, n - i - 1):
				if roll[j] > roll[j + 1]:
					(roll[j], roll[j + 1]) = (roll[j + 1], roll[j])
					swapped = True
					count += 2
			if swapped == False:
				break
		total_marks = 0
		for i in range(n):
			total_marks += marks[i]
		result = (total_marks - count) / n
		if result > avg:
			return 1
		else:
			return 0
t = int(input())
for tc in range(t):
	(n, avg) = input().split()
	n = int(n)
	avg = float(avg)
	roll = list(map(int, input().split()))
	marks = list(map(int, input().split()))
	ob = Solution()
	print(ob.shouldPunish(roll, marks, n, avg))