• + 0 comments

    Hello , leaving here my soluton with typescript just for compare reasons

    function rotateLeftIndex(index: number,numberOfRotations:number ,arr: number[]): number{
    
       let rotatedIndex = index - numberOfRotations;
    
       if(rotatedIndex < 0){
        rotatedIndex =  arr.length + rotatedIndex;// significa que deu a volta
       }
        return rotatedIndex;
    }
    
    function rotateLeft(d: number, arr: number[]): number[] {
        const hits = new Map<number,number>()    
       
        for(let i = 0; i < arr.length ; i++){
            const rotatedIndex = rotateLeftIndex(i,d,arr);
    
            hits.set(rotatedIndex,arr[rotatedIndex]);
    
            const storedValue = hits.get(i); 
    
            if(storedValue){
               arr[rotatedIndex] = storedValue;
               hits.delete(i);
               continue;
            }
    
            arr[rotatedIndex] = arr[i]; 
    
        }
       
        return arr
    }