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.
$length=count($input);$result='YES';for($i=0;$i<$length;$i++){if($input[$i]==$i+1){continue;}else{while($input[$i]!==$i+1){$searchElement=$i+1;$index=array_search($searchElement,$input);$diff=$index-$searchElement;if($diff==0){if(!isset($input[$index+1])){$result='NO';break2;// stop the while loop}$first=$input[$index-1];//5$second=$input[$index];//2;$third=$input[$index+1];//6$input[$index]=$third;$input[$index-1]=$second;$input[$index+1]=$first;}else{$first=$input[$index];//2$second=$input[$index-1];//5$third=$input[$index-2];//6$input[$index]=$third;$input[$index-1]=$first;$input[$index-2]=$second;}}}}echo$result;``
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Larry's Array
You are viewing a single comment's thread. Return to all comments →