From 0172c299c7cb3394ff8700c71c2cc00d24451994 Mon Sep 17 00:00:00 2001 From: Azad Date: Wed, 4 Nov 2020 16:28:51 +0530 Subject: [PATCH 1/6] Added New Java file in Java/Misc --- Misc/largestRange.java | 46 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 Misc/largestRange.java diff --git a/Misc/largestRange.java b/Misc/largestRange.java new file mode 100644 index 000000000000..fd61c38446d2 --- /dev/null +++ b/Misc/largestRange.java @@ -0,0 +1,46 @@ +import java.util.*; +public class largestRange +{ + // Finds the length of longest occurring consecutive numbers range in an array + public static int longestRange(int[] nums) + { + int longestRange = 0; + HashMap num = new HashMap<>(); // Stores a mapping of a number to whether the current number is part of a particular consecutive range or not + for(int x : nums) + num.put(x, true); + for(int x : nums) + { + if(!num.get(x)) + continue; + num.replace(x, false); + int currentRange=1; + int left = x - 1; + int right = x + 1; + while(num.containsKey(left)) // Search leftwards for consecutive range + { + num.replace(left, false); + currentRange+=1; + left--; + } + while(num.containsKey(right)) // Search rightwards for consecutive range + { + num.replace(right, false); + currentRange+=1; + right++; + } + if(currentRange > longestRange) // Store longest range at every interation + longestRange = currentRange; + } + return longestRange; + } + + public static void main(String[] args) { + // Testcases + assert longestRange(new int[]{1, 2, 3, 4, -1, 11, 10}) == 4; + // The longest consecutive number range is of length 4 i.e. {1, 2, 3, 4} + assert longestRange(new int[]{-1, 1, 3, 5, 7})==1; + // The longest consecutive number range is of length 1 i.e. any of the element alone + assert longestRange(new int[]{0, 1, 2, 3, 4, 7, 6, 5})==8; + // The longest consecutive number range is of length 8 i.e. {0, 1, 2, 3, 4, 5, 6, 7} + } +} From ee34da41ed97b131f9356260370107fd6aba3d96 Mon Sep 17 00:00:00 2001 From: Azad Nautiyal Date: Wed, 4 Nov 2020 16:38:42 +0530 Subject: [PATCH 2/6] Update largestRange.java --- Misc/largestRange.java | 1 + 1 file changed, 1 insertion(+) diff --git a/Misc/largestRange.java b/Misc/largestRange.java index fd61c38446d2..6877a0a600aa 100644 --- a/Misc/largestRange.java +++ b/Misc/largestRange.java @@ -1,3 +1,4 @@ +package Misc; import java.util.*; public class largestRange { From 4bee137ea6f5318f95c71b06c343a59f9309717c Mon Sep 17 00:00:00 2001 From: Azad Nautiyal Date: Wed, 4 Nov 2020 16:40:53 +0530 Subject: [PATCH 3/6] Update largestRange.java --- Misc/largestRange.java | 1 + 1 file changed, 1 insertion(+) diff --git a/Misc/largestRange.java b/Misc/largestRange.java index 6877a0a600aa..7dc300450834 100644 --- a/Misc/largestRange.java +++ b/Misc/largestRange.java @@ -1,4 +1,5 @@ package Misc; + import java.util.*; public class largestRange { From 603e0100e177be498bedad122498b1e69d1d7805 Mon Sep 17 00:00:00 2001 From: Azad Nautiyal Date: Wed, 4 Nov 2020 18:24:12 +0530 Subject: [PATCH 4/6] Update largestRange.java --- Misc/largestRange.java | 84 +++++++++++++++++++++--------------------- 1 file changed, 42 insertions(+), 42 deletions(-) diff --git a/Misc/largestRange.java b/Misc/largestRange.java index 7dc300450834..3b6a7f48ff7c 100644 --- a/Misc/largestRange.java +++ b/Misc/largestRange.java @@ -1,48 +1,48 @@ package Misc; import java.util.*; -public class largestRange -{ - // Finds the length of longest occurring consecutive numbers range in an array - public static int longestRange(int[] nums) - { - int longestRange = 0; - HashMap num = new HashMap<>(); // Stores a mapping of a number to whether the current number is part of a particular consecutive range or not - for(int x : nums) - num.put(x, true); - for(int x : nums) - { - if(!num.get(x)) - continue; - num.replace(x, false); - int currentRange=1; - int left = x - 1; - int right = x + 1; - while(num.containsKey(left)) // Search leftwards for consecutive range - { - num.replace(left, false); - currentRange+=1; - left--; - } - while(num.containsKey(right)) // Search rightwards for consecutive range - { - num.replace(right, false); - currentRange+=1; - right++; - } - if(currentRange > longestRange) // Store longest range at every interation - longestRange = currentRange; - } - return longestRange; - } - public static void main(String[] args) { - // Testcases - assert longestRange(new int[]{1, 2, 3, 4, -1, 11, 10}) == 4; - // The longest consecutive number range is of length 4 i.e. {1, 2, 3, 4} - assert longestRange(new int[]{-1, 1, 3, 5, 7})==1; - // The longest consecutive number range is of length 1 i.e. any of the element alone - assert longestRange(new int[]{0, 1, 2, 3, 4, 7, 6, 5})==8; - // The longest consecutive number range is of length 8 i.e. {0, 1, 2, 3, 4, 5, 6, 7} +public class largestRange { + + // Finds the length of longest occurring consecutive numbers range in an array + public static int longestRange(int[] nums) { + int longestRange = 0; + HashMap num = new HashMap<>(); + + /** + * Stores a mapping of a number to whether the current number is part of a particular + * consecutive range or not. + */ + for (int x : nums) num.put(x, true); + for (int x : nums) { + if (!num.get(x)) continue; + num.replace(x, false); + int currentRange = 1; + int left = x - 1; + int right = x + 1; + while (num.containsKey(left)) { // Search leftwards for consecutive range + num.replace(left, false); + currentRange += 1; + left--; + } + while (num.containsKey(right)) { // Search rightwards for consecutive range + num.replace(right, false); + currentRange += 1; + right++; + } + if (currentRange > longestRange) + longestRange = currentRange; // Store longest range at every interation } + return longestRange; + } + + public static void main(String[] args) { + // Testcases + assert longestRange(new int[] {1, 2, 3, 4, -1, 11, 10}) == 4; + // The longest consecutive number range is of length 4 i.e. {1, 2, 3, 4} + assert longestRange(new int[] {-1, 1, 3, 5, 7}) == 1; + // The longest consecutive number range is of length 1 i.e. any of the element alone + assert longestRange(new int[] {0, 1, 2, 3, 4, 7, 6, 5}) == 8; + // The longest consecutive number range is of length 8 i.e. {0, 1, 2, 3, 4, 5, 6, 7} + } } From a6f88cd038fac7b41a28d8302c87e20f10280516 Mon Sep 17 00:00:00 2001 From: Azad Nautiyal Date: Wed, 4 Nov 2020 18:27:45 +0530 Subject: [PATCH 5/6] Update largestRange.java --- Misc/largestRange.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/Misc/largestRange.java b/Misc/largestRange.java index 3b6a7f48ff7c..f24e5e2438a8 100644 --- a/Misc/largestRange.java +++ b/Misc/largestRange.java @@ -1,5 +1,3 @@ -package Misc; - import java.util.*; public class largestRange { From 653fc6061c4772f905563f2027ce7cac059b7672 Mon Sep 17 00:00:00 2001 From: Azad Nautiyal Date: Sat, 7 Nov 2020 01:42:46 +0530 Subject: [PATCH 6/6] Update largestRange.java --- Misc/largestRange.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/largestRange.java b/Misc/largestRange.java index f24e5e2438a8..fd6d521f82f9 100644 --- a/Misc/largestRange.java +++ b/Misc/largestRange.java @@ -39,7 +39,7 @@ public static void main(String[] args) { assert longestRange(new int[] {1, 2, 3, 4, -1, 11, 10}) == 4; // The longest consecutive number range is of length 4 i.e. {1, 2, 3, 4} assert longestRange(new int[] {-1, 1, 3, 5, 7}) == 1; - // The longest consecutive number range is of length 1 i.e. any of the element alone + // The longest consecutive number range is of length 1 i.e. any of the elements alone assert longestRange(new int[] {0, 1, 2, 3, 4, 7, 6, 5}) == 8; // The longest consecutive number range is of length 8 i.e. {0, 1, 2, 3, 4, 5, 6, 7} }