my $j = 1; my $p = $s[1]; #values are present starting from 1st index while ($j != $n - 1) { if ( ($p < $s[$j+1]) || ($p == $s[$j+1]) ) { }else{ $p = $s[$j+1]; } $j++; }