- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Even numbers at even index and odd numbers at odd index in C++

In this problem, we are given an array arr[] of size n consisting of n/2 even values and n/2 odd values. Our task is to create a program to place *even numbers at even index and odd numbers at odd index. *

## Let’s take an example to understand the problem,

**Input: **arr[] = {5, 1, 6, 4, 3, 8}

**Output: **arr[] = {6, 1, 5, 4, 3, 8}

## Solution Approach −

A solution would be traversing the array and then find the end number which is not at even position and replacing it with the next off place value. This is a promising solution but the solution can be made more efficient by using two indexes one for even and one for odd. If there is an element at even index which is not even and an odd element which is not at odd index, we will swap them otherwise increase both indices by two.

## Program to illustrate the working of our solution,

## Example

#include <iostream> using namespace std; void O_EReshuffle(int arr[], int n) { int oIndex = 1; int eIndex = 0; for(int i = 0; i < n; ) { while (eIndex < n && arr[eIndex] % 2 == 0) eIndex += 2; while (oIndex < n && arr[oIndex] % 2 == 1) oIndex += 2; if (eIndex < n && oIndex < n) swap (arr[eIndex], arr[oIndex]); else break; } } int main() { int arr[] = { 5, 1, 6, 4, 3, 8 }; int n = sizeof(arr) / sizeof(arr[0]); cout << "Array before Reshuffling: "; for(int i = 0; i < n ; i++){ cout<<arr[i]<<"\t"; } O_EReshuffle(arr, n); cout<<"\nArray after Reshuffling: "; for(int i = 0; i < n ; i++){ cout<<arr[i]<<"\t"; }; return 0; }

## Output −

Array before Reshuffling: 5 1 6 4 3 8 Array after Reshuffling: 4 1 6 5 8 3

- Related Questions & Answers
- Odd even index difference - JavaScript
- Swap Even Index Elements And Odd Index Elements in Python
- Find even odd index digit difference - JavaScript
- Swapping even and odd index pairs internally in JavaScript
- Adding only odd or even numbers JavaScript
- Largest Even and Odd N-digit numbers in C++
- Array Index with same count of even or odd numbers on both sides in C++
- Positive elements at even and negative at odd positions (Relative order not maintained) in C++
- Python program to Count Even and Odd numbers in a List
- C++ program for the Array Index with same count of even or odd numbers on both sides?
- Count Numbers in Range with difference between Sum of digits at even and odd positions as Prime in C++
- Separate odd and even in JavaScript
- Rearrange array such that even index elements are smaller and odd index elements are greater in C++
- C program to store even, odd and prime numbers into separate files
- Check whether product of digits at even places is divisible by sum of digits at odd place of a numbers in Python

Advertisements