Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR CPP

rotate array cpp

###############################
# Rotate array clockwise
###############################

vector<int> kClockwiseRotate(vector<int> a, int k) {
    int n = a.size() - 1;
  
    while (k != 0) {
      int x = a[n];
      
      for (int i = n; i >= 0; i--) {
        a[i + 1] = a[i];
        a[i] = x;
      }
      k--;
    }
    return a;
  }

###############################
# Rotate array anti-clockwise
###############################

vector<int> kAntiClockwiseRotate(vector<int> a, int k) {
    int n = a.size() - 1;
  
    while (k != 0) { 
	 for (int i = 0; i < n; i++) {
      int x = a[i + 1];
      a[i + 1] = a[i];
      a[i] = x;
    }
      k--;
    }
    return a;
 }



Source by codejudge.blogspot.com #
 
PREVIOUS NEXT
Tagged: #rotate #array #cpp
ADD COMMENT
Topic
Name
9+9 =