Reversal algorithm for array rotation written in C++

In the given problem, we have an array and we need to rotate the array by d elements using inversion algorithm, for example −
Input : arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2 Output : arr[] = [3, 4, 5, 6, 7, 1, 2] Explanation : As you can see we have to rotate this array by d = 2 but our main task is to achieve this by using a reversal technique.
We have an array The rotation was calculated with some inversion techniques and came to the conclusion:
- First, we invert the first d elements of the array.
- Second, we reverse the remaining elements.
- Third, we reverse the entire array.
By applying these three steps, we can get the rotated array.
Solution Method
In this problem, first, we are going to write a function that reverses the elements; now we follow the above steps.
Example
#include <bits/stdc++.h>
using namespace std;
void reverseArray(int arr[], int start, int end) { // our reversal algorithm
while (start < end) { // if start becomes equal to end we break the loop
int temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
start++;
end--;
}
return ;
}
void Rotate(int arr[], int d, int n) { // rotation function
if (d == 0) // no rotation required
return;
d = d % n; // when d becomes equal to n so our array comes to its original form
reverseArray(arr, 0, d - 1); // reversing first d elements
reverseArray(arr, d, n - 1); // reversing the remaining elements
reverseArray(arr, 0, n - 1); // reversing the whole array
return ;
}
int main() {
int arr[] = { 1, 2, 3, 4, 5, 6, 7 }; // given array
int n = sizeof(arr) / sizeof(arr[0]); // size of our array
int d = 2;
Rotate(arr, d, n);
for(int i = 0; i < n; i++) // printing the array
cout << arr[i] << " ";
cout << "\n";
return 0;
}Output
3 4 5 6 7 1 2
Explanation of the above code
In the above method, we first create an inversion technique that will accept three parameters, namely the array, starting index and ending index, and reverses our array from the starting position to the ending position. Since we have developed the algorithm previously, we will use this function to apply the algorithm. First, we reverse the first d elements. Then, we reverse the remaining elements, and finally, we reverse the entire array. As a result, our array is rotated by d positions. In the rotation function, we set d to d % n. This is because if we rotate the first n elements of the array, we will get the same answer as before, so we take d modulo n.
Conclusion
In this article, we solved a problem of applying the inversion algorithm for array rotation. We also learned the C program and the complete (normal) method to solve this problem. We can write the same program in other languages like C, Java, Python and others. Hope this article is helpful to you.
The above is the detailed content of Reversal algorithm for array rotation written in C++. For more information, please follow other related articles on the PHP Chinese website!
Hot AI Tools
Undresser.AI Undress
AI-powered app for creating realistic nude photos
AI Clothes Remover
Online AI tool for removing clothes from photos.
Undress AI Tool
Undress images for free
Clothoff.io
AI clothes remover
AI Hentai Generator
Generate AI Hentai for free.
Hot Article
Hot Tools
Notepad++7.3.1
Easy-to-use and free code editor
SublimeText3 Chinese version
Chinese version, very easy to use
Zend Studio 13.0.1
Powerful PHP integrated development environment
Dreamweaver CS6
Visual web development tools
SublimeText3 Mac version
God-level code editing software (SublimeText3)
Hot Topics
1378
52
How to rotate Word pictures
Mar 19, 2024 pm 06:16 PM
When we use Word office software for document processing, we often need to insert some pictures and other materials into the document. However, in order to achieve beautiful layout, we also need to perform some special layout on the pictures, among which rotation processing is the most basic. Typesetting processing, however, for some newcomers to the workplace who have just come into contact with Word office software, they may not be able to process pictures in Word documents. Below, we will share how to rotate pictures in Word. We hope it will be helpful and inspiring to you. 1. First, we open a Word document, and then click the Insert-Picture button on the menu bar to insert a random picture on the computer to facilitate our operation and demonstration. 2. If we want to rotate the image, then we need to
How to use Vue to achieve 3D stereoscopic rotation effects
Sep 19, 2023 am 08:42 AM
How to use Vue to achieve 3D three-dimensional rotation effects As a popular front-end framework, Vue.js plays an important role in developing dynamic web pages and applications. It provides an intuitive, efficient way to build interactive interfaces and is easy to integrate and extend. This article will introduce how to use Vue.js to implement a stunning 3D stereoscopic rotation effect, and provide specific code examples. Before you start, make sure you have Vue.js installed and have some understanding of the basic usage of Vue.js. If you still
Use C++ to write code to find the Nth non-square number
Aug 30, 2023 pm 10:41 PM
We all know numbers that are not the square of any number, such as 2, 3, 5, 7, 8, etc. There are N non-square numbers, and it is impossible to know every number. So, in this article, we will explain everything about squareless or non-square numbers and ways to find the Nth non-square number in C++. Nth non-square number If a number is the square of an integer, then the number is called a perfect square. Some examples of perfect square numbers are -1issquareof14issquareof29issquareof316issquareof425issquareof5 If a number is not the square of any integer, then the number is called non-square. For example, the first 15 non-square numbers are -2,3,5,6,
Tips and methods for implementing loading animation effects with CSS
Oct 19, 2023 am 10:42 AM
CSS techniques and methods for implementing loading animation effects With the development of the Internet, loading speed has become one of the important indicators of user experience. In order to improve the user experience when the page is loading, we usually use loading animation effects to increase the interactivity and attractiveness of the page. As one of the important technologies in front-end development, CSS provides many techniques and methods to achieve loading animation effects. This article will introduce several common techniques and methods for implementing CSS loading animation effects, and provide corresponding code examples. Spin loading animation Spin loading animation is a
How to scale and rotate images using Python
Aug 17, 2023 pm 10:52 PM
How to use Python to scale and rotate images Introduction: Today, we often use images to enrich our web design, mobile applications, social media and other scenarios. In image processing, scaling and rotation are two common requirements. Python, as a scripting language and powerful image processing tool, provides many libraries and methods to handle these tasks. This article will introduce how to use Python to scale and rotate images, and provide code examples. 1. Zooming pictures Zooming pictures is one of the basic operations of adjusting image size.
How to rotate backpack hero
Mar 15, 2024 pm 03:30 PM
Players can rotate their field of view when playing games in Backpack Hero. Many players do not know how to rotate Backpack Hero. Players can click on the settings icon in the lower right corner of the main interface to find the lens control, and turn on the automatic perspective adjustment option to adjust it. How to rotate Backpack Hero 1. After entering the game, click the "Settings" icon in the lower right corner of the main interface. 2. Find "Lens Control" in the settings interface, and then you can see that there is an "Automatic Angle Adjustment Option". 3. Turn on this switch to realize automatic rotation of the field of view. When you are in combat or operating complex operations, you can turn on this function to facilitate observation of the battle situation. 4. If you want to perform rotation operations while moving, you can also do so. Just find the gyroscope in the settings and check "Switch to manual aiming when moving".
How to use Layui to implement image cropping and rotation functions
Oct 26, 2023 am 09:39 AM
How to use Layui to implement image cropping and rotating functions 1. Background introduction In web development, we often encounter scenarios that require cropping and rotating images, such as avatar uploading, image editing, etc. Layui is a lightweight front-end framework that provides rich UI components and friendly APIs, and is especially suitable for quickly building web applications. This article will introduce how to use Layui to implement image cropping and rotation functions, and provide specific code examples. 2. Environment preparation Before starting, you need to confirm that the following environment is ready:
In C programming, find the area of a circle
Aug 25, 2023 pm 10:57 PM
A circle is a closed figure. All points on a circle are equidistant from a point inside the circle. The center point is called the center of the circle. The distance from a point to the center of a circle is called the radius. Area is a quantitative representation of the span of dimensions of a closed figure. The area of a circle is the area enclosed within the dimensions of the circle. The formula to calculate the area of a circle, Area=π*r*r To calculate the area, we give the radius of the circle as input, we will use the formula to calculate the area, algorithm STEP1: Takeradiusasinputfromtheuserusingstdinput.STEP2: Calculatetheareaofcircleusing, area=(


