Here is my solution in R

FindSmallestAdjacentReplacement=function(number) { Min=number Digits=(strtoi(unlist(strsplit(toString(number),"")))) for(i in 2:length(Digits)) { BEG="" TAIL="" MIDDLE=max(Digits[i-1],Digits[i]) if(i<length(Digits)) TAIL=(paste(Digits[(i+1):length(Digits)],collapse="")) if(i>2) { BEG=paste(Digits[1:(i-2)],collapse="") } NUM=strtoi(paste(BEG,MIDDLE,TAIL,sep="")) if(NUM < Min) { Min=NUM } } return(Min) } print(FindSmallestAdjacentReplacement(987654321))category tags

company tags

Interview Questions