From 1ec340ce5d301e4eca20acab0058683ccaaeb9c6 Mon Sep 17 00:00:00 2001 From: Ayush Dwivedi Date: Sun, 12 Apr 2026 02:17:32 +0530 Subject: [PATCH 1/2] Improve two_pointer documentation and readability Enhanced the docstring for better clarity, added explanation of approach, time and space complexity, and improved formatting. --- maths/two_pointer.py | 33 ++++++++++++++++++++++----------- 1 file changed, 22 insertions(+), 11 deletions(-) diff --git a/maths/two_pointer.py b/maths/two_pointer.py index 8a6d8eb7aff0..d93aa82176bb 100644 --- a/maths/two_pointer.py +++ b/maths/two_pointer.py @@ -1,21 +1,32 @@ -""" + """ +Two Pointer Technique for Two Sum Problem (Sorted Array) + Given a sorted array of integers, return indices of the two numbers such that they add up to a specific target using the two pointers technique. -You may assume that each input would have exactly one solution, and you -may not use the same element twice. +Assumptions: +- The input array is sorted in non-decreasing order. +- Each input has exactly one solution. +- The same element cannot be used twice. -This is an alternative solution of the two-sum problem, which uses a -map to solve the problem. Hence can not solve the issue if there is a -constraint not use the same index twice. [1] +Approach: +- Initialize two pointers: + left pointer at the beginning (index 0) + right pointer at the end (index n-1) +- Compare sum of elements at both pointers: + - If sum == target → return indices + - If sum < target → move left pointer forward + - If sum > target → move right pointer backward -Example: -Given nums = [2, 7, 11, 15], target = 9, +Time Complexity: O(n) +Space Complexity: O(1) -Because nums[0] + nums[1] = 2 + 7 = 9, -return [0, 1]. +Example: +Input: nums = [2, 7, 11, 15], target = 9 +Output: [0, 1] -[1]: https://github.com/TheAlgorithms/Python/blob/master/other/two_sum.py +Reference: +https://github.com/TheAlgorithms/Python/blob/master/other/two_sum.py """ from __future__ import annotations From abdc97f81c989f6cd721ac356534f620598395a6 Mon Sep 17 00:00:00 2001 From: Ayush Dwivedi Date: Sun, 12 Apr 2026 02:56:36 +0530 Subject: [PATCH 2/2] fix: remove indentation error and improve formatting Updated docstring to clarify the purpose of the function. --- maths/two_pointer.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/maths/two_pointer.py b/maths/two_pointer.py index d93aa82176bb..ee1a04906d1f 100644 --- a/maths/two_pointer.py +++ b/maths/two_pointer.py @@ -1,4 +1,4 @@ - """ +""" Two Pointer Technique for Two Sum Problem (Sorted Array) Given a sorted array of integers, return indices of the two numbers such