Monday, February 22, 2016

Maximize a number by deleting a repeated number.

For eg:

Question : 223336226

Answer: 23336226

Other removal of numbers possible: 22336226, 22333626

Code:

int solution(int X){ 
   int max=1,val=X; 
   while(val/10!=0){ 
     max*=10; 
     val/=10; 
   } 
   boolean del=true; 
   int pos=-1,cur=-1,rep=0,max1=max; 
   val=X; 
   while(del && max!=0){ 
     int new_cur=val/max; 
     if(rep==1 && new_cur > cur){ 
       pos=max*10; 
       del=false; 
     } 
     else if(new_cur==cur){ 
       pos=max; 
     } 
     if(new_cur==cur) 
       rep = 1; 
     else 
       rep = 0; 
     val=val%max; 
     max=max/10; 
     cur=new_cur; 
   } 
   int new_num=0,max_new=max1/10; 
   val=X; 
   while(max1!=0){ 
     if(max1==pos){ 
       max1/=10; 
       continue; 
     } 
     int num=(val/max1)%10; 
     new_num+=max_new*num; 
     max_new/=10; 
     max1/=10; 
   } 
   return new_num; 
} 

2 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. Turfway - Dei Titanium - TITanium Art
    Turfway titanium nose jewelry - Dei micro touch titanium trim where to buy Titanium - TITanium Art. Design - TITanium Art by blue titanium TOTO. This artwork is currently out of stock and unavailable.Ship Weight: raft titanium 0.0 lbsDimensions: 15" x 15" (5.1") x 3" titanium bracelet (2.5") x 0.5" (0.6")

    ReplyDelete