From 42318bb38de59268bbe2e7afa3418338f8169216 Mon Sep 17 00:00:00 2001 From: Morvin-Ian Date: Fri, 1 Nov 2024 18:05:58 +0300 Subject: [PATCH 1/2] more arrays --- data_structures/arrays/array_leader.py | 21 ++++++++++++++ .../arrays/max_product_subarray.py | 19 +++++++++++++ data_structures/arrays/max_subarray.py | 28 +++++++++++++++++++ data_structures/arrays/missing_number.py | 25 +++++++++++++++++ data_structures/arrays/rotate_elements.py | 19 +++++++++++++ 5 files changed, 112 insertions(+) create mode 100644 data_structures/arrays/array_leader.py create mode 100644 data_structures/arrays/max_product_subarray.py create mode 100644 data_structures/arrays/max_subarray.py create mode 100644 data_structures/arrays/missing_number.py create mode 100644 data_structures/arrays/rotate_elements.py diff --git a/data_structures/arrays/array_leader.py b/data_structures/arrays/array_leader.py new file mode 100644 index 000000000000..4e3311fc1541 --- /dev/null +++ b/data_structures/arrays/array_leader.py @@ -0,0 +1,21 @@ +""" +Given an array arr[] of size n, the task is to find all the Leaders in the array. + An element is a Leader if it is greater than all the elements to its right side. + +Note: The rightmost element is always a leader. +""" + +def array_leader(arr): + leaders = [] + for i in range(len(arr)): + for j in range(i+1, len(arr)): + if arr[i] < arr[j]: + break + else: + leaders.append(arr[i]) + + return " ".join(map(str, leaders)) + + +# Test the function with the provided input +print(array_leader([16, 17, 4, 3, 5, 2])) # Expected output: 17 5 2 \ No newline at end of file diff --git a/data_structures/arrays/max_product_subarray.py b/data_structures/arrays/max_product_subarray.py new file mode 100644 index 000000000000..7d608afe6593 --- /dev/null +++ b/data_structures/arrays/max_product_subarray.py @@ -0,0 +1,19 @@ +""" +Given an integer array, the task is to find the maximum product of any subarray. +Input: arr[] = {-1, -3, -10, 0, 60} +Output: 60 +Explanation: The subarray with maximum product is {60}. +""" + +def max_product(arr): + result = 0 + + for x in range(len(arr)): + current_product = 1 + for y in range(x, len(arr)): + current_product *= arr[y] + result = max(result, current_product) + return result + +# Test the function with the provided input +print(max_product([-1, -3, -10, 0, 60])) # Expected output: 60 \ No newline at end of file diff --git a/data_structures/arrays/max_subarray.py b/data_structures/arrays/max_subarray.py new file mode 100644 index 000000000000..f0bd6478d5da --- /dev/null +++ b/data_structures/arrays/max_subarray.py @@ -0,0 +1,28 @@ +""" +Input: arr[] = {2, 3, -8, 7, -1, 2, 3} +Output: 11 +Explanation: The subarray {7, -1, 2, 3} has the largest sum 11. + +Input: arr[] = {-2, -4} +Output: –2 +Explanation: The subarray {-2} has the largest sum -2. + +Input: arr[] = {5, 4, 1, 7, 8} +Output: 25 +Explanation: The subarray {5, 4, 1, 7, 8} has the largest sum 25. +""" + +def max_subarray(arr): + result = arr[0] + + for x in range(len(arr)): + current_sum = 0 + for y in range(x, len(arr)): + current_sum += arr[y] + result = max(result, current_sum) + return result + +# Test the function with the provided inputs +print(max_subarray([2, 3, -8, 7, -1, 2, 3])) # Expected output: 11 +print(max_subarray([-2, -4])) # Expected output: -2 +print(max_subarray([5, 4, 1, 7, 8])) # Expected output: 25 \ No newline at end of file diff --git a/data_structures/arrays/missing_number.py b/data_structures/arrays/missing_number.py new file mode 100644 index 000000000000..c59ae26bf7eb --- /dev/null +++ b/data_structures/arrays/missing_number.py @@ -0,0 +1,25 @@ +''' +Given an array arr[] of size n-1 with integers in the range of [1, n], the task is to find the missing number from the first N integers. + +Note: There are no duplicates in the list. + +Input: arr[] = {1, 2, 4, 6, 3, 7, 8} , n = 8 +Output: 5 +Explanation: Here the size of the array is 8, so the range will be [1, 8]. The missing number between 1 to 8 is 5 + +Input: arr[] = {1, 2, 3, 5}, n = 5 +Output: 4 +Explanation: Here the size of the array is 4, so the range will be [1, 5]. The missing number between 1 to 5 is 4 +''' + +def find_missing_number(arr, n): + hash = [0] * (n+1) + + for num in arr: + hash[num] += 1 + for i in range(1, (n+1)): + if hash[i] == 0: + return i + return -1 + +print(find_missing_number([1,2,3,4,6,7,8], 8)) \ No newline at end of file diff --git a/data_structures/arrays/rotate_elements.py b/data_structures/arrays/rotate_elements.py new file mode 100644 index 000000000000..247ad02eccd9 --- /dev/null +++ b/data_structures/arrays/rotate_elements.py @@ -0,0 +1,19 @@ +""" +Given an Array of size N and a value K, +around which we need to right rotate the array. +How do you quickly print the right rotated array? + +Input: Array[] = {1, 3, 5, 7, 9}, K = 2. +Output: 7 9 1 3 5 +Explanation: +After 1st rotation – {9, 1, 3, 5, 7}After 2nd rotation – {7, 9, 1, 3, 5} +""" + +def right_rotate(arr, k): + n = len(arr) + k = k % n + arr = arr[n-k:] + arr[:n-k] + return arr + +# Test the function with the provided input +print(right_rotate([1, 3, 5, 7, 9], 2)) # Expected output: [7, 9, 1, 3, 5] From 414afc5eeccdb182966e18a8d68c423a2cd08707 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 3 Nov 2024 12:25:54 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- data_structures/arrays/array_leader.py | 9 +++++---- data_structures/arrays/max_product_subarray.py | 6 ++++-- data_structures/arrays/max_subarray.py | 4 +++- data_structures/arrays/missing_number.py | 12 +++++++----- data_structures/arrays/rotate_elements.py | 8 +++++--- 5 files changed, 24 insertions(+), 15 deletions(-) diff --git a/data_structures/arrays/array_leader.py b/data_structures/arrays/array_leader.py index 4e3311fc1541..fe03878bd40e 100644 --- a/data_structures/arrays/array_leader.py +++ b/data_structures/arrays/array_leader.py @@ -2,20 +2,21 @@ Given an array arr[] of size n, the task is to find all the Leaders in the array. An element is a Leader if it is greater than all the elements to its right side. -Note: The rightmost element is always a leader. +Note: The rightmost element is always a leader. """ + def array_leader(arr): leaders = [] for i in range(len(arr)): - for j in range(i+1, len(arr)): + for j in range(i + 1, len(arr)): if arr[i] < arr[j]: break else: leaders.append(arr[i]) - + return " ".join(map(str, leaders)) # Test the function with the provided input -print(array_leader([16, 17, 4, 3, 5, 2])) # Expected output: 17 5 2 \ No newline at end of file +print(array_leader([16, 17, 4, 3, 5, 2])) # Expected output: 17 5 2 diff --git a/data_structures/arrays/max_product_subarray.py b/data_structures/arrays/max_product_subarray.py index 7d608afe6593..fde2d3f75bbe 100644 --- a/data_structures/arrays/max_product_subarray.py +++ b/data_structures/arrays/max_product_subarray.py @@ -1,10 +1,11 @@ """ -Given an integer array, the task is to find the maximum product of any subarray. +Given an integer array, the task is to find the maximum product of any subarray. Input: arr[] = {-1, -3, -10, 0, 60} Output: 60 Explanation: The subarray with maximum product is {60}. """ + def max_product(arr): result = 0 @@ -15,5 +16,6 @@ def max_product(arr): result = max(result, current_product) return result + # Test the function with the provided input -print(max_product([-1, -3, -10, 0, 60])) # Expected output: 60 \ No newline at end of file +print(max_product([-1, -3, -10, 0, 60])) # Expected output: 60 diff --git a/data_structures/arrays/max_subarray.py b/data_structures/arrays/max_subarray.py index f0bd6478d5da..d4733be490b2 100644 --- a/data_structures/arrays/max_subarray.py +++ b/data_structures/arrays/max_subarray.py @@ -12,6 +12,7 @@ Explanation: The subarray {5, 4, 1, 7, 8} has the largest sum 25. """ + def max_subarray(arr): result = arr[0] @@ -22,7 +23,8 @@ def max_subarray(arr): result = max(result, current_sum) return result + # Test the function with the provided inputs print(max_subarray([2, 3, -8, 7, -1, 2, 3])) # Expected output: 11 print(max_subarray([-2, -4])) # Expected output: -2 -print(max_subarray([5, 4, 1, 7, 8])) # Expected output: 25 \ No newline at end of file +print(max_subarray([5, 4, 1, 7, 8])) # Expected output: 25 diff --git a/data_structures/arrays/missing_number.py b/data_structures/arrays/missing_number.py index c59ae26bf7eb..a327a12dc4fa 100644 --- a/data_structures/arrays/missing_number.py +++ b/data_structures/arrays/missing_number.py @@ -1,4 +1,4 @@ -''' +""" Given an array arr[] of size n-1 with integers in the range of [1, n], the task is to find the missing number from the first N integers. Note: There are no duplicates in the list. @@ -10,16 +10,18 @@ Input: arr[] = {1, 2, 3, 5}, n = 5 Output: 4 Explanation: Here the size of the array is 4, so the range will be [1, 5]. The missing number between 1 to 5 is 4 -''' +""" + def find_missing_number(arr, n): - hash = [0] * (n+1) + hash = [0] * (n + 1) for num in arr: hash[num] += 1 - for i in range(1, (n+1)): + for i in range(1, (n + 1)): if hash[i] == 0: return i return -1 -print(find_missing_number([1,2,3,4,6,7,8], 8)) \ No newline at end of file + +print(find_missing_number([1, 2, 3, 4, 6, 7, 8], 8)) diff --git a/data_structures/arrays/rotate_elements.py b/data_structures/arrays/rotate_elements.py index 247ad02eccd9..137145f7c06b 100644 --- a/data_structures/arrays/rotate_elements.py +++ b/data_structures/arrays/rotate_elements.py @@ -1,6 +1,6 @@ """ -Given an Array of size N and a value K, -around which we need to right rotate the array. +Given an Array of size N and a value K, +around which we need to right rotate the array. How do you quickly print the right rotated array? Input: Array[] = {1, 3, 5, 7, 9}, K = 2. @@ -9,11 +9,13 @@ After 1st rotation – {9, 1, 3, 5, 7}After 2nd rotation – {7, 9, 1, 3, 5} """ + def right_rotate(arr, k): n = len(arr) k = k % n - arr = arr[n-k:] + arr[:n-k] + arr = arr[n - k :] + arr[: n - k] return arr + # Test the function with the provided input print(right_rotate([1, 3, 5, 7, 9], 2)) # Expected output: [7, 9, 1, 3, 5]