• + 0 comments
    $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';
    			           break 2; // 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;``