We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Explanation : If you look in terms on just numbers lets say 2314 (bcad thinking in terms of alphabets or ascii can be tricky)
the immideate next of this is 2341 bcda so we traverse from the last till we find an element which is greater than the previous element 23[1]4 here its one then we have to swap with the element next to it
if we swap these elements we somewhat have the solution but
if its 1432 the immideate next is 2134 which we get by, first as explained above selecting the element which have the next element greater than it [1]432 and after we do swapping. so we will get the result as 4132 but its not correct right because to find the immideate next we need to swap the selected element with the closest element from the rest of the array hence a second for loop. to the the elemenets that needs to be sorted will become [1] 4 3 [2] for the selected element 1 the immideate next in the remaning array is 2. so we will get the output 2431 but one last hurdle is after swapping
the elements after the swapped element need to be sorted 2[431]
431 as thats how we will get the nearest next so i will become 2134
I would be happy if you upvoted my effort explaning
defbiggerIsGreater(w):w_list=list(w)swap_by=""swap_loc=0if(w.count(w[0])==len(w_list)):return"no answer"foriinrange(len(w_list)-1,0,-1):if(w_list[i-1]<w_list[i]):swap_pos=i-1swap_ele=w_list[i-1]#pick should be nearest greater thandistance=ord(min(w))+ord(max(w))forjinrange(swap_pos+1,len(w_list)):if(w_list[j]>swap_ele):#we need to find the nearest nextif(distance>ord(w_list[j])-ord(swap_ele)):distance=ord(w_list[j])-ord(swap_ele)swap_by=w_list[j]swap_loc=j#after the completion of this loop we will have the nearest #next elementtemp=w_list[swap_loc]w_list[swap_loc]=w_list[swap_pos]w_list[swap_pos]=temp#fromswap_pos+1tilllen(w_list)sortandreplacetheorginallistsecond_half=sorted(w_list[swap_pos+1:len(w_list)])first_half=''.join(w_list[:swap_pos+1])first_half+=''.join(second_half)return(first_half)#print( string[swap_pos+1:len(w_list)].sort() )elif(i-1==0):return"no answer"
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Bigger is Greater
You are viewing a single comment's thread. Return to all comments →
Explanation : If you look in terms on just numbers lets say 2314 (bcad thinking in terms of alphabets or ascii can be tricky) the immideate next of this is 2341 bcda so we traverse from the last till we find an element which is greater than the previous element 23[1]4 here its one then we have to swap with the element next to it if we swap these elements we somewhat have the solution but
if its 1432 the immideate next is 2134 which we get by, first as explained above selecting the element which have the next element greater than it [1]432 and after we do swapping. so we will get the result as 4132 but its not correct right because to find the immideate next we need to swap the selected element with the closest element from the rest of the array hence a second for loop. to the the elemenets that needs to be sorted will become [1] 4 3 [2] for the selected element 1 the immideate next in the remaning array is 2. so we will get the output 2431 but one last hurdle is after swapping the elements after the swapped element need to be sorted 2[431] 431 as thats how we will get the nearest next so i will become 2134
I would be happy if you upvoted my effort explaning