Sort by

recency

|

3528 Discussions

|

  • + 0 comments

    I have got a very short and efficient code for this. I used Python

    def rotateLeft(d, arr): # Efficient left rotation using slicing return arr[d:] + arr[:d]

  • + 0 comments

    public static void reverse(List arr1) { int left = 0, right = arr1.size() - 1; while (left < right) { int l = arr1.get(left); int r = arr1.get(right); arr1.set(left, r); arr1.set(right, l); left++; right--; } }

    public static List<Integer> rotateLeft(int d, List<Integer> arr) {
        d = d % arr.size();
        if (d == 0)
            return arr;
        else {
            List<Integer> l1 = new ArrayList<>(arr.subList(0, d));
            List<Integer> l2 = new ArrayList<>(arr.subList(d, arr.size()));
            reverse(l1);
            reverse(l2);
            l1.addAll(l2);
            reverse(l1);
            return l1;
        }
    }
    
  • + 0 comments
    function rotateLeft(d: number, arr: number[]): number[] {
        // Write your code here
        /**
         * Well, we could rotate the array `d` number of times,
         * but that seems like wasteful action.
         * 
         * Better to slice the array and move the chunk
         */
        const left: number[] = arr.slice(0, d);
        const right: number[] = arr.slice(d, arr.length);
        return right.concat(left);}
    }
    
  • + 0 comments

    def rotateLeft(d, arr): for i in range(d): arr.append(arr.pop(0)) return arr

  • + 0 comments

    Hey everyone, I was working through the Left Rotation problem, and I found it to be a great exercise for understanding array manipulation. If you're interested in checking out more tools related to data and statistics, I recently came across a cool website that offers a Name Popularity Checker. It's a handy resource to check how popular a name has been over time. Might be fun to try out if you're interested in names and trends!