Home > Backend Development > C++ > body text

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

WBOY
Release: 2023-08-30 18:17:11
forward
1059 people have browsed it

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!

    Related labels:
    source:tutorialspoint.com
    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
    Popular Tutorials
    More>
    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template