Sort by

recency

|

3500 Discussions

|

  • + 0 comments

    Left rotation refers to the process of shifting elements of an array or list to the left by a specified number of positions. cricbet99 betting id

  • + 0 comments

    Here is my c++ solution, you can have the video explanation here : https://youtu.be/lyUDSB4Sg7Y

    vector<int> rotateLeft(int d, vector<int> arr) {
        int n = arr.size();
        vector<int> result(n);
        for(int i = 0; i < n; i++){
            int newIndex = (i - d + n) % n;
            result[newIndex] = arr[i];
        }
        return result;
    }
    
  • + 0 comments

    C w/ memory copy

    int* rotateLeft(int d, int arr_count, int* arr, int* result_count) {
        assert(result_count);
        *result_count = arr_count;
        
        //
        // Use extra memory to rotate an array
        // 1. copy into extra memory [0]..[d]
        // 2. then copy the rest of the array [d]..[end] into the [0] index
        // 3. then copy the extra memory bytes into rest of the main array
        //     starting from [d]
        //
        
        if (!arr_count || arr_count == 1 || !arr) return arr;
    
        int* extra = malloc(sizeof(int) * d);
        assert(extra);
    
        memcpy(extra, arr, d * sizeof(int));
        memcpy(arr, arr + d, sizeof(int) * (arr_count - d));
        memcpy(arr + arr_count - d, extra, d * sizeof(int));
        
        free(extra);
        return arr;
    }
    
  • + 0 comments

    Who designed those API?

    int* rotateLeft(int d, int arr_count, int* arr, int* result_count)
    

    Can anyone explain to me why there is result_count if the size of the array never changes?

  • + 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
    }