Perl Interview Questions and Answers - March 2023 Updated 2
Find Lowest Missing Numbers in perl
The given program is a Perl script designed to find the lowest missing numbers from an array of integers. The script begins by initializing an array of integers named "@arr", which contains a set of random integers. The script then creates a hash called "%hash" that maps each integer in "@arr" to the value 1.
Next, the script finds the maximum integer in "@arr" using the "sort" function and saves it to a variable named "$max". It also sets the value of the minimum integer to -1, and initializes an empty array called "@missing" to store the missing numbers.
The script then loops through each integer from the minimum to the maximum value in the array "@arr". For each integer, it checks if that integer exists in the hash "%hash". If it doesn't exist, then it's considered a missing number, and it's added to the "@missing" array.
Finally, the script prints out the "@missing" array, which contains all the lowest missing integers.
use strict; use warnings; my @arr = (8, 4, 3, 1, 0, -1, 6, 10); my %hash = map { $_ => 1 } @arr; my $min = -1; my $max = (sort { $a <=> $b } @arr)[-1]; my @missing; while ($min <= $max) { $min++; push @missing, $min unless exists $hash{$min}; } print "The most lowest missing numbers are: @missing\n";
0 Comments:
Post a Comment
Note: only a member of this blog may post a comment.
<< Home