Rearrange Even and Odd Index Elements in K Operations
Last Updated :
15 Feb, 2024
Given an array arr[] of size N (N is even) and a positive integer K, the task is to apply an operation to the array arr[] for K times. Applying the operation once will shift all the even indexed numbers to the beginning and all the odd indexed numbers to the end, maintaining their relative order.
Examples:
Input: K = 1, N = 6, arr[] = {1, 2, 3, 4, 5, 6}
Output: {1, 3, 5, 2, 4, 6}
Explanation: Applying the operation once would be equal to arr'[] = {arr[0], arr[2], arr[4], arr[1], arr[3], arr[5]} = {1, 3, 5, 2, 4, 6}
Input: K = 2, N = 10, arr[]= {20, 123, 23, 21, 33, 13, 55, 65, 90, 100}
Output: {20, 33, 90, 21, 65, 23, 55, 123, 13, 100}
Explanation: Applying the operation once would be equal to arr'[] = {arr[0], arr[2], arr[4], arr[6], arr[8], arr[1], arr[3], arr[5], arr[7], arr[9]} = {20, 23, 33, 55, 90, 123, 21, 13, 65, 100} and applying it again would be equal to arr”[] = {20, 33, 90, 21, 65, 23, 55, 123, 13, 100}
Approach: The problem can be solved using the following approach:
If we observe carefully, shifting all the even indexed numbers to the beginning and odd indexed integers to the end is same as applying a permutation P to S where P = [0, 2, 4, 6, … N-2, 1, 3, 5, …N-1]. So we can get our answer by simply applying the permutation P to S for K times. This can be simply done in log2(K) time using Binary Exponentiation.
Step-by-step approach:
- Declare a permutation P = [0, 2, 4, 6, … N-2, 1, 3, 5, …N-1].
- Initialise a function apply(S, P) which applies the permutation P to the array arr[], and stores it back to arr[].
- Use Binary exponentiation to reduce the number of operations to log2K time.
- If K is odd, apply permutation P to array arr[], apply(arr, P)
- Then apply permutation P to itself, apply(P, P)
- Reduce K to half
- Keep doing the above steps till K is greater than 0
- Return the answer as arr[].
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
#define ll long long int
using namespace std;
void apply(vector<ll>& arr, vector<ll>& P)
{
vector<ll> temp(arr.size());
for ( int i = 1; i < arr.size(); i++) {
temp[i] = arr[P[i]];
}
for ( int i = 1; i < arr.size(); i++)
arr[i] = temp[i];
}
void solve(ll N, ll K, vector<ll>& arr)
{
vector<ll> P;
for ( int i = 0; i < N; i += 2)
P.push_back(i);
for ( int i = 1; i < N; i += 2)
P.push_back(i);
while (K) {
if (K & 1)
apply(arr, P);
apply(P, P);
K >>= 1;
}
for ( int i = 0; i < N; i++)
cout << arr[i] << " ";
cout << "\n";
}
int main()
{
ll N = 10, K = 2;
vector<ll> arr
= { 20, 123, 23, 21, 33, 13, 55, 65, 90, 100 };
solve(N, K, arr);
return 0;
}
|
Java
import java.util.ArrayList;
public class Main {
static void apply(ArrayList<Long> arr, ArrayList<Long> P) {
ArrayList<Long> temp = new ArrayList<>(arr.size());
for ( int i = 0 ; i < arr.size(); i++) {
temp.add(arr.get(( int ) ( long ) P.get(i)));
}
for ( int i = 0 ; i < arr.size(); i++) {
arr.set(i, temp.get(i));
}
}
static void solve( long N, long K, ArrayList<Long> arr) {
ArrayList<Long> P = new ArrayList<>();
for ( int i = 0 ; i < N; i += 2 ) {
P.add(( long ) i);
}
for ( int i = 1 ; i < N; i += 2 ) {
P.add(( long ) i);
}
while (K > 0 ) {
if ((K & 1 ) == 1 ) {
apply(arr, P);
}
apply(P, P);
K >>= 1 ;
}
for ( int i = 0 ; i < N; i++) {
System.out.print(arr.get(i) + " " );
}
System.out.println();
}
public static void main(String[] args) {
long N = 10 , K = 2 ;
ArrayList<Long> arr = new ArrayList<>();
arr.add(20L);
arr.add(123L);
arr.add(23L);
arr.add(21L);
arr.add(33L);
arr.add(13L);
arr.add(55L);
arr.add(65L);
arr.add(90L);
arr.add(100L);
solve(N, K, arr);
}
}
|
Python3
def apply (arr, P):
temp = [ 0 ] * len (arr)
for i in range ( 1 , len (arr)):
temp[i] = arr[P[i]]
for i in range ( 1 , len (arr)):
arr[i] = temp[i]
def solve(N, K, arr):
P = []
for i in range ( 0 , N, 2 ):
P.append(i)
for i in range ( 1 , N, 2 ):
P.append(i)
while K:
if K & 1 :
apply (arr, P)
apply (P, P)
K >> = 1
print ( * arr)
if __name__ = = "__main__" :
N = 10
K = 2
arr = [ 20 , 123 , 23 , 21 , 33 , 13 , 55 , 65 , 90 , 100 ]
solve(N, K, arr)
|
C#
using System;
using System.Collections.Generic;
public class Program
{
private static void Apply(List< long > arr, List< long > P)
{
List< long > temp = new List< long >(arr.Count);
for ( int i = 0; i < arr.Count; i++)
{
temp.Add(arr[( int )P[i]]);
}
for ( int i = 0; i < arr.Count; i++)
{
arr[i] = temp[i];
}
}
private static void Solve( long N, long K, List< long > arr)
{
List< long > P = new List< long >();
for ( int i = 0; i < N; i += 2)
{
P.Add(i);
}
for ( int i = 1; i < N; i += 2)
{
P.Add(i);
}
while (K > 0)
{
if ((K & 1) == 1)
{
Apply(arr, P);
}
Apply(P, P);
K >>= 1;
}
foreach ( long num in arr)
{
Console.Write(num + " " );
}
Console.WriteLine();
}
public static void Main( string [] args)
{
long N = 10, K = 2;
List< long > arr = new List< long > { 20, 123, 23, 21, 33, 13, 55, 65, 90, 100 };
Solve(N, K, arr);
}
}
|
Javascript
function apply(arr, P) {
const temp = Array(arr.length).fill(0);
for (let i = 1; i < arr.length; i++) {
temp[i] = arr[P[i]];
}
for (let i = 1; i < arr.length; i++) {
arr[i] = temp[i];
}
}
function solve(N, K, arr) {
const P = [];
for (let i = 0; i < N; i += 2) {
P.push(i);
}
for (let i = 1; i < N; i += 2) {
P.push(i);
}
while (K) {
if (K & 1) {
apply(arr, P);
}
apply(P, P);
K >>= 1;
}
console.log(...arr);
}
const N = 10;
const K = 2;
const arr = [20, 123, 23, 21, 33, 13, 55, 65, 90, 100];
solve(N, K, arr);
|
Output
20 33 90 21 65 23 55 123 13 100
Time Complexity: O(N * log2K) where N is the length of the input sequence S and K is the number of operations to be performed
Auxiliary Space: O(N) to store the permutation
Share your thoughts in the comments
Please Login to comment...