From 7e8cee4a18b47edefd73f5b24c234af1979633c8 Mon Sep 17 00:00:00 2001 From: Hassen K Mohammed Date: Wed, 17 Aug 2022 18:51:23 -0400 Subject: [PATCH 1/4] added example --- leetcode/array/ContainsDuplicatesII.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/leetcode/array/ContainsDuplicatesII.java b/leetcode/array/ContainsDuplicatesII.java index 81240ea2..e85c6ae3 100644 --- a/leetcode/array/ContainsDuplicatesII.java +++ b/leetcode/array/ContainsDuplicatesII.java @@ -1,6 +1,9 @@ //Given an array of integers and an integer k, find out whether there are two distinct indices i and //j in the array such that nums[i] = nums[j] and the absolute difference between i and j is at most k. +//example [2,5,-1,6,-9,5,8]. k=3, i=1, j= 5 |i-j|=4 4>3 --> false +//example [2,3,-1,3,-9,5,8]. k=3, i=1, j= 3 |i-j|=2 2<3 --> true + class ContainsDuplicatesII { public boolean containsNearbyDuplicate(int[] nums, int k) { HashMap map = new HashMap(); From 8921c0db42667f67c261ec3dec6f12c561bb3ad5 Mon Sep 17 00:00:00 2001 From: Hassen K Mohammed Date: Wed, 17 Aug 2022 18:52:45 -0400 Subject: [PATCH 2/4] Update ContainsDuplicatesII.java --- leetcode/array/ContainsDuplicatesII.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/leetcode/array/ContainsDuplicatesII.java b/leetcode/array/ContainsDuplicatesII.java index e85c6ae3..597acf58 100644 --- a/leetcode/array/ContainsDuplicatesII.java +++ b/leetcode/array/ContainsDuplicatesII.java @@ -1,8 +1,10 @@ //Given an array of integers and an integer k, find out whether there are two distinct indices i and //j in the array such that nums[i] = nums[j] and the absolute difference between i and j is at most k. -//example [2,5,-1,6,-9,5,8]. k=3, i=1, j= 5 |i-j|=4 4>3 --> false -//example [2,3,-1,3,-9,5,8]. k=3, i=1, j= 3 |i-j|=2 2<3 --> true +//Example + +// [2,5,-1,6,-9,5,8]. k=3, i=1, j= 5 |i-j|=4 4>3 --> false +// [2,3,-1,3,-9,5,8]. k=3, i=1, j= 3 |i-j|=2 2<3 --> true class ContainsDuplicatesII { public boolean containsNearbyDuplicate(int[] nums, int k) { From 8ed8ab9955a4f95ce6cf753ca371703e657dc0bb Mon Sep 17 00:00:00 2001 From: Hassen K Mohammed Date: Wed, 17 Aug 2022 20:54:23 -0400 Subject: [PATCH 3/4] Solved without extra space The suggestion was to solve it without extra space, but this example uses hash map that needs extra space. My solution here will not need extra space --- .../FindAllNumbersDisappearedInAnArray.java | 25 +++++++------------ 1 file changed, 9 insertions(+), 16 deletions(-) diff --git a/leetcode/array/FindAllNumbersDisappearedInAnArray.java b/leetcode/array/FindAllNumbersDisappearedInAnArray.java index c9ca7780..db2d5900 100644 --- a/leetcode/array/FindAllNumbersDisappearedInAnArray.java +++ b/leetcode/array/FindAllNumbersDisappearedInAnArray.java @@ -14,24 +14,17 @@ class FindAllNumbersDisappearedInAnArray { public List findDisappearedNumbers(int[] nums) { - List result = new ArrayList(); - HashMap map = new HashMap(); - for(int i = 1; i <= nums.length; i++) { - map.put(i, 1); - } - - for(int i = 0; i < nums.length; i++) { - if(map.containsKey(nums[i])) { - map.put(nums[i], -1); - } - } - - for(int i: map.keySet()) { - if(map.get(i) != -1) { - result.add(i); + List result = new ArrayList<>(); + for (int i = 0; i < ary.length; i++) { + if (ary[i] < i + 1) { + result.add(i + 1); + } else if (ary[i] > i + 1 && ary[ary[i] - 1]!=ary[i]) { + int temp = ary[ary[i] - 1]; + ary[ary[i] - 1] = ary[i]; + ary[i] = temp; + i--; } } - return result; } } From 9075a8d2fad654dce225a4982b065c5d40d157d4 Mon Sep 17 00:00:00 2001 From: Hassen K Mohammed Date: Wed, 17 Aug 2022 21:00:23 -0400 Subject: [PATCH 4/4] fixed --- leetcode/array/FindAllNumbersDisappearedInAnArray.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/leetcode/array/FindAllNumbersDisappearedInAnArray.java b/leetcode/array/FindAllNumbersDisappearedInAnArray.java index db2d5900..e79ff356 100644 --- a/leetcode/array/FindAllNumbersDisappearedInAnArray.java +++ b/leetcode/array/FindAllNumbersDisappearedInAnArray.java @@ -23,6 +23,8 @@ public List findDisappearedNumbers(int[] nums) { ary[ary[i] - 1] = ary[i]; ary[i] = temp; i--; + } else if (ary[i]!=i+1) { + result.add(i + 1); } } return result;