
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
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 Articles
- Odd even index difference - JavaScript
- Swap Even Index Elements And Odd Index Elements in Python
- What are even and odd numbers?
- Find even odd index digit difference - JavaScript
- Swapping even and odd index pairs internally in JavaScript
- Array Index with same count of even or odd numbers on both sides in C++
- Adding only odd or even numbers JavaScript
- Largest Even and Odd N-digit numbers in C++
- C++ program for the Array Index with same count of even or odd numbers on both sides?
- How To Do Divisibility In Odd Or Even Numbers
- State whether the following statements are True or False:(a) The sum of three odd numbers is even.(b) The sum of two odd numbers and one even number is even.(c) The product of three odd numbers is odd.(d) If an even number is divided by 2, the quotient is always odd.(e) All prime numbers are odd.(f) Prime numbers do not have any factors.(g) Sum of two prime numbers is always even.(h) 2 is the only even prime number.(i) All even numbers are composite numbers.(j) The product of two even numbers is always even.
- Rearrange array such that even index elements are smaller and odd index elements are greater in C++
- Python program to Count Even and Odd numbers in a List
- The sum of two prime numbers $(>2)$ is(A) odd (B) even (C) prime (D) even or odd
- Replace Odd Numbers with Square root & Even Numbers with Square in Java

Advertisements