Print array elements in alternatively increasing and decreasing order - GeeksforGeeks

05 Nov, 2021
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 52The 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++
Java
Python3
C#
PHP
Javascript
// C++ program to print array elements in// alternative increasing and decreasing// order #include <bits/stdc++.h>using namespace std; // Function to print array elements in// alternative increasing and decreasing// ordervoid printArray(int arr[], int n){    // First sort the array in increasing order    sort(arr, arr + n);     int l = 0, r = n - 1, flag = 0, i;     // start with 2 elements in    // increasing order    int k = 2;     // till all the elements are not printed    while (l <= r) {        // printing the elements in        // increasing order        if (flag == 0) {            for (i = l; i < l + k && i <= r; i++)                cout << arr[i] << " ";             flag = 1;            l = i;        }        else // printing the elements in        // decreasing order        {            for (i = r; i > r - k && i >= l; i--)                cout << arr[i] << " ";             flag = 0;            r = i;        }         // increasing the number of elements        // to printed in next iteration        k++;    }} // Driver Codeint main(){    int n = 6;     int arr[] = { 1, 2, 3, 4, 5, 6 };     printArray(arr, n);     return 0;}
// Java program to print array elements in// alternative increasing and decreasing// orderimport java.util.*;class Solution{  // Function to print array elements in// alternative increasing and decreasing// orderstatic void printArray(int arr[], int n){    // First sort the array in increasing order    Arrays.sort(arr);      int l = 0, r = n - 1, flag = 0, i;      // start with 2 elements in    // increasing order    int k = 2;      // till all the elements are not printed    while (l <= r) {        // printing the elements in        // increasing order        if (flag == 0) {            for (i = l; i < l + k && i <= r; i++)                System.out.print(arr[i] + " ");              flag = 1;            l = i;        }        else // printing the elements in        // decreasing order        {            for (i = r; i > r - k && i >= l; i--)                System.out.print(arr[i] + " ");              flag = 0;            r = i;        }          // increasing the number of elements        // to printed in next iteration        k++;    }}  // Driver Codepublic static void main(String args[]){    int n = 6;      int arr[] = { 1, 2, 3, 4, 5, 6 };      printArray(arr, n);   } }//contributed by Arnab Kundu
# Python 3 program to print array elements# in alternative increasing and decreasing# order # Function to print array elements in# alternative increasing and decreasing# orderdef printArray(arr, n):     # First sort the array in    # increasing order    arr.sort()     l = 0    r = n - 1    flag = 0         # start with 2 elements in    # increasing order    k = 2     # till all the elements are not printed    while (l <= r) :                 # printing the elements in        # increasing order        if (flag == 0):            i = l            while i < l + k and i <= r:                print(arr[i], end = " ")                i += 1             flag = 1            l = i                 else:     # printing the elements in                 # decreasing order            i = r            while i > r - k and i >= l:                print(arr[i], end = " ")                i -= 1             flag = 0            r = i         # increasing the number of elements        # to printed in next iteration        k += 1 # Driver Codeif __name__ == "__main__":         n = 6    arr = [ 1, 2, 3, 4, 5, 6 ]    printArray(arr, n) # This code is contributed by ita_c
// C# program to print array elements in// alternative increasing and decreasing// orderusing System;  class GFG{      // Function to print array elements in// alternative increasing and decreasing// orderstatic void printArray(int []arr, int n){         // First sort the array    // in increasing order    Array.Sort(arr);      int l = 0, r = n - 1, flag = 0, i;      // start with 2 elements in    // increasing order    int k = 2;      // till all the elements    // are not printed    while (l <= r) {                 // printing the elements in        // increasing order        if (flag == 0) {                         for (i = l; i < l + k && i <= r; i++)                    Console.Write(arr[i] + " ");              flag = 1;            l = i;        }        else                 // printing the elements in        // decreasing order        {            for (i = r; i > r - k && i >= l; i--)                Console.Write(arr[i] + " ");              flag = 0;            r = i;        }          // increasing the number of elements        // to printed in next iteration        k++;    }}  // Driver Codestatic public void Main (){              int n = 6;    int []arr = { 1, 2, 3, 4, 5, 6 };    printArray(arr, n); }} // This code is contributed by Sach_Code
<?php// PHP program to print array elements in// alternative increasing and decreasing// order // Function to print array elements in// alternative increasing and decreasing// orderfunction printArray($arr, $n){    // First sort the array in    // increasing order    sort($arr);     $l = 0;    $r = $n - 1;    $flag = 0;     // start with 2 elements in    // increasing order    $k = 2;     // till all the elements are    // not printed    while ($l <= $r)    {        // printing the elements in        // increasing order        if ($flag == 0)        {            for ($i = $l;                 $i < $l + $k &&                 $i <= $r; $i++)                echo $arr[$i] , " ";             $flag = 1;            $l = $i;        }        else // printing the elements in             // decreasing order        {            for ($i = $r;                 $i > $r - $k &&                 $i >= $l; $i--)                echo $arr[$i] , " ";             $flag = 0;            $r = $i;        }         // increasing the number of elements        // to printed in next iteration        $k++;    }} // Driver Code$n = 6;$arr = array( 1, 2, 3, 4, 5, 6 ); printArray($arr, $n); // This code is contributed by jit_t?>
<script> // Javascript program to print array elements in// alternative increasing and decreasing// order // Function to print array elements in// alternative increasing and decreasing// orderfunction printArray(arr, n){         // First sort the array    // in increasing order    arr.sort();     let l = 0, r = n - 1, flag = 0, i;     // start with 2 elements in    // increasing order    let k = 2;     // till all the elements    // are not printed    while (l <= r)    {                 // printing the elements in        // increasing order        if (flag == 0)        {            for(i = l; i < l + k && i <= r; i++)                document.write(arr[i] + " ");             flag = 1;            l = i;        }        else         // Printing the elements in        // decreasing order        {            for(i = r; i > r - k && i >= l; i--)                document.write(arr[i] + " ");             flag = 0;            r = i;        }         // Increasing the number of elements        // to printed in next iteration        k++;    }} // Driver codelet n = 6;let arr = [ 1, 2, 3, 4, 5, 6 ];printArray(arr, n); // This code is contributed by suresh07 </script>
1 2 6 5 4 3
 
Time Complexity : O(nlogn)Auxiliary Space: O(1) 
andrew1234
Sach_Code
jit_t
ukasp
VishalBachchas
suresh07
pankajsharmagfg
ashutoshsinghgeeksforgeeks
Constructive Algorithms
Oracle
Technical Scripter 2018
Algorithms
Sorting
Oracle
Sorting
Algorithms
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
SDE SHEET - A Complete Guide for SDE Preparation
DSA Sheet by Love Babbar
Introduction to Algorithms
Difference between Informed and Uninformed Search in AI
Quick Sort vs Merge Sort