From d37112657756b0f91a1453658a30b4584cab11a6 Mon Sep 17 00:00:00 2001 From: rahulp99 <44538358+rahulp99@users.noreply.github.com> Date: Thu, 15 Oct 2020 10:15:50 +0530 Subject: [PATCH 1/3] Problem: Replace for X --- .../CodeChef/p41_REPLESX.py | 82 +++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 CompetitiveProgramming/CodeChef/p41_REPLESX.py 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() From fa94eadd891f59b29154f8cad39a7ac05e46ffa5 Mon Sep 17 00:00:00 2001 From: rahulp99 <44538358+rahulp99@users.noreply.github.com> Date: Thu, 15 Oct 2020 10:18:04 +0530 Subject: [PATCH 2/3] Codechef: Positive AND --- CompetitiveProgramming/CodeChef/P42_POSAND.py | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 CompetitiveProgramming/CodeChef/P42_POSAND.py 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() From d420517ce2b7bbe06ac3cc05a2ec47adf301d323 Mon Sep 17 00:00:00 2001 From: rahulp99 <44538358+rahulp99@users.noreply.github.com> Date: Thu, 15 Oct 2020 10:19:02 +0530 Subject: [PATCH 3/3] Codechef: Replace for X --- .../CodeChef/{p41_REPLESX.py => P41_REPLESX.py} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename CompetitiveProgramming/CodeChef/{p41_REPLESX.py => P41_REPLESX.py} (100%) diff --git a/CompetitiveProgramming/CodeChef/p41_REPLESX.py b/CompetitiveProgramming/CodeChef/P41_REPLESX.py similarity index 100% rename from CompetitiveProgramming/CodeChef/p41_REPLESX.py rename to CompetitiveProgramming/CodeChef/P41_REPLESX.py