From da50b106f4f2e6830d84c01c61eeff4cec92a915 Mon Sep 17 00:00:00 2001 From: joltedThought <95027707+joltedThought@users.noreply.github.com> Date: Fri, 5 Jan 2024 23:39:27 +0530 Subject: [PATCH 1/4] added optimised twoSum solution --- src/main/kotlin/math/TwoSum.kt | 31 +++++++++++++++++++++++++++++++ src/test/kotlin/math/TwoSum.kt | 10 ++++++++++ 2 files changed, 41 insertions(+) diff --git a/src/main/kotlin/math/TwoSum.kt b/src/main/kotlin/math/TwoSum.kt index 9ca0657..6e0fc97 100644 --- a/src/main/kotlin/math/TwoSum.kt +++ b/src/main/kotlin/math/TwoSum.kt @@ -24,3 +24,34 @@ fun twoSum(nums: IntArray, target: Int): IntArray{ return intArrayOf(0,1) } + +/** + * Approach 2: Using HashMap + * + * Complexity Analysis: + * + * Time complexity: O(n) + * Space complexity: O(n) + * + * Create an empty mutableMap and for every num in nums + * if map contains target-num, return num and target-num, + * otherwise add num, this approach returns all distinct pairs + * of such pairs. + * @param nums Array of integers. + * @param target Integer target. + * @return Indices of the two numbers such that they add up to target. + */ +fun twoSumOptimised(nums: IntArray, target: Int): IntArray{ + + val map: MutableMap = HashMap() + for(num in nums) { + val targetDiff = target - num; + if(map[targetDiff] == null) + map[num] = 1 + else return intArrayOf(num, targetDiff) + } + + return intArrayOf(0,1) + + +} diff --git a/src/test/kotlin/math/TwoSum.kt b/src/test/kotlin/math/TwoSum.kt index 7e5e00f..0efcf20 100644 --- a/src/test/kotlin/math/TwoSum.kt +++ b/src/test/kotlin/math/TwoSum.kt @@ -12,4 +12,14 @@ class TwoSumTest { val result = intArrayOf(0,1) assert(twoSum(array,target).contentEquals(result)) } + + @Test + fun testTwoSumOptimised(){ + val array = IntArray(2) + array[0] = 3 + array[1] = 3 + val target = 6 + val result = intArrayOf(0,1) + assert(twoSum(array,target).contentEquals(result)) + } } \ No newline at end of file From 43899ca096787044398e6f9a399acf52f6429f34 Mon Sep 17 00:00:00 2001 From: joltedThought <95027707+joltedThought@users.noreply.github.com> Date: Sat, 6 Jan 2024 01:34:19 +0530 Subject: [PATCH 2/4] fixed testcase of optimised twoSum solution --- src/test/kotlin/math/TwoSum.kt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/test/kotlin/math/TwoSum.kt b/src/test/kotlin/math/TwoSum.kt index 0efcf20..06fab55 100644 --- a/src/test/kotlin/math/TwoSum.kt +++ b/src/test/kotlin/math/TwoSum.kt @@ -19,7 +19,7 @@ class TwoSumTest { array[0] = 3 array[1] = 3 val target = 6 - val result = intArrayOf(0,1) - assert(twoSum(array,target).contentEquals(result)) + val result = intArrayOf(3,3) + assert(twoSumOptimised(array,target).contentEquals(result)) } } \ No newline at end of file From f5ebcaeae252f8ae3be1ba5433ea7d67b7b28ab7 Mon Sep 17 00:00:00 2001 From: Abhishek Tiwari Date: Sat, 6 Jan 2024 01:41:15 +0530 Subject: [PATCH 3/4] updated logic for optimised two sum --- src/main/kotlin/math/TwoSum.kt | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/kotlin/math/TwoSum.kt b/src/main/kotlin/math/TwoSum.kt index 6e0fc97..047bde6 100644 --- a/src/main/kotlin/math/TwoSum.kt +++ b/src/main/kotlin/math/TwoSum.kt @@ -39,19 +39,19 @@ fun twoSum(nums: IntArray, target: Int): IntArray{ * of such pairs. * @param nums Array of integers. * @param target Integer target. - * @return Indices of the two numbers such that they add up to target. + * @return the two numbers such that they add up to target. */ fun twoSumOptimised(nums: IntArray, target: Int): IntArray{ val map: MutableMap = HashMap() - for(num in nums) { + for(num in nums) { val targetDiff = target - num; if(map[targetDiff] == null) map[num] = 1 else return intArrayOf(num, targetDiff) } - return intArrayOf(0,1) + return intArrayOf() } From 57b85349a1f360297e5380960fe8e1d1d688eac1 Mon Sep 17 00:00:00 2001 From: Abhishek Tiwari Date: Sun, 7 Jan 2024 00:39:11 +0530 Subject: [PATCH 4/4] updated logic for optimised two sum test --- src/main/kotlin/math/TwoSum.kt | 9 ++++++--- src/test/kotlin/math/TwoSum.kt | 12 ++++++------ 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/src/main/kotlin/math/TwoSum.kt b/src/main/kotlin/math/TwoSum.kt index 047bde6..77e324a 100644 --- a/src/main/kotlin/math/TwoSum.kt +++ b/src/main/kotlin/math/TwoSum.kt @@ -41,17 +41,20 @@ fun twoSum(nums: IntArray, target: Int): IntArray{ * @param target Integer target. * @return the two numbers such that they add up to target. */ -fun twoSumOptimised(nums: IntArray, target: Int): IntArray{ +fun twoSumOptimised(nums: IntArray, target: Int): Array>{ + val array: MutableList> = mutableListOf() val map: MutableMap = HashMap() for(num in nums) { val targetDiff = target - num; if(map[targetDiff] == null) map[num] = 1 - else return intArrayOf(num, targetDiff) + else { + array.add(Pair(targetDiff, num)) + } } - return intArrayOf() + return array.toTypedArray() } diff --git a/src/test/kotlin/math/TwoSum.kt b/src/test/kotlin/math/TwoSum.kt index 06fab55..e38d247 100644 --- a/src/test/kotlin/math/TwoSum.kt +++ b/src/test/kotlin/math/TwoSum.kt @@ -15,11 +15,11 @@ class TwoSumTest { @Test fun testTwoSumOptimised(){ - val array = IntArray(2) - array[0] = 3 - array[1] = 3 - val target = 6 - val result = intArrayOf(3,3) - assert(twoSumOptimised(array,target).contentEquals(result)) + val array = intArrayOf(1, 0, -1, 2, 4, 5, 3, 2) + val target: Int = 4 + val result = twoSumOptimised(array, target).apply { + this.all { it.first + it.second == target } + } + assert(result.isNotEmpty()) } } \ No newline at end of file