Sort by

recency

|

3505 Discussions

|

  • + 0 comments

    A JS solution:

    function rotateLeft(d, arr) {
        // Write your code here
        let rotateArr=new Array(arr.length).fill(null);
        //The start index is from where elements are aligned with arr
        let i=(arr.length - (d % arr.length));
        let j=0;
        while(i <= arr.length - 1) {
            rotateArr[i]=arr[j];
            j++;
            i++;
        }
         i=0;
    		 // the end point is where the alignment begins
        while(i<(arr.length - (d % arr.length))) {
              rotateArr[i]=arr[j];
              j++;
              i++;
             
        }
        return rotateArr;
    }
    
  • + 0 comments

    C# solution

        List<int> ans  = new List<int>();
        int start  = d%n ;
        int newSize = n + start ;
        for(int i = start ;i<newSize;i++ )
            ans.Add(arr[i%n]);
                        return ans;
    
  • + 0 comments

    C# solution

    List ans = new List(); int start = d%n ; int newSize = n + start ; for(int i = start ;i

  • + 0 comments

    List list = new ArrayList<>(); while(list.size()=arr.size()){ d=0; } } return list;

  • + 0 comments

    Java

    public static List<Integer> rotateLeft(int d, List<Integer> arr) {
            Queue<Integer> q = new ArrayDeque<>(arr);
    
            while(d > 0){
                int data = q.poll();
                q.offer(data);
                d--;
            }
    
            return new ArrayList<>(q);
        }