You are viewing a single comment's thread. Return to all comments →
//Java8 // Write your code here int indx=0; Integer tempData; while (k>0) { if(indx>=arr.size()-1){ break; } if(arr.size()!=0 && arr.size()>2){ Integer max = arr.stream().skip(indx).max(Integer::compare).get(); tempData = arr.get(indx); int mxindx = arr.indexOf(max); if(mxindx==indx){ indx++; }else{ arr.set(indx,max); arr.set(mxindx,tempData); indx++; k--; } }else if(arr.size()==1){ break;} else if (arr.size()==2){ Boolean flag = arr.get(0)>arr.get(1)?true:false; if(!flag){ int a = arr.get(0); int b = arr.get(1); arr.set(0, b); arr.set(1, a); k--; } break; } }
return arr;
Seems like cookies are disabled on this browser, please enable them to open this website
Largest Permutation
You are viewing a single comment's thread. Return to all comments →
//Java8 // Write your code here int indx=0; Integer tempData; while (k>0) { if(indx>=arr.size()-1){ break; } if(arr.size()!=0 && arr.size()>2){ Integer max = arr.stream().skip(indx).max(Integer::compare).get(); tempData = arr.get(indx); int mxindx = arr.indexOf(max); if(mxindx==indx){ indx++; }else{ arr.set(indx,max); arr.set(mxindx,tempData); indx++; k--; } }else if(arr.size()==1){ break;} else if (arr.size()==2){ Boolean flag = arr.get(0)>arr.get(1)?true:false; if(!flag){ int a = arr.get(0); int b = arr.get(1); arr.set(0, b); arr.set(1, a); k--; } break; } }