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 :: c++ replace character in string 
Cpp :: call of overloaded ‘swap(int&, int&)’ is ambiguous 
Cpp :: c++ unary minus overload 
Cpp :: taking user input for a vector in c++ 
Cpp :: c++ stream string into fiel 
Cpp :: 2d vector initialization in cpp 
Cpp :: c++ remove whitespace from string 
Cpp :: check variable type c++ 
Cpp :: std cout c++ 
Cpp :: prime number program in c c++ 
Cpp :: how to hide ui elements unity 
Cpp :: c++ vector add only unique elements 
Cpp :: Write C++ program to copy one string to another string using pointers 
Cpp :: print linked list recursively c++ 
Cpp :: minimum and maximum value of a vector in C++ 
Cpp :: extends c++ 
Cpp :: format string cpp 
Cpp :: c++ string remove first character 
Cpp :: sum of vector elements c++ 
Cpp :: how to convert int to string c++ 
Cpp :: queue in c++ 
Cpp :: how to add colored text in c++ 
Cpp :: how to convert int to std::string 
Cpp :: how to make calculaor in c++ 
Cpp :: int_max cpp 
Cpp :: terminal compile c++ 
Cpp :: sieve of eratosthenes algorithm in c++ 
Cpp :: c++ programming language 
Cpp :: how to rotate canvas android 
Cpp :: append string cpp 
ADD CONTENT
Topic
Content
Source link
Name
3+6 =