title: Build Lowest Number by Removing n digits from a given number
body: <p>It takes as input a number(string) and then the task is to remove n numbers to give a resulting lowest possible number, but you have to take care of the order, that is the constraint. You cannot change the order of the original numbers.</p>
  
  <p>I wanted it to work in O(n), so I did this:</p>
  
  <pre><code>#!/usr/bin/perl
  #lowestNS.pl
  #Date: 2016-06-28
  
  use warnings;
  use strict;
  use utf8;
  
  (@ARGV == 2) or die "2 args needed";
  my $num = $ARGV[0];
  my $d = $ARGV[1];
  my @arr;
  
  int($num) &gt; 0 or die "Enter positive number";
  
  print "Number in: $num\nDel: $d\n";
  
  if(int($num) == 0) {
      print "Result: 0\n";
      exit;
  }
  else {
      my $str = $num;
      @arr = split(//, $str); #/Split on each number
      #Split and multiply by reverse index, to give precedence to the order of numbers
      for(my $i = 0; $i &lt; @arr; $i++) {
          $arr[$i] *= (@arr - $i);
      }
  }
  
  print "arr: " . join(',' , @arr) . "\n";
  
  for (my $j = 0; $j &lt; $d; $j++) {
      my $max = $arr[0];
      my $m_index = -1;
  
      #replace nth maximum with -1
  
      for (my $i = 0; $i &lt; @arr; $i++) {
          if($max &lt;= $arr[$i]) {
              $max = $arr[$i];
              $m_index = $i;
          }
      }
      $arr[$m_index] = -1;
  }
  
  
  #return all numbers with value other than -1
  
  my $result = "";
  
  for (my $i = 0; $i &lt; @arr; $i++) {
      if($arr[$i] != -1){
          $result = $result . "" . $arr[$i]/(@arr - $i);
      }
  }
  
  print "Result: $result\n";
  </code></pre>
  
  <p>It works in all cases, except, cases like: <br>
  <code>Number = 765028321</code>
  <code>Delete = 5</code> <br>
  The problem is the removal of 7650<code>2</code>8321 when it should have removed the 765028<code>3</code>21.<br>
  because 2*5 > 3*3.</p>