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.
functionsortBalls($containers){$ballTypes=[];$swaps=0;// Group the balls by typeforeach($containersas$container){$ballType=$container[0];if(!isset($ballTypes[$ballType])){$ballTypes[$ballType]=[];}$ballTypes[$ballType]=array_merge($ballTypes[$ballType],$container);}// Assign the balls back to the containers$index=0;foreach($ballTypesas$type=>$balls){foreach($ballsas$ball){if($containers[$index][0]!=$ball){// Swap the ball to its correct container$swaps++;$temp=$containers[$index][0];$containers[$index][0]=$ball;$containers[$index+1][0]=$temp;}$index++;}}return$swaps;}// Example usage$containers=[[1,1,2,2,3,3],[1,1,2,2,3,3],[1,2,3,1,2,3]];$swaps=sortBalls($containers);echo"Minimum number of swaps required: ".$swaps;
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Organizing Containers of Balls
You are viewing a single comment's thread. Return to all comments →
These sorting are used in CMS especially WordPress . I design a website in WordPress( https://goldrateinfo.ae/سوق-الذهب-الشارقة ) and used it internally in func.php
PHP Solution