Search
 
SCRIPT & CODE EXAMPLE
 

CPP

rotate in cpp

vector<int> v{1,2,3,4,5,6,7,8,9};

int rot = 2;
//rotate left
rotate(v.begin(), v.begin() + rot, v.end()); //vector: 3 4 5 6 7 8 9 1 2
//rotate right
rotate(v.begin(), v.begin()+v.size()-rot, v.end()); //vector: 8 9 1 2 3 4 5 6 7
Comment

rotate in cpp

#include<iostream>
using namespace std;
void leftRotateByOne(int arr[], int n)
{
	int temp = arr[0];
	for (int i = 0; i < n - 1; i++)
	{
		arr[i] = arr[i + 1];
	}
	arr[n - 1] = temp;
}
void leftRotate(int arr[], int d, int n)
{
	for (int i = 0; i < d; i++)
	{
		leftRotateByOne(arr,n);
	}
}
void print(int arr[],int n)
{
	for (int i = 0; i < n; i++)
	{
		cout << arr[i] << " ";
	}
	cout << endl;
}
void main()
{
	int arr[] = { 1,2,3,4,5,6,7 };
	int n = sizeof(arr) / sizeof(arr[0]);
	// function calling.
	leftRotate(arr, 2, n);
	print(arr, n);
}
Comment

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;
 }



Comment

Array Rotate in c++

#include<iostream>
using namespace std;
void leftRotateByOne(int arr[], int n)
{
	int temp = arr[0];
	for (int i = 0; i < n - 1; i++)
	{
		arr[i] = arr[i + 1];
	}
	arr[n - 1] = temp;
}
void leftRotate(int arr[], int d, int n)
{
	for (int i = 0; i < d; i++)
	{
		leftRotateByOne(arr,n);
	}
}
void print(int arr[],int n)
{
	for (int i = 0; i < n; i++)
	{
		cout << arr[i] << " ";
	}
	cout << endl;
}
void main()
{
	int arr[] = { 1,2,3,4,5,6,7 };
	int n = sizeof(arr) / sizeof(arr[0]);
	// function calling.
	leftRotate(arr, 2, n);
	print(arr, n);
}
Comment

PREVIOUS NEXT
Code Example
Cpp :: int main() { 
Cpp :: even and odd sum in c++ 
Cpp :: how to remove a index from a string in cpp 
Cpp :: cpp string slice 
Cpp :: c++ modulo positive 
Cpp :: team fortress 
Cpp :: draw rectangle opencv c++ 
Cpp :: How to create files in C++ 
Cpp :: c++ average 
Cpp :: iterate through map c++ 
Cpp :: how to square a number in c++ 
Cpp :: c++ input 
Cpp :: max in c++ 
Cpp :: #define online judge in cpp 
Cpp :: string length in c++ 
Cpp :: cpp class constructor 
Cpp :: print two dimensional array c++ 
Cpp :: cpp string find all occurence 
Cpp :: c++ string slicing 
Cpp :: all permutations with repetition C++ 
Cpp :: cpp ifdef 
Cpp :: 31. Next Permutation leetcode solution in c++ 
Cpp :: log base 2 in c++ 
Cpp :: c++ function of find maximum value in an array 
Cpp :: variables in c++ 
Cpp :: toString method in c++ using sstream 
Cpp :: fill two dimensional array c++ 
Cpp :: UENUM ue4 
Cpp :: adding variables c++ 
Cpp :: google test assert exception 
ADD CONTENT
Topic
Content
Source link
Name
7+8 =