Skip to content

Instantly share code, notes, and snippets.

@AashishNandakumar
Last active August 25, 2024 08:31
Show Gist options
  • Select an option

  • Save AashishNandakumar/c3232ba2bf40c287466d9eb9700bf9ff to your computer and use it in GitHub Desktop.

Select an option

Save AashishNandakumar/c3232ba2bf40c287466d9eb9700bf9ff to your computer and use it in GitHub Desktop.
# Holloumi Boxes
# logic
def can_sort_it(a, n, k) -> bool:
# def reverse(a, ptr1, ptr2):
# a[ptr1:ptr2] = reversed(a[ptr1:ptr2])
# if n == 1:
# return True
# for i in range(n - 1):
# for j in range(i + 1, n):
# if j - i + 1 <= k:
# if a[j] < a[i]:
# reverse(a, i, j + 1)
# else:
# continue
# else:
# break
# # check if the list is sorted
# for i in range(0, n - 1):
# if a[i] > a[i + 1]:
# return False
# else:
# return True
# if k > 1 then there is always a way to sort
if k > 1:
return True
# if k is <= 1
for i in range(n - 1):
# check if it is not already sorted then can't sort
if a[i] > a[i + 1]:
return False
# it is already sorted
return True
# no of test cases
t = int(input())
for _ in range(t):
n, k = map(int, input().split())
a = list(map(int, input().split()))
print("YES" if can_sort_it(a, n, k) else "NO")
# t = 1
# n = 2
# k = 1
# a = [3, 1]
# print("YES" if can_sort_it(a, n, k) else "NO")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment