Home  >  Article  >  Backend Development  >  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++)

WBOY
WBOYforward
2023-08-30 18:17:11999browse

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

    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

    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:
    This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete