From c08e1388e664e9f392d0a03835879c0efd49a403 Mon Sep 17 00:00:00 2001 From: siddwarr Date: Mon, 2 Oct 2023 21:28:17 +0530 Subject: [PATCH 01/18] added power_of_4 --- bit_manipulation/power_of_4 | 61 +++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 bit_manipulation/power_of_4 diff --git a/bit_manipulation/power_of_4 b/bit_manipulation/power_of_4 new file mode 100644 index 000000000000..071a53a5dc4e --- /dev/null +++ b/bit_manipulation/power_of_4 @@ -0,0 +1,61 @@ +""" + +Task: +Given a positive int number. Return True if this number is power of 4 +or False otherwise. + +Implementation notes: Use bit manipulation. +For example if the number is the power of 2 it's bits representation: +n = 0..100..00 +n - 1 = 0..011..11 + +n & (n - 1) - no intersections = 0 +If the number is a power of 4 then it should be a power of 2 and the set bit should be at an odd position +""" + + +def power_of_4(number: int) -> bool: + """ + Return True if this number is power of 4 or False otherwise. + + >>> power_of_4(0) + False + >>> power_of_4(1) + True + >>> power_of_4(2) + False + >>> power_of_4(4) + True + >>> power_of_4(6) + False + >>> power_of_4(8) + False + >>> power_of_4(17) + False + >>> power_of_4(-1) + Traceback (most recent call last): + ... + ValueError: number must be positive + >>> power_of_4(1.2) + Traceback (most recent call last): + ... + TypeError: unsupported operand type(s) for &: 'float' and 'float' + + """ + if number <= 0: + raise ValueError("number must be positive") + if number & (number - 1) == 0: + c = 0 + while number: + c+=1 + number>>=1 + if c&1==0: + return True + else: + return False + + +if __name__ == "__main__": + import doctest + + doctest.testmod() From 60e1a83bdb9bceb16f24aab4e9a76ef5e329f4cd Mon Sep 17 00:00:00 2001 From: siddwarr Date: Mon, 2 Oct 2023 21:45:03 +0530 Subject: [PATCH 02/18] deleted power_of_4 --- bit_manipulation/power_of_4 | 61 ------------------------------------- 1 file changed, 61 deletions(-) delete mode 100644 bit_manipulation/power_of_4 diff --git a/bit_manipulation/power_of_4 b/bit_manipulation/power_of_4 deleted file mode 100644 index 071a53a5dc4e..000000000000 --- a/bit_manipulation/power_of_4 +++ /dev/null @@ -1,61 +0,0 @@ -""" - -Task: -Given a positive int number. Return True if this number is power of 4 -or False otherwise. - -Implementation notes: Use bit manipulation. -For example if the number is the power of 2 it's bits representation: -n = 0..100..00 -n - 1 = 0..011..11 - -n & (n - 1) - no intersections = 0 -If the number is a power of 4 then it should be a power of 2 and the set bit should be at an odd position -""" - - -def power_of_4(number: int) -> bool: - """ - Return True if this number is power of 4 or False otherwise. - - >>> power_of_4(0) - False - >>> power_of_4(1) - True - >>> power_of_4(2) - False - >>> power_of_4(4) - True - >>> power_of_4(6) - False - >>> power_of_4(8) - False - >>> power_of_4(17) - False - >>> power_of_4(-1) - Traceback (most recent call last): - ... - ValueError: number must be positive - >>> power_of_4(1.2) - Traceback (most recent call last): - ... - TypeError: unsupported operand type(s) for &: 'float' and 'float' - - """ - if number <= 0: - raise ValueError("number must be positive") - if number & (number - 1) == 0: - c = 0 - while number: - c+=1 - number>>=1 - if c&1==0: - return True - else: - return False - - -if __name__ == "__main__": - import doctest - - doctest.testmod() From 2290ff9361069b90e802594715fb290af7addf0d Mon Sep 17 00:00:00 2001 From: siddwarr Date: Tue, 3 Oct 2023 18:29:32 +0530 Subject: [PATCH 03/18] added pairs_with_given_sum --- .../arrays/pairs_with_given_sum.py | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 data_structures/arrays/pairs_with_given_sum.py diff --git a/data_structures/arrays/pairs_with_given_sum.py b/data_structures/arrays/pairs_with_given_sum.py new file mode 100644 index 000000000000..677a99be1f59 --- /dev/null +++ b/data_structures/arrays/pairs_with_given_sum.py @@ -0,0 +1,48 @@ +""" +Author : Siddharth Warrier +Date : October 3, 2023 + +Task: +Count the no of pairs in a given array with given sum + +Implementation notes: Using hashing +The idea is that we hash the array in a dictionary +Then go throught the elemnts of the array +We subtract this with the given sum +and check if that is there in the array +We also check the edge cases like if there are multiple same elements +Finally we divide the count by 2 +to avoid the same pair getting counted twice +""" + + +def pairs_with_sum(arr, k): + """ + Return the no. of pairs with sum k + + >>> pairs_with_sum([1,5,7,1],6) + 2 + >>> pairs_with_sum([1,1,1,1,1,1,1,1],2) + 28 + >>> pairs_with_sum([1,7,6,2,5,4,3,1,9,8],7) + 4 + """ + d = {} + for i in arr: + if i in d: + d[i]+=1 + else: + d[i] = 1 + ans = 0 + for i in arr: + d[i]-=1 + if k-i in d and d[k-i]!=0: + ans+=d[k-i] + d[i]+=1 + return ans//2 + + +if __name__ == "__main__": + import doctest + + doctest.testmod() From 1f94b8371fd558ca259e39f40fec7ff1c23d725b Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 3 Oct 2023 13:02:45 +0000 Subject: [PATCH 04/18] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- data_structures/arrays/pairs_with_given_sum.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/data_structures/arrays/pairs_with_given_sum.py b/data_structures/arrays/pairs_with_given_sum.py index 677a99be1f59..3f587ed8f99b 100644 --- a/data_structures/arrays/pairs_with_given_sum.py +++ b/data_structures/arrays/pairs_with_given_sum.py @@ -30,16 +30,16 @@ def pairs_with_sum(arr, k): d = {} for i in arr: if i in d: - d[i]+=1 + d[i] += 1 else: d[i] = 1 ans = 0 for i in arr: - d[i]-=1 - if k-i in d and d[k-i]!=0: - ans+=d[k-i] - d[i]+=1 - return ans//2 + d[i] -= 1 + if k - i in d and d[k - i] != 0: + ans += d[k - i] + d[i] += 1 + return ans // 2 if __name__ == "__main__": From 4251cf593d4da2dab3f04bd5639e564eef897642 Mon Sep 17 00:00:00 2001 From: siddwarr Date: Tue, 3 Oct 2023 18:34:13 +0530 Subject: [PATCH 05/18] updated the comment --- data_structures/arrays/pairs_with_given_sum.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/data_structures/arrays/pairs_with_given_sum.py b/data_structures/arrays/pairs_with_given_sum.py index 677a99be1f59..2707d06119d4 100644 --- a/data_structures/arrays/pairs_with_given_sum.py +++ b/data_structures/arrays/pairs_with_given_sum.py @@ -7,7 +7,7 @@ Implementation notes: Using hashing The idea is that we hash the array in a dictionary -Then go throught the elemnts of the array +Then go through the elements of the array We subtract this with the given sum and check if that is there in the array We also check the edge cases like if there are multiple same elements From a3ee9ebf0b534440172c6803979514feb123a192 Mon Sep 17 00:00:00 2001 From: siddwarr Date: Tue, 3 Oct 2023 20:52:30 +0530 Subject: [PATCH 06/18] updated return hint --- data_structures/arrays/pairs_with_given_sum.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/data_structures/arrays/pairs_with_given_sum.py b/data_structures/arrays/pairs_with_given_sum.py index 928c80523671..a5a70935c3a7 100644 --- a/data_structures/arrays/pairs_with_given_sum.py +++ b/data_structures/arrays/pairs_with_given_sum.py @@ -16,7 +16,7 @@ """ -def pairs_with_sum(arr, k): +def pairs_with_sum(arr, k) -> int: """ Return the no. of pairs with sum k From f6f790fdbd0b8579195dd67385fdebdd842c18c5 Mon Sep 17 00:00:00 2001 From: siddwarr Date: Tue, 3 Oct 2023 20:55:50 +0530 Subject: [PATCH 07/18] updated type hints --- data_structures/arrays/pairs_with_given_sum.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/data_structures/arrays/pairs_with_given_sum.py b/data_structures/arrays/pairs_with_given_sum.py index a5a70935c3a7..3b8e0bd1a858 100644 --- a/data_structures/arrays/pairs_with_given_sum.py +++ b/data_structures/arrays/pairs_with_given_sum.py @@ -16,9 +16,9 @@ """ -def pairs_with_sum(arr, k) -> int: +def pairs_with_sum(arr: list, sum: int) -> int: """ - Return the no. of pairs with sum k + Return the no. of pairs with sum "sum" >>> pairs_with_sum([1,5,7,1],6) 2 @@ -36,8 +36,8 @@ def pairs_with_sum(arr, k) -> int: ans = 0 for i in arr: d[i] -= 1 - if k - i in d and d[k - i] != 0: - ans += d[k - i] + if sum - i in d and d[sum - i] != 0: + ans += d[sum - i] d[i] += 1 return ans // 2 From 6a0daf4f6b2d60dbc8a630340c2883124a3ae725 Mon Sep 17 00:00:00 2001 From: siddwarr Date: Tue, 3 Oct 2023 20:57:55 +0530 Subject: [PATCH 08/18] updated the variable --- data_structures/arrays/pairs_with_given_sum.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/data_structures/arrays/pairs_with_given_sum.py b/data_structures/arrays/pairs_with_given_sum.py index 3b8e0bd1a858..4ec18ed29244 100644 --- a/data_structures/arrays/pairs_with_given_sum.py +++ b/data_structures/arrays/pairs_with_given_sum.py @@ -16,7 +16,7 @@ """ -def pairs_with_sum(arr: list, sum: int) -> int: +def pairs_with_sum(arr: list, req_sum: int) -> int: """ Return the no. of pairs with sum "sum" @@ -36,8 +36,8 @@ def pairs_with_sum(arr: list, sum: int) -> int: ans = 0 for i in arr: d[i] -= 1 - if sum - i in d and d[sum - i] != 0: - ans += d[sum - i] + if req_sum - i in d and d[req_sum - i] != 0: + ans += d[req_sum - i] d[i] += 1 return ans // 2 From 77b6a7c482da5fa77981477fd7a3118484e0b93d Mon Sep 17 00:00:00 2001 From: siddwarr Date: Tue, 3 Oct 2023 21:03:22 +0530 Subject: [PATCH 09/18] updated annotation --- data_structures/arrays/pairs_with_given_sum.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/data_structures/arrays/pairs_with_given_sum.py b/data_structures/arrays/pairs_with_given_sum.py index 4ec18ed29244..d11e08f8672b 100644 --- a/data_structures/arrays/pairs_with_given_sum.py +++ b/data_structures/arrays/pairs_with_given_sum.py @@ -27,7 +27,7 @@ def pairs_with_sum(arr: list, req_sum: int) -> int: >>> pairs_with_sum([1,7,6,2,5,4,3,1,9,8],7) 4 """ - d = {} + d = dict() for i in arr: if i in d: d[i] += 1 From 8bf754d37359591aaf37e34e21a522836e8cba10 Mon Sep 17 00:00:00 2001 From: siddwarr Date: Tue, 3 Oct 2023 21:05:57 +0530 Subject: [PATCH 10/18] updated code --- data_structures/arrays/pairs_with_given_sum.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/data_structures/arrays/pairs_with_given_sum.py b/data_structures/arrays/pairs_with_given_sum.py index d11e08f8672b..4ec18ed29244 100644 --- a/data_structures/arrays/pairs_with_given_sum.py +++ b/data_structures/arrays/pairs_with_given_sum.py @@ -27,7 +27,7 @@ def pairs_with_sum(arr: list, req_sum: int) -> int: >>> pairs_with_sum([1,7,6,2,5,4,3,1,9,8],7) 4 """ - d = dict() + d = {} for i in arr: if i in d: d[i] += 1 From 42d2c138baff146c993f8ce809b269c4a35a606a Mon Sep 17 00:00:00 2001 From: siddwarr Date: Tue, 3 Oct 2023 21:15:20 +0530 Subject: [PATCH 11/18] updated code --- data_structures/arrays/pairs_with_given_sum.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/data_structures/arrays/pairs_with_given_sum.py b/data_structures/arrays/pairs_with_given_sum.py index 4ec18ed29244..18d0f2438bbc 100644 --- a/data_structures/arrays/pairs_with_given_sum.py +++ b/data_structures/arrays/pairs_with_given_sum.py @@ -27,7 +27,7 @@ def pairs_with_sum(arr: list, req_sum: int) -> int: >>> pairs_with_sum([1,7,6,2,5,4,3,1,9,8],7) 4 """ - d = {} + d:dict = {} for i in arr: if i in d: d[i] += 1 From f39e16846bfaf55d28eaa6df75f842c289e4899d Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 3 Oct 2023 15:49:37 +0000 Subject: [PATCH 12/18] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- data_structures/arrays/pairs_with_given_sum.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/data_structures/arrays/pairs_with_given_sum.py b/data_structures/arrays/pairs_with_given_sum.py index 18d0f2438bbc..d718650b6140 100644 --- a/data_structures/arrays/pairs_with_given_sum.py +++ b/data_structures/arrays/pairs_with_given_sum.py @@ -27,7 +27,7 @@ def pairs_with_sum(arr: list, req_sum: int) -> int: >>> pairs_with_sum([1,7,6,2,5,4,3,1,9,8],7) 4 """ - d:dict = {} + d: dict = {} for i in arr: if i in d: d[i] += 1 From fdfe4f4e038672d3d843cc6f6225bd2b0e02758c Mon Sep 17 00:00:00 2001 From: Siddharth Warrier <117698635+siddwarr@users.noreply.github.com> Date: Thu, 12 Oct 2023 13:56:21 +0530 Subject: [PATCH 13/18] added the problem link and used defaultdict --- data_structures/arrays/pairs_with_given_sum.py | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/data_structures/arrays/pairs_with_given_sum.py b/data_structures/arrays/pairs_with_given_sum.py index d718650b6140..d453731f7f89 100644 --- a/data_structures/arrays/pairs_with_given_sum.py +++ b/data_structures/arrays/pairs_with_given_sum.py @@ -4,6 +4,7 @@ Task: Count the no of pairs in a given array with given sum +Problem URL- https://practice.geeksforgeeks.org/problems/count-pairs-with-given-sum5022/0 Implementation notes: Using hashing The idea is that we hash the array in a dictionary @@ -15,7 +16,7 @@ to avoid the same pair getting counted twice """ - +from collections import defaultdict def pairs_with_sum(arr: list, req_sum: int) -> int: """ Return the no. of pairs with sum "sum" @@ -27,18 +28,15 @@ def pairs_with_sum(arr: list, req_sum: int) -> int: >>> pairs_with_sum([1,7,6,2,5,4,3,1,9,8],7) 4 """ - d: dict = {} + d = defaultdict(int) for i in arr: - if i in d: - d[i] += 1 - else: - d[i] = 1 + d[i] += 1 ans = 0 for i in arr: - d[i] -= 1 + d[i]-=1 if req_sum - i in d and d[req_sum - i] != 0: - ans += d[req_sum - i] - d[i] += 1 + ans += d[req_sum - i]-1 + d[i]+=1 return ans // 2 From 0752ae113708a8707aaee989f260f6558e1766a6 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 12 Oct 2023 08:27:54 +0000 Subject: [PATCH 14/18] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- data_structures/arrays/pairs_with_given_sum.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/data_structures/arrays/pairs_with_given_sum.py b/data_structures/arrays/pairs_with_given_sum.py index d453731f7f89..6d1503ea36cb 100644 --- a/data_structures/arrays/pairs_with_given_sum.py +++ b/data_structures/arrays/pairs_with_given_sum.py @@ -17,6 +17,8 @@ """ from collections import defaultdict + + def pairs_with_sum(arr: list, req_sum: int) -> int: """ Return the no. of pairs with sum "sum" @@ -33,10 +35,10 @@ def pairs_with_sum(arr: list, req_sum: int) -> int: d[i] += 1 ans = 0 for i in arr: - d[i]-=1 + d[i] -= 1 if req_sum - i in d and d[req_sum - i] != 0: - ans += d[req_sum - i]-1 - d[i]+=1 + ans += d[req_sum - i] - 1 + d[i] += 1 return ans // 2 From d40eaa1e1be8fc2d5a4ffbffa7cd487e2de789c4 Mon Sep 17 00:00:00 2001 From: Siddharth Warrier <117698635+siddwarr@users.noreply.github.com> Date: Thu, 12 Oct 2023 14:04:00 +0530 Subject: [PATCH 15/18] corrected import formatting --- data_structures/arrays/pairs_with_given_sum.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/data_structures/arrays/pairs_with_given_sum.py b/data_structures/arrays/pairs_with_given_sum.py index 6d1503ea36cb..837960dbfd4e 100644 --- a/data_structures/arrays/pairs_with_given_sum.py +++ b/data_structures/arrays/pairs_with_given_sum.py @@ -16,7 +16,7 @@ to avoid the same pair getting counted twice """ -from collections import defaultdict + def pairs_with_sum(arr: list, req_sum: int) -> int: @@ -44,5 +44,5 @@ def pairs_with_sum(arr: list, req_sum: int) -> int: if __name__ == "__main__": import doctest - + from collections import defaultdict doctest.testmod() From ce68b14c66ac06e65f312f733becdf1c8188d364 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 12 Oct 2023 08:34:35 +0000 Subject: [PATCH 16/18] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- data_structures/arrays/pairs_with_given_sum.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/data_structures/arrays/pairs_with_given_sum.py b/data_structures/arrays/pairs_with_given_sum.py index 837960dbfd4e..d7aae96a455a 100644 --- a/data_structures/arrays/pairs_with_given_sum.py +++ b/data_structures/arrays/pairs_with_given_sum.py @@ -17,8 +17,6 @@ """ - - def pairs_with_sum(arr: list, req_sum: int) -> int: """ Return the no. of pairs with sum "sum" @@ -45,4 +43,5 @@ def pairs_with_sum(arr: list, req_sum: int) -> int: if __name__ == "__main__": import doctest from collections import defaultdict + doctest.testmod() From 27dc6d2051dd5684baada729edafaee4c42cab94 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Thu, 12 Oct 2023 11:00:40 +0200 Subject: [PATCH 17/18] Update pairs_with_given_sum.py --- .../arrays/pairs_with_given_sum.py | 43 ++++++------------- 1 file changed, 12 insertions(+), 31 deletions(-) diff --git a/data_structures/arrays/pairs_with_given_sum.py b/data_structures/arrays/pairs_with_given_sum.py index d7aae96a455a..49922f204df7 100644 --- a/data_structures/arrays/pairs_with_given_sum.py +++ b/data_structures/arrays/pairs_with_given_sum.py @@ -1,47 +1,28 @@ -""" -Author : Siddharth Warrier -Date : October 3, 2023 +#!/usr/bin/env python3 -Task: -Count the no of pairs in a given array with given sum -Problem URL- https://practice.geeksforgeeks.org/problems/count-pairs-with-given-sum5022/0 +""" +Given an array of integers and an integer req_sum, find the number of pairs of array +elements whose sum is equal to req_sum. -Implementation notes: Using hashing -The idea is that we hash the array in a dictionary -Then go through the elements of the array -We subtract this with the given sum -and check if that is there in the array -We also check the edge cases like if there are multiple same elements -Finally we divide the count by 2 -to avoid the same pair getting counted twice +https://practice.geeksforgeeks.org/problems/count-pairs-with-given-sum5022/0 """ +from itertools import combinations def pairs_with_sum(arr: list, req_sum: int) -> int: """ Return the no. of pairs with sum "sum" - - >>> pairs_with_sum([1,5,7,1],6) + >>> pairs_with_sum([1, 5, 7, 1], 6) 2 - >>> pairs_with_sum([1,1,1,1,1,1,1,1],2) + >>> pairs_with_sum([1, 1, 1, 1, 1, 1, 1, 1], 2) 28 - >>> pairs_with_sum([1,7,6,2,5,4,3,1,9,8],7) + >>> pairs_with_sum([1, 7, 6, 2, 5, 4, 3, 1, 9, 8], 7) 4 """ - d = defaultdict(int) - for i in arr: - d[i] += 1 - ans = 0 - for i in arr: - d[i] -= 1 - if req_sum - i in d and d[req_sum - i] != 0: - ans += d[req_sum - i] - 1 - d[i] += 1 - return ans // 2 + return sum(int(bool(a + b == req_sum)) for a, b in combinations(arr, 2)) if __name__ == "__main__": - import doctest - from collections import defaultdict + from doctest import testmod - doctest.testmod() + testmod() From e4befa321221c6b4623c92d811edee0f1e99513c Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Thu, 12 Oct 2023 11:04:00 +0200 Subject: [PATCH 18/18] Update data_structures/arrays/pairs_with_given_sum.py --- data_structures/arrays/pairs_with_given_sum.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/data_structures/arrays/pairs_with_given_sum.py b/data_structures/arrays/pairs_with_given_sum.py index 49922f204df7..c4a5ceeae456 100644 --- a/data_structures/arrays/pairs_with_given_sum.py +++ b/data_structures/arrays/pairs_with_given_sum.py @@ -19,7 +19,7 @@ def pairs_with_sum(arr: list, req_sum: int) -> int: >>> pairs_with_sum([1, 7, 6, 2, 5, 4, 3, 1, 9, 8], 7) 4 """ - return sum(int(bool(a + b == req_sum)) for a, b in combinations(arr, 2)) + return len([1 for a, b in combinations(arr, 2) if a + b == req_sum]) if __name__ == "__main__":