diff options
| -rw-r--r-- | challenge-106/cristian-heredia/perl/ch_1.pl | 53 | ||||
| -rw-r--r-- | challenge-106/cristian-heredia/python/ch_1.py | 48 |
2 files changed, 101 insertions, 0 deletions
diff --git a/challenge-106/cristian-heredia/perl/ch_1.pl b/challenge-106/cristian-heredia/perl/ch_1.pl new file mode 100644 index 0000000000..d27b24e3ab --- /dev/null +++ b/challenge-106/cristian-heredia/perl/ch_1.pl @@ -0,0 +1,53 @@ +=begin + + TASK #1 › Maximum Gap + Submitted by: Mohammad S Anwar + You are given an array of integers @N. + + Write a script to display the maximum difference between two successive elements once the array is sorted. + + If the array contains only 1 element then display 0. + + Example + Input: @N = (2, 9, 3, 5) + Output: 4 + + Input: @N = (1, 3, 8, 2, 0) + Output: 5 + + Input: @N = (5) + Output: 0 + +=end +=cut + + +use strict; +use warnings; +use Data::Dumper; + + +#Input +my @N = (2, 9, 3, 5); + +my $result = 0; +my $sub = 0; + +if (@N != 1) { + my @sorted = sort { $a <=> $b } @N; + for (my $i= 0; $i<@N-1; $i++) { + $sub = $sorted[$i+1] - $sorted[$i]; + if ($sub > $result) { + $result = $sub; + } + } +} +print "$result\n"; + + + + + + + + diff --git a/challenge-106/cristian-heredia/python/ch_1.py b/challenge-106/cristian-heredia/python/ch_1.py new file mode 100644 index 0000000000..0bdb1efffb --- /dev/null +++ b/challenge-106/cristian-heredia/python/ch_1.py @@ -0,0 +1,48 @@ +''' + + TASK #1 › Maximum Gap + Submitted by: Mohammad S Anwar + You are given an array of integers @N. + + Write a script to display the maximum difference between two successive elements once the array is sorted. + + If the array contains only 1 element then display 0. + + Example + Input: @N = (2, 9, 3, 5) + Output: 4 + + Input: @N = (1, 3, 8, 2, 0) + Output: 5 + + Input: @N = (5) + Output: 0 + +''' + + +#Input +N = [2, 9, 3, 5] + +result = 0 +subs = 0 +counter = 0 + + +if (len(N) != 1): + sortedList = sorted(N) + while (counter < len(N)-1): + subs = sortedList[counter+1] - sortedList[counter] + if (subs > result): + result = subs; + counter += 1 + +print (result); + + + + + + + + |
