We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
  • HackerRank Home

    HackerRank

  • |
  • Prepare
  • Certify
  • Compete
  • Hiring developers?
  1. Arrays: Left Rotation
  2. Discussions

Arrays: Left Rotation

Problem
Submissions
Leaderboard
Discussions
Editorial
Topics

Sort 4869 Discussions, By:

recency

Please Login in order to post a comment

  • billones_kenllo1
    3 days ago+ 0 comments

    Simple Java Code

        public static List<Integer> rotLeft(List<Integer> a, int d) {
            List<Integer> returnList = new ArrayList<>();
            int max = a.size();
            for(int i = d; i < max; i++){
                returnList.add(a.get(i));
                if(i + 1 == a.size()){
                    i = -1;
                    max = d;
                }
            }
    
            return returnList;
        }
    
    1|
    Permalink
  • khuongnv
    3 days ago+ 0 comments

    C++

    vector<int> rotLeft(vector<int> a, int d) {
        int n = a.size();
        vector<int> arr(a.size(), 0);
        for(int i = 0; i < n; ++i)
        {
            int index = i - (n - d);
            if(index < 0) index += n;
            arr[i] = a[index];    
        }
        return arr;
    }
    
    0|
    Permalink
  • elina_pavlova_02
    6 days ago+ 0 comments

    C#

    public static List<int> rotLeft(List<int> a, int d)
        {
            var count = a.Count;
            var newA = new int[count];
    
            for (var i = 0; i < count; i++)
            {
                var newIndex = count + i - d;
                newIndex = newIndex >= count 
                    ? newIndex - count 
                    : newIndex;
                
                newA[newIndex] = a[i];
            }
    
            return newA.ToList();
        }
    
    0|
    Permalink
  • devreviewblogs
    6 days ago+ 0 comments

    Here's my solution:

    vector rotateLeft(int d, vector arr) { vector a; int j=0;

    a=arr;
    for(int i=0;i<arr.size();i++)
    {
    
        if((i+d)<(arr.size()))
        {
            arr[i]=a[i+d];
        }
        else{
            arr[i]=a[j];
            j++;
    
        }
    }
    

    return arr; }

    0|
    Permalink
  • zimkhandiaye
    7 days ago+ 0 comments

    Please someone help me

    public static List rotationLeft(List a, int b) { List newList = new ArrayList<>(); int size = a.size(); if(b == size) return a; else if (b > size) b = b -size; else if(b < 0) b = size + b; int [] tab = new int[size]; int [] tab2 = new int[size]; int i = 0; for(int item: a) { tab[i] = item; i++; }

        int j = 0;
        for(int e = size -b ; e < size; e++) {
            tab2[e] = tab[j];
            j++;
        }
        int indice = b;
        for(int n = size -b -1; n >=0; n--){
            tab2[n] = tab[indice];
            indice++;
        }
        tab2[size - b] = tab[0];
        for (int entier: tab2) {
            newList.add(entier);
        }
    
        return newList;
    }
    
    0|
    Permalink
Load more conversations

Need Help?


View editorial
View top submissions
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy