Print array elements in alternatively increasing and decreasing order
Last Updated :
04 Nov, 2023
Given an array of N elements. The task is to print the array elements in such a way that first two elements are in increasing order, next 3 in decreasing order, next 4 in increasing order and so on.
Examples:
Input : arr = {2, 6, 2, 4, 0, 1, 4, 8, 2, 0, 0, 5,2,2}
Output : 0 0 8 6 5 0 1 2 2 4 4 2 2 2
Input : arr = {1, 2, 3, 4, 5, 6}
Output : 1 2 6 5 4 3
Source :Oracle Interview experience set 52
The idea is to use 2 pointer technique. First sort the array in increasing order and maintain two pointers and where is to print the array in increasing order and to print the array in decreasing order. Keep a variable to specify the number of elements to be printed in an iteration and a variable flag to switch between printing in increasing order and decreasing order alternatively.
Below is the implementation of above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void printArray( int arr[], int n)
{
sort(arr, arr + n);
int l = 0, r = n - 1, flag = 0, i;
int k = 2;
while (l <= r) {
if (flag == 0) {
for (i = l; i < l + k && i <= r; i++)
cout << arr[i] << " " ;
flag = 1;
l = i;
}
else
{
for (i = r; i > r - k && i >= l; i--)
cout << arr[i] << " " ;
flag = 0;
r = i;
}
k++;
}
}
int main()
{
int n = 6;
int arr[] = { 1, 2, 3, 4, 5, 6 };
printArray(arr, n);
return 0;
}
|
Java
import java.util.*;
class Solution
{
static void printArray( int arr[], int n)
{
Arrays.sort(arr);
int l = 0 , r = n - 1 , flag = 0 , i;
int k = 2 ;
while (l <= r) {
if (flag == 0 ) {
for (i = l; i < l + k && i <= r; i++)
System.out.print(arr[i] + " " );
flag = 1 ;
l = i;
}
else
{
for (i = r; i > r - k && i >= l; i--)
System.out.print(arr[i] + " " );
flag = 0 ;
r = i;
}
k++;
}
}
public static void main(String args[])
{
int n = 6 ;
int arr[] = { 1 , 2 , 3 , 4 , 5 , 6 };
printArray(arr, n);
}
}
|
Python3
def printArray(arr, n):
arr.sort()
l = 0
r = n - 1
flag = 0
k = 2
while (l < = r) :
if (flag = = 0 ):
i = l
while i < l + k and i < = r:
print (arr[i], end = " " )
i + = 1
flag = 1
l = i
else :
i = r
while i > r - k and i > = l:
print (arr[i], end = " " )
i - = 1
flag = 0
r = i
k + = 1
if __name__ = = "__main__" :
n = 6
arr = [ 1 , 2 , 3 , 4 , 5 , 6 ]
printArray(arr, n)
|
C#
using System;
class GFG{
static void printArray( int []arr, int n)
{
Array.Sort(arr);
int l = 0, r = n - 1, flag = 0, i;
int k = 2;
while (l <= r) {
if (flag == 0) {
for (i = l; i < l + k && i <= r; i++)
Console.Write(arr[i] + " " );
flag = 1;
l = i;
}
else
{
for (i = r; i > r - k && i >= l; i--)
Console.Write(arr[i] + " " );
flag = 0;
r = i;
}
k++;
}
}
static public void Main ()
{
int n = 6;
int []arr = { 1, 2, 3, 4, 5, 6 };
printArray(arr, n);
}
}
|
Javascript
<script>
function printArray(arr, n)
{
arr.sort();
let l = 0, r = n - 1, flag = 0, i;
let k = 2;
while (l <= r)
{
if (flag == 0)
{
for (i = l; i < l + k && i <= r; i++)
document.write(arr[i] + " " );
flag = 1;
l = i;
}
else
{
for (i = r; i > r - k && i >= l; i--)
document.write(arr[i] + " " );
flag = 0;
r = i;
}
k++;
}
}
let n = 6;
let arr = [ 1, 2, 3, 4, 5, 6 ];
printArray(arr, n);
</script>
|
PHP
<?php
function printArray( $arr , $n )
{
sort( $arr );
$l = 0;
$r = $n - 1;
$flag = 0;
$k = 2;
while ( $l <= $r )
{
if ( $flag == 0)
{
for ( $i = $l ;
$i < $l + $k &&
$i <= $r ; $i ++)
echo $arr [ $i ] , " " ;
$flag = 1;
$l = $i ;
}
else
{
for ( $i = $r ;
$i > $r - $k &&
$i >= $l ; $i --)
echo $arr [ $i ] , " " ;
$flag = 0;
$r = $i ;
}
$k ++;
}
}
$n = 6;
$arr = array ( 1, 2, 3, 4, 5, 6 );
printArray( $arr , $n );
?>
|
Time Complexity : O(nlogn)
Auxiliary Space: O(1)
Approach#2: Using deque
This approach uses a deque from the collections module to implement a queue. It then iterates through the queue in a while loop, using a boolean flag to determine whether to pop from the left or the right of the queue, and print the element. Finally, the flag is toggled for the next iteration.
Algorithm
1. Initialize a queue and enqueue all the elements of the array.
2. Initialize a flag variable as True.
3. While the queue is not empty, do the following:
a. If the flag is True, print the element at the front of the queue and dequeue it.
b. If the flag is False, print the element at the rear of the queue and dequeue it.
c. Toggle the flag variable.
4. Repeat step 3 until the queue is empty.
C++
#include <iostream>
#include <deque>
#include <vector>
using namespace std;
void print_alternate(vector< int > arr)
{
deque< int > q(arr.begin(), arr.end());
bool flag = true ;
while (!q.empty()) {
if (flag) {
cout << q.front() << " " ;
q.pop_front();
}
else {
cout << q.back() << " " ;
q.pop_back();
}
flag = !flag;
}
}
int main()
{
vector< int > arr = {2, 6, 2, 4, 0, 1, 4, 8, 2, 0, 0, 5, 2, 2};
print_alternate(arr);
return 0;
}
|
Java
import java.util.*;
public class Main
{
public static void printAlternate(ArrayList<Integer> arr)
{
Deque<Integer> q = new LinkedList<>(arr);
boolean flag = true ;
while (!q.isEmpty())
{
if (flag) {
System.out.print(q.removeFirst() + " " );
}
else {
System.out.print(q.removeLast() + " " );
}
flag = !flag;
}
}
public static void main(String[] args)
{
ArrayList<Integer> arr = new ArrayList<>(Arrays.asList( 2 , 6 , 2 , 4 , 0 , 1 , 4 , 8 , 2 , 0 , 0 , 5 , 2 , 2 ));
printAlternate(arr);
}
}
|
Python3
from collections import deque
def print_alternate(arr):
q = deque(arr)
flag = True
while q:
if flag:
print (q.popleft(), end = " " )
else :
print (q.pop(), end = " " )
flag = not flag
arr = [ 2 , 6 , 2 , 4 , 0 , 1 , 4 , 8 , 2 , 0 , 0 , 5 , 2 , 2 ]
print_alternate(arr)
|
C#
using System;
using System.Collections.Generic;
class Program
{
static void PrintAlternate(List< int > arr)
{
LinkedList< int > deque = new LinkedList< int >(arr);
bool flag = true ;
while (deque.Count > 0)
{
if (flag)
{
Console.Write(deque.First.Value + " " );
deque.RemoveFirst();
}
else
{
Console.Write(deque.Last.Value + " " );
deque.RemoveLast();
}
flag = !flag;
}
}
static void Main( string [] args)
{
List< int > arr = new List< int > { 2, 6, 2, 4, 0, 1, 4, 8, 2, 0, 0, 5, 2, 2 };
PrintAlternate(arr);
}
}
|
Javascript
function printAlternate(arr) {
const deque = arr.slice();
let flag = true ;
while (deque.length > 0) {
if (flag) {
process.stdout.write(deque.shift() + ' ' );
}
else {
process.stdout.write(deque.pop() + ' ' );
}
flag = !flag;
}
}
function main() {
const arr = [2, 6, 2, 4, 0, 1, 4, 8, 2, 0, 0, 5, 2, 2];
printAlternate(arr);
console.log();
}
main();
|
Output2 2 6 2 2 5 4 0 0 0 1 2 4 8
Time Complexity: O(n) for traversing the array and enqueueing/dequeueing elements
Auxiliary Space: O(n) for the queue
Share your thoughts in the comments
Please Login to comment...