Table of Contents
Solution Method
Example
Output
Explanation of the above code
Conclusion
Home Backend Development C++ Reversal algorithm for array rotation written in C++

Reversal algorithm for array rotation written in C++

Aug 28, 2023 pm 11:13 PM
rotate c programming Reversal Algorithm Array

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

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.

Copy after login

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

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

}

Copy after login

Output

1

3 4 5 6 7 1 2

Copy after login

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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to rotate Word pictures 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 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 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 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 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 use Layui to implement image cropping and rotation functions 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:

How to rotate backpack hero 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 &quot;Settings&quot; icon in the lower right corner of the main interface. 2. Find &quot;Lens Control&quot; in the settings interface, and then you can see that there is an &quot;Automatic Angle Adjustment Option&quot;. 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 &quot;Switch to manual aiming when moving&quot;.

Can css make text rotate? Can css make text rotate? Feb 02, 2023 am 10:01 AM

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.

See all articles