Table of Contents
Let's look at various input and output scenarios for this -
Output
Home Backend Development C++ Rearrange an array so that elements in even positions are larger than elements in odd positions (C++)

Rearrange an array so that elements in even positions are larger than elements in odd positions (C++)

Aug 30, 2023 pm 06:17 PM
array reorder even-odd

Rearrange an array so that elements in even positions are larger than elements in odd positions (C++)

We get an array of integer type containing positive and negative numbers, say, arr[] of any given size. The task is to rearrange the array in such a way that all elements at even positions or indices should be larger than elements at odd positions or indices, and Print the results.

Let's look at various input and output scenarios for this -

Input− int arr[] = {2, 1, 4, 3, 6, 5, 8, 7}

Output− Array before sorting: 2 1 4 3 6 5 8 7 Rearrange the array so that even positions are larger than odd positions: 1 2 3 4 5 6 7 8

Explanation− We get an integer array of size 8, which contains positive and negative elements . Now, we rearrange the array so that all elements in even positions are larger than elements in odd positions. The resulting array is 1 2 3 4 5 6 7 8.

Input− int arr[] = {-3, 2, -4, -1}

Output− Array before arrangement: - 3 2 -4 -1 Rearrange an array so that even positions are larger than odd ones: -4 -3 -1 2

Explanation - We get an integer array of size 8 containing positive and negative elements. Now, we rearrange the array so that all elements at even positions are larger than elements at odd positions. The resulting array after doing this is -4 -3 -1 2.

The following program uses the following method
  • Input an array of integer elements and calculate the size of the array.

    li>
  • Sort an array using the C STL's sort method by passing the array and the size of the array to the sort function.

  • Declare an integer variable and set it by calling the function Rearrangement(arr, size)

  • In the function Rearrangement(arr, size) Inside

      Declare an integer type array, assuming that the size of ptr[size] is the same as the array arr[size]

  • Declare a temporary integer type variable , i.e. the first one is 0 and the last one is of size -1.

  • Loop FOR from i to 0 until i is less than the size of the array. Inside the loop, check IF (i 1) % 2 equals 0, then set ptr[i] to arr[last--].

  • ELSE, set ptr[i ] to arr[first ].

  • Print the result.

  • Example
    #include <bits/stdc++.h>
    using namespace std;
    void Rearrangement(int* arr, int size){
       int ptr[size];
       int first = 0;
       int last = size - 1;
       for (int i = 0; i < size; i++){
          if((i + 1) % 2 == 0){
             ptr[i] = arr[last--];
          }
          else{
             ptr[i] = arr[first++];
          }
       }
    }
    int main(){
       //input an array
       int arr[] = {2, 1, 4, 3, 6, 5, 8, 7};
       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] << " ";
       }
       //sort an Array
       sort(arr, arr + size);
       //calling the function to rearrange the array
       Rearrangement(arr, size);
       //print the array after rearranging the values
       cout<<"\nRearrangement of an array such that even positioned are greater than odd is: ";
       for(int i = 0; i < size; i++){
          cout<< arr[i] << " ";
       }
       return 0;
    }
    Copy after login

    Output

    If we run the above code it will generate the following output

    Array before Arrangement: 2 1 4 3 6 5 8 7
    Rearrangement of an array such that even positioned are greater than odd is: 1 2 3 4 5 6 7 8
    Copy after login

    The above is the detailed content of Rearrange an array so that elements in even positions are larger than elements in odd positions (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

    Video Face Swap

    Video Face Swap

    Swap faces in any video effortlessly with our completely free AI face swap tool!

    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)

    In C language, array post-increment and front-increment In C language, array post-increment and front-increment Aug 30, 2023 pm 04:57 PM

    Question: Use C program to explain the concepts of post-increment and pre-increment of arrays. Solution Increment Operator (++) - There are two types of increment operators used to increase the value of a variable by 1 - pre-increment and post-increment. In prepended increment, the increment operator is placed before the operand, and the value is incremented first and then the operation is performed. eg:z=++a;a=a+1z=a The increment operator is placed after the operand in the post-increment operation, and the value will increase after the operation is completed. eg:z=a++;z=aa=a+1 Let us consider an example of accessing a specific element in a memory location by using pre-increment and post-increment. Declare an array of size 5 and perform compile-time initialization. Afterwards try assigning the pre-increment value to variable 'a'. a=++arr[1]

    Basic operations and usage of arrays in PHP Basic operations and usage of arrays in PHP Jun 28, 2023 pm 08:02 PM

    Basic operations and usage of arrays in PHP 1. Overview Array is a very important data type in PHP. It can be used to store multiple values, and these values ​​can be accessed through indexes or keys. Arrays have rich operations and usage methods in PHP. This article will introduce in detail the basic operations and usage methods of arrays in PHP. 2. Create arrays In PHP, you can create arrays in two ways: countable arrays and associative arrays. Creating a Countable Array A countable array is an array that is arranged in order and indexed numerically

    In Java, how to add new elements to an array? In Java, how to add new elements to an array? Jan 03, 2024 pm 03:30 PM

    Adding new elements to an array is a common operation in Java and can be accomplished using a variety of methods. This article will introduce several common methods of adding elements to an array and provide corresponding code examples. 1. A common way to use a new array is to create a new array, copy the elements of the original array to the new array, and add new elements at the end of the new array. The specific steps are as follows: Create a new array whose size is 1 larger than the original array. This is because a new element is being added. Copy the elements of the original array to the new array. Add to the end of the new array

    Rearrange an array so that arr becomes arr] and only use O(1) extra space, implemented in C++ Rearrange an array so that arr becomes arr] and only use O(1) extra space, implemented in C++ Aug 28, 2023 am 11:53 AM

    We get an array of positive integer type, say, arr[] of any given size, such that the element value in the array should be greater than 0 but less than the size of the array. The task is to rearrange an array only by changing arr[i] to arr[arr[i]] in the given O(1) space and print the final result. Let’s look at various input and output scenarios for this situation − Input − intarr[] = {032154} Output − Array before arrangement: 032154 Rearrange the array so that arr[i] becomes arr[arr[i]], And has O(1) extra space: 012345 Explanation − We are given an integer array of size 6, and all elements in the array have values ​​less than 6. Now we will rearrange

    How to implement stack in Java using arrays and generics? How to implement stack in Java using arrays and generics? Sep 05, 2023 pm 09:25 PM

    Java implements the stack by utilizing arrays and generics. This creates a versatile and reusable data structure that operates on the last-in-first-out (LIFO) principle. Following this principle, elements are added and removed from the top. By utilizing arrays as the basis, it ensures efficient memory allocation and access. Additionally, by incorporating generics, the stack is able to accommodate elements of different types, thereby enhancing its versatility. The implementation involves the definition of a Stack class containing generic type parameters. It includes basic methods such as push(), pop(), peek() and isEmpty(). Handling of edge cases, such as stack overflows and underflows, is also critical to ensure seamless functionality. This implementation enables developers to create programs that accommodate

    Arrays in C/C++? Arrays in C/C++? Sep 20, 2023 pm 08:25 PM

    An array is a sequential collection of elements of the same type. Arrays are used to store collections of data, but it is often more useful to think of arrays as collections of variables of the same type. Instead of declaring a single variable such as number0, number1, ... and number99, you can declare an array variable (e.g. number) and represent it using numbers[0], numbers[1] and ..., numbers[99] each variable. Specific elements in the array are accessed through indexing. All arrays consist of contiguous memory locations. The lowest address corresponds to the first element, and the highest address corresponds to the last element. Declaring an ArrayDeclaring an array requires specifying the type of elements and the number of elements required. An array is as follows -ty

    Rearrange an array so that elements in even positions are larger than elements in odd positions (C++) Rearrange an array so that elements in even positions are larger than elements in odd positions (C++) Aug 30, 2023 pm 06:17 PM

    We get an array of integer type containing positive and negative numbers, say, arr[] of any given size. The task is to rearrange the array in such a way that all elements at even positions or indices should be larger than elements at odd positions or indices and print the result. Let’s look at various input and output scenarios for this - input −intarr[]={2,1,4,3,6,5,8,7} output − array before arrangement: 21436587 Rearrange the array so that the even positions are Greater than odd position: 12345678 Explanation − We get an integer array of size 8 containing positive and negative factors. Now, we rearrange the array so that all elements in even positions are larger than elements in odd positions,

    A deep dive into the capabilities and features of JavaScript's built-in objects A deep dive into the capabilities and features of JavaScript's built-in objects Jan 10, 2024 pm 05:23 PM

    In-depth analysis of the functions and characteristics of JS built-in objects JavaScript is an object-based programming language. It provides many built-in objects with various rich functions and characteristics. In this article, we will provide an in-depth analysis of some commonly used built-in objects and give corresponding code examples. Math object The Math object provides some basic mathematical operation methods, such as exponentiation, square root, logarithm, etc. The following are some commonly used Math object method examples: //Find the absolute value Math.abs(-10

    See all articles