Sort by

recency

|

3542 Discussions

|

  • + 0 comments

    vector rotateLeft(int d, vector arr) { int n = arr.size(); if(d>n){ d = d%n; } reverse(arr.begin(),arr.begin()+d); reverse(arr.begin()+d,arr.end()); reverse(arr.begin(),arr.end()); return arr; }

  • + 0 comments

    Java Solution

    int n = arr.size();
    List<Integer> rotated = new ArrayList<>();
    
    for (int i = 0; i < n; i++) {
        int index = (i + d) % n; 
        rotated.add(arr.get(index));
    }
    
    return rotated;
    }
    
  • + 1 comment

    my python sollution

    def rotateLeft(d, arr):
        return arr[d:] + arr[:d]
    
  • + 0 comments

    My solution in Python 3:

    def rotateLeft(d, arr):
        n = len(arr)
        new_arr = []
        for idx in range(n):
            new_arr.append(arr[(idx + d) % n])
        return new_arr
    
  • + 0 comments

    My solution in Python:

    def rotateLeft(d, arr):
        shift_by = d % len(arr)
        result = []
        
        result.extend(arr[shift_by:])
        result.extend(arr[:shift_by])
                
        return result