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 −
1 2 3 |
|
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
|
Output
1 |
|
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



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

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,

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 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 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:

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".

CSS can rotate text. The method to achieve text rotation is: 1. Create a new HTML file; 2. Use the span tag to create a line of text; 3. Add a class attribute to the span tag for style definition; 4. In the css tag Inside, define the style of the span tag through class and set it as a block-level element; 5. Use the transform attribute and use rotate to set the span rotation to a specified angle.
