From eb0628b0e7b36be24d9a7d1947aea2d3feb93e82 Mon Sep 17 00:00:00 2001 From: Sankalp Sharma Date: Mon, 17 May 2021 10:43:53 +0530 Subject: [PATCH 1/4] missing number problem --- algorithms/Python/arrays/missing_number.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 algorithms/Python/arrays/missing_number.py diff --git a/algorithms/Python/arrays/missing_number.py b/algorithms/Python/arrays/missing_number.py new file mode 100644 index 00000000..110ee57a --- /dev/null +++ b/algorithms/Python/arrays/missing_number.py @@ -0,0 +1,15 @@ +""" +Algorithm Type: Array Traversal +Time Complexity: O(n) +""" +numbers = [1, 2, 4, 3, 6, 7, 9, 8, 10] +n = 10 + +def missing_number(numbers, n): + s = sum(numbers) + expected_s = (n*(n+1))//2 #for C++ or similar, keep in mind expected_s can overflow MAX INT limit. + return expected_s - s + + +if __name__ == "__main__": + print(missing_number(numbers, n)) From f1ace9bb8bdfb55da2dd35f5b5c630796253d891 Mon Sep 17 00:00:00 2001 From: Sankalp Sharma Date: Mon, 17 May 2021 10:58:36 +0530 Subject: [PATCH 2/4] added majority element --- algorithms/Python/arrays/majority_element.py | 26 ++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 algorithms/Python/arrays/majority_element.py diff --git a/algorithms/Python/arrays/majority_element.py b/algorithms/Python/arrays/majority_element.py new file mode 100644 index 00000000..f371169f --- /dev/null +++ b/algorithms/Python/arrays/majority_element.py @@ -0,0 +1,26 @@ +""" +Algorithm Type: Array Traversal +Time Complexity: O(n) +""" +numbers = [2, 2, 5, 6, 2, 6, 2, 10, 2] + +def majority_element(numbers): + m = float("inf") + cnt = 0 + for num in numbers: + if cnt == 0: + m = num + cnt += 1 + elif m == num: + cnt += 1 + else: + cnt -= 1 + # to verify the most frequent number is greater than length of aaray//2 + recheck_cnt = 0 + for num in numbers: + if num == m: + recheck_cnt += 1 + return m if recheck_cnt > len(numbers)//2 else None + +if __name__ == "__main__": + print(majority_element(numbers)) From e81e2f84097fe1b2a929cf64026bf6d29d603993 Mon Sep 17 00:00:00 2001 From: Sankalp Sharma Date: Mon, 17 May 2021 11:44:23 +0530 Subject: [PATCH 3/4] rotate array complete --- algorithms/Python/arrays/rotate_array.py | 26 ++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 algorithms/Python/arrays/rotate_array.py diff --git a/algorithms/Python/arrays/rotate_array.py b/algorithms/Python/arrays/rotate_array.py new file mode 100644 index 00000000..f4a621de --- /dev/null +++ b/algorithms/Python/arrays/rotate_array.py @@ -0,0 +1,26 @@ +""" +Algorithm Type: Array Traversal +Time Complexity: O(n) +""" +numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] +k = 3 + +def rotate_array(numbers, k): + n = len(numbers) + k = k % n + if k == 0: + return numbers + for i in range(n-k): + if i>((n-k)//2): + break + numbers[i], numbers[n-k-(i+1)] = numbers[n-k-(i+1)], numbers[i] + + for i in range(k): + if i>k//2: + break + numbers[-1-i], numbers[-k+i] = numbers[-k+i], numbers[-1-i] + reversed_numbers = numbers[::-1] + return reversed_numbers + +if __name__ == "__main__": + print(rotate_array(numbers, k)) From a6a38a28eb1337fb3771e4d5a8d8e6b4535583e9 Mon Sep 17 00:00:00 2001 From: Sankalp Sharma Date: Mon, 17 May 2021 11:46:39 +0530 Subject: [PATCH 4/4] updated readme --- algorithms/Python/README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/algorithms/Python/README.md b/algorithms/Python/README.md index 49dc6d79..f43cc1f8 100644 --- a/algorithms/Python/README.md +++ b/algorithms/Python/README.md @@ -2,6 +2,9 @@ ## Arrays 1. [Count Inversions](arrays/counting_inversions.py) +2. [Majority Element](arrays/majority_element.py) +3. [Rotate Array](arrays/rotate_array.py) +4. [Missing Number](arrays/missing_number.py) ## Linked Lists 1. [Doubly](linked_lists/doubly.py)