Reversal algorithm for Array rotation in C++17 Mar 2025 | 4 min read Problem statement: You are given an array, and your task is to rotate the array by one step for an integer number of times. Rotating an array means shifting the first element of the array to the last of the array so that the first element is present in the last position and every other element shifts towards the left. For more clarity, we can understand this problem in this way. First of all, the element in the 0 index is taken, and the element in the first index shifts towards the left, which means it will occupy the index 0. Likewise, the element present in the second index will occupy the first index. In this way, all elements will shift left, and there will be the last index unoccupied, so this index is replaced with the first element in the array, which is initially taken out from the array. This entire operation should be repeated a given number of times. Input format:Array of integers int[] array Number of rotations n Output:Print the array after n rotations Example for illustration:Array = {10, 20, 30, 40, 50, 60, 70, 80} Num = 4; Initial array: {10, 20, 30, 40, 50, 60, 70, 80} After one rotation: {20, 30, 40, 50, 60, 70, 80, 10} After the second rotation: {30, 40, 50, 60, 70, 80, 10, 20} After the third rotation: {40, 50, 60, 70, 80, 10, 20, 30} After the fourth rotation: {50, 60, 70, 80, 10, 20, 30, 40} Example:The solution for the above problem is given below: Output: ![]() Explanation:This program contains two functions: one is the main function, and the other is the function that is used to reverse the array. This program is used for rotating the array. First, the program asks the user to enter the length of the array. After that, it asks the user to enter the elements to store in the array. After entering the elements, it asks for the number of rotations to do on the array. Now, we call the function named reverseFunction, which takes three parameters: that are integer array, the starting index, and the ending index. So, this reverseFunction is used to reverse the given integer array from the starting index to the ending index. The first time, the parameters are array, zero, one less than the number of rotations. Again, this reverseFunction is called with the parameters array, number of rotations, and one less than the size of the array. After that, the final call to the reverseFunction is made with the parameters integer array, zero, and one less than the size of the array. After this step, the array will be rotated, and then the resultant array will be printed. The functionality of the reverseFunction. It takes an array and two indices as input. Using a while loop, it swaps elements at the first and last indices, moving towards the centre. This process effectively reverses the portion of the array between these indices. By swapping elements iteratively until the first and last indices meet, the function achieves a complete reversal. This method, based on simple swapping logic, demonstrates a fundamental technique widely used in computer programming for tasks like array manipulation and rotation. Its efficiency lies in its straightforward approach, making it both intuitive and practical for array reversal operations. Conclusion:In conclusion, the C++ program efficiently rotates an array by a specified number of positions using a reversal algorithm. It provides an intuitive user interface, allowing seamless input and showcasing the rotated array accurately. The code demonstrates a clear understanding of array manipulation techniques, offering a practical solution. Next TopicSort elements by frequency in C++ |
Algorithms that only use integer operations must be used to draw circles without the use of floating-point math. Bresenham's circle drawing algorithm is one of the regularly used algorithms for this purpose. Using solely integer arithmetic, this approach efficiently and effectively creates circles. A version of Bresenham's...
6 min read
C++ is a strong and flexible programming language renowned for its object-oriented features. Encapsulation is one of the core concepts of object-oriented programming (OOP), which enables us to hide the internal features of a class and expose only the necessary functionality to the outside world. To...
5 min read
In this article, you will learn about the with its syntax and examples. What is the std::get_temporary_buffer? The <memory> header in C++ contains the std::get_temporary_buffer function, which is used to get a temporary buffer to hold uninitialized memory for a specific number of objects. When the temporary...
3 min read
A pointer is used to store the address of another variable. In other words, a pointer extracts the information of a resource that is outside the program (heap memory). We use a copy of the resource, and to make a change, it is done in the copied...
4 min read
Merg Sort Algorithm in C++ Merge Sort is a fundamental sorting algorithm that belongs to the divide-and-conquer family of algorithms. It is renowned for its efficiency and reliability and is often used as a benchmark against which other sorting algorithms are measured. The essence of Merge Sort...
14 min read
In this article, we will discuss the C++ program for sentinel linear search with different approaches. But before discussing their implementations, we must know about the sentinel linear search in C++. What is the Sentinel Linear Search? The "sentinel linear search" is a variation of the linear search...
11 min read
The goal is to convert an N-node tree into a bipartite graph by adding as many edges as possible. Remember that self-loops and multiple edges are not permitted, although cycles are. Illustration: Explanation: An edge connecting nodes 3 and 4 can be added to keep the graph bipartite. There can...
3 min read
In C++, if multiple overloaded methods exist in a base class with the same name, programmers can hide them in the derived class by using the "using" declaration. It is known as method hiding. In this article, we will discuss how to hide all overloading methods...
4 min read
Introduction: Binary heap is a fundamental data structure commonly used in computer science for efficient implementation of priority queues. It is a complete binary tree where each node has a value smaller than or equal to its children if it's a min-heap or greater if it's a...
6 min read
partition point() acquires the partition point: Returns an iterator to the first partitioned element in the range [first, last] for which pred(predicate) is false, indicating the partition point of that element. As if partition had been called with the identical inputs, the range's elements must already be...
4 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India