Sort by

recency

|

3551 Discussions

|

  • + 0 comments

    Data structures are the backbone of efficient programming! They help organize and store data in ways that make problem-solving faster and more effective. Betinexchange247

  • + 0 comments

    With out extra space , use reverse function

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

    import java.io.; import java.util.;

    public class Solution {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
    
        int n = in.nextInt();  // number of elements
        int d = in.nextInt();  // number of left rotations
    
        int[] arr = new int[n];
        for (int i = 0; i < n; i++) {
            arr[i] = in.nextInt();
        }
    
        // Perform left rotations efficiently
        int[] rotated = new int[n];
        for (int i = 0; i < n; i++) {
            rotated[i] = arr[(i + d) % n];
        }
    
        // Print result
        for (int i = 0; i < n; i++) {
            System.out.print(rotated[i] + (i < n - 1 ? " " : ""));
        }
    
        in.close();
    }
    

    }

  • + 0 comments

    Here's a solution in Javascript

    function rotateLeft(d, arr) {
      let arrSlice = arr.slice(0, d);
      let rotateArr = arr.slice(d)
      let finalArr = [...rotateArr, ...arrSlice];
      return finalArr;
    }
    
  • + 0 comments

    Simple solution in JS (not pure though:))

    function rotateLeft(d, arr) {
        // Write your code here
        while(d--) {
            arr.push(arr.shift());
        }
        
        return arr;
    }