


Rearrange an array so that every odd-indexed element is greater than its previous element
We have an array of positive integer type, assumed to be arr[], with any size. The task is to rearrange the array so that all elements at odd index positions have greater values than elements at even index positions, and print the result.
Let us look at various input and output situations:
Input − int arr[] = {2, 1, 5, 4, 3, 7, 8}
Output − Array before sorting: 2 1 5 4 3 7 8 Rearrange the array so that every odd-indexed element is greater than its previous element: 1 4 2 5 3 8 7
Explanation - We are given an array of integers of size 7 . Now, if the element at even index is larger, we will swap the element at even index and the element at odd index
Arr[0] > arr[1] = call swap = {1, 2, 5, 4, 3, 7, 8} Arr[2] > arr[3] = call swap = {1, 2, 4, 5, 3, 7, 8} Arr[6] > arr[5] = call swap = {1, 2, 4, 5, 3, 8, 7} Arr[2] > arr[1] = call swap = {1, 4, 2, 5, 3, 8, 7}
input− int arr[] = {3, 2, 6, 9}
Output− Array before sorting: 3 2 6 9 Rearrangement of an array such that every odd indexed element is greater than it previous is: 2 3 6 9
Explanation − we are given an integer array of size 4. Now, we will swap the elements at even index with the elements at odd index if even indexed elements are greater i.e. Arr[0] > arr[1] = call swap = {2, 3, 6, 9}. No need to further call the swap method as all the elements at positions satisfies the conditions
Approach used in the below program is as follows
Input an array of integer type elements and calculate the size of an array .
- ##Print the array before arrangement and call the function Rearrangement(arr, size) ##Inside the function Rearrangement(arr, size)
- Create a variable of integer type let's say, ptr and set it with size-1.
- ##Start loop FOR, from i to 0 till i less than ptr and i = i 1. Inside the loop, check if arr[i] is greater than arr[i 1] then call swap(arr[i], arr[i 1]).
- Check IF size & 1 then start loop FOR from i to ptr till i greater than 0 and i = i - 2. Inside the loop, check IF arr[i] greater than arr[i - 1] then call swap(arr[i], arr[i-1])
- Example
#include <iostream> using namespace std; void Rearrangement(int arr[], int size){ int ptr = size - 1; for(int i = 0; i < ptr; i = i+2){ if(arr[i] > arr[i+1]){ swap(arr[i], arr[i+1]); } } if(size & 1){ for(int i = ptr; i > 0; i = i-2){ if(arr[i] > arr[i-1]){ swap(arr[i], arr[i-1]); } } } } int main(){ //input an array int arr[] = {2, 1, 5, 4, 3, 7, 8}; int size = sizeof(arr) / sizeof(arr[0]); //print the original Array cout<<"Array before Arrangement: "; for (int i = 0; i < size; i++){ cout << arr[i] << " "; } //calling the function to rearrange the array Rearrangement(arr, size); //print the array after rearranging the values cout<<"\nRearrangement of an array such that every odd indexed element is greater than it previous is: "; for(int i = 0; i < size; i++){ cout<< arr[i] << " "; } return 0; }
Array before Arrangement: 2 1 5 4 3 7 8
Rearrangement of an array such that every odd indexed element is greater than it previous is: 1 4 2 5 3 8 7
Copy after login
Array before Arrangement: 2 1 5 4 3 7 8 Rearrangement of an array such that every odd indexed element is greater than it previous is: 1 4 2 5 3 8 7
The above is the detailed content of Rearrange an array so that every odd-indexed element is greater than its previous element. 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



An array is a linear sequential data structure used to hold homogeneous data in contiguous memory locations. Like other data structures, arrays must have the ability to insert, delete, traverse, and update elements in some efficient way. In C++, our arrays are static. C++ also provides some dynamic array structures. For a static array, Z elements may be stored in the array. So far we have n elements. In this article, we will learn how to insert elements at the end of an array (also known as appending elements) in C++. Understand the concept through examples. The usage of ‘this’ keyword is as follows: GivenarrayA=[10,14,65,85,96,12,35,74,69]Afterin

CSS transformation: How to achieve the rotation effect of elements requires specific code examples. In web design, animation effects are one of the important ways to improve user experience and attract user attention, and rotation animation is one of the more classic ones. In CSS, you can use the "transform" attribute to achieve various deformation effects of elements, including rotation. This article will introduce in detail how to use CSS "transform" to achieve the rotation effect of elements, and provide specific code examples. 1. How to use CSS’s “transf

CSS transition effect: How to achieve the sliding effect of elements Introduction: In web design, the dynamic effect of elements can improve the user experience, among which the sliding effect is a common and popular transition effect. Through the transition property of CSS, we can easily achieve the sliding animation effect of elements. This article will introduce how to use CSS transition properties to achieve the sliding effect of elements, and provide specific code examples to help readers better understand and apply. 1. Introduction to CSS transition attribute transition CSS transition attribute tra

The javafx.scene.shape package provides some classes with which you can draw various 2D shapes, but these are just primitive shapes like lines, circles, polygons and ellipses etc... So if you want to draw complex For custom shapes, you need to use the Path class. Path class Path class You can draw custom paths using this geometric outline that represents a shape. To draw custom paths, JavaFX provides various path elements, all of which are available as classes in the javafx.scene.shape package. LineTo - This class represents the path element line. It helps you draw a straight line from the current coordinates to the specified (new) coordinates. HlineTo - This is the table

In this article, we will discuss how to modify a given string in C++ by rearranging the vowels alphabetically at their respective indices. We will also explain the methods used to solve this problem and provide examples with test cases. Problem Statement Given a string, rearrange the vowels at their respective indices in alphabetical order. Consonants in the string should maintain their original order. For example, given the string "tutorialspoint", the output should be "tatiriolspount". Method This problem can be solved using a simple algorithm. We can first create a separate string containing all the vowels in the given string in their respective order. We can then sort that string alphabetically. at last,

Elements that HTML5 does not support are purely expressive elements, frame-based elements, application elements, replaceable elements and old form elements. Detailed introduction: 1. Purely expressive elements, such as font, center, s, u, etc., these elements are usually used to control text style and layout; 2. Frame-based elements, such as frame, frameset and noframes, these elements are used in In the past, it was used to create web page layouts and split windows; 3. Application-related elements, such as applet, isinde, etc.

How to use HTML and CSS to implement a layout with a fixed navigation menu. In modern web design, fixed navigation menus are one of the common layouts. It can keep the navigation menu always at the top or side of the page, allowing users to browse web content conveniently. This article will introduce how to use HTML and CSS to implement a layout with a fixed navigation menu, and provide specific code examples. First, you need to create an HTML structure to present the content of the web page and the navigation menu. Here is a simple example

How to use CSS to achieve the transparency gradient effect of elements In web development, adding transition effects to web page elements is one of the important means to improve user experience. The gradient effect of transparency can not only make the page smoother, but also highlight the key content of the element. This article will introduce how to use CSS to achieve the transparency gradient effect of elements and provide specific code examples. Using the CSS transition attribute To achieve the transparency gradient effect of an element, we need to use the CSS transition attribute. t
