diff --git a/CompetitiveProgramming/CodeChef/P41_REPLESX.py b/CompetitiveProgramming/CodeChef/P41_REPLESX.py new file mode 100644 index 0000000..4ea2bf0 --- /dev/null +++ b/CompetitiveProgramming/CodeChef/P41_REPLESX.py @@ -0,0 +1,82 @@ +def binarySearchCountLess(arr, n, key): + + left = 0 + right = n + + mid = 0 + while (left < right): + + mid = (right + left)//2 + if (arr[mid] == key): + + while (mid + 1 key): + right = mid + else: + left = mid + 1 + + while (mid > -1 and arr[mid] > key): + mid-= 1 + + return mid + 1 + +def main(): + t = int(input()) + for _ in range(t): + n, x, p, k = [int(y) for y in input().split()] + a = [int(y) for y in input().split()] + a.sort() + if n <= 5: + count = 0 + if a[p-1] == x: + print(0) + else: + for i in range(n+1): + a[k-1] = x + a.sort() + count += 1 + if a[p-1] == x: + print(count) + break + if a[p-1] != x: + print(-1) + else: + if a[p - 1] == x: + res = 0 + else: + if p < k: + if a[p-1] < x: + res = -1 + else: + ind = binarySearchCountLess(a, n, x) + res = p - ind + elif p > k: + if a[p - 1] > x: + res = -1 + else: + ind = binarySearchCountLess(a, n, x) + if a[ind - 1] != x: + ind += 1 + else: + while True: + if a[ind - 2] == x: + ind -= 1 + else: + break + res = ind - p + else: + if x < a[p-1]: + ind = binarySearchCountLess(a, n, x) + res = p - ind + else: + ind = binarySearchCountLess(a, n, x) + if a[ind - 1] != x: + ind += 1 + res = ind - p + print(res) + return 0 + +main() diff --git a/CompetitiveProgramming/CodeChef/P42_POSAND.py b/CompetitiveProgramming/CodeChef/P42_POSAND.py new file mode 100644 index 0000000..ba7d318 --- /dev/null +++ b/CompetitiveProgramming/CodeChef/P42_POSAND.py @@ -0,0 +1,36 @@ +def main(): + t = int(input()) + res = [2,3,1] + last = 3 + check = 4 + for _ in range(t): + n = int(input()) + if n == 1: + print(1) + elif (n & (n-1)) == 0: + print(-1) + elif n <= last: + for i in range(n): + print(res[i], end=" ") + print() + else: + for i in range(last): + print(res[i], end=" ") + last += 1 + while last <= n: + if last == check: + res.append(last + 1) + res.append(last) + print((last + 1), end=" ") + print(last, end=" ") + last += 2 + check *= 2 + else: + print(last, end=" ") + res.append(last) + last += 1 + print() + last -= 1 + return 0 + +main()