Circular array

An array is called circular if we consider first element as next of last element. Circular arrays are used to implement queue (Refer this and this).

An example problem :
Suppose n people are sitting in a circular table with names A, B, C, D,…. Given a name, we need to print all n people (in order) starting from given name.

For example, consider 6 people A B C D E F and given name as ‘D’. People sitting in circular manner starting from D are D E F A B C.

A simple solution is to create an auxiliary array of size 2*n and store in another array. For example for 6 people, we create below auxiliary array.
A B C D E F A B C D E F

Now for any given index, we simply print n elements starting from it. For example, we print following 6.
A B C D E F A B C D E F

Below is the implementation of the above approach.

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP program to demonstrate use of circular 
// array using extra memory space
#include <bits/stdc++.h>
using namespace std;
void print(char a[], int n, int ind)
{
    // Create an auxiliary array of twice size.
    char b[(2 * n)];
  
    // Copy a[] to b[] two times 
    for (int i = 0; i < n; i++) 
        b[i] = b[n + i] = a[i];
  
    // print from ind-th index to (n+i)th index.
    for (int i = ind; i < n + ind; i++)
        cout << b[i] << " ";
}
  
// driver code 
int main()
{
    char a[] = { 'A', 'B', 'C', 'D', 'E', 'F' };
    int n = sizeof(a) / sizeof(a[0]);
    print(a, n, 3);
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate use of circular 
// array using extra memory space
import java.util.*;
import java.lang.*;
  
public class GfG{
      
    public static void print(char a[], int n, 
                                  int ind){
      
        // Create an auxiliary array 
        // of twice size.
        char[] b = new char[(2 * n)];
  
        // Copy a[] to b[] two times 
        for (int i = 0; i < n; i++) 
            b[i] = b[n + i] = a[i];
  
        // print from ind-th index to 
        // (n+i)th index.
        for (int i = ind; i < n + ind; i++)
            System.out.print(b[i]+" ");
    }
      
    // driver function
    public static void main(String argc[]){
        char[] a = new char[]{ 'A', 'B', 'C'
                             'D', 'E', 'F' };
        int n = 6;
        print(a, n, 3);
    }
}
  
/* This code is contributed by Sagar Shukla */

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to demonstrate use of 
# circular array using extra memory space
  
def prints(a, n, ind):
      
    # Create an auxiliary array of twice size.
    b = [None]*2*n
    i = 0
      
    # Copy a[] to b[] two times 
    while i < n: 
        b[i] = b[n + i] = a[i]
        i = i + 1
      
    i = ind
  
    # print from ind-th index to (n+i)th index.
    while i < n + ind :
        print(b[i], end = " ");
        i = i + 1
  
# Driver Code
a = ['A', 'B', 'C', 'D', 'E', 'F']
n = len(a);
prints(a, n, 3);
  
#This code is contributed by rishabh_jain

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to demonstrate use of circular
// array using extra memory space
using System;
  
public class GfG {
  
    public static void print(char[] a, int n,
                                     int ind)
    {
        // Create an auxiliary array
        // of twice size.
        char[] b = new char[(2 * n)];
  
        // Copy a[] to b[] two times
        for (int i = 0; i < n; i++)
            b[i] = b[n + i] = a[i];
  
        // print from ind-th index to
        // (n+i)th index.
        for (int i = ind; i < n + ind; i++)
            Console.Write(b[i] + " ");
    }
  
    // driver function
    public static void Main()
    {
        char[] a = new char[] { 'A', 'B', 'C',
                                'D', 'E', 'F' };
        int n = 6;
        print(a, n, 3);
    }
}
  
/* This code is contributed by vt_m*/

chevron_right



Output:

D E F A B C 

This approach takes of O(n) time but takes extra space of order O(n)

An efficient solution is to deal with circular arrays using the same array. If a careful observation is run through the array, then after n-th index, the next index always starts from 0 so using mod operator, we can easily access the elements of the circular list, if we use (i)%n and run the loop from i-th index to n+i-th index . and apply mod we can do the traversal in a circular array within the given array without using any extra space.

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP program to demonstrate the use of circular
// array without using extra memory space
#include <bits/stdc++.h>
using namespace std;
  
// function to print circular list starting
// from given index ind.
void print(char a[], int n, int ind)
{
    // print from ind-th index to (n+i)th index.
    for (int i = ind; i < n + ind; i++)
        cout << a[(i % n)] << " ";
}
  
// driver code to check the above function
int main()
{
    char a[] = { 'A', 'B', 'C', 'D', 'E', 'F' };
    int n = sizeof(a) / sizeof(a[0]);
    print(a, n, 3);
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate use of circular 
// array using extra memory space
import java.util.*;
import java.lang.*;
  
public class GfG{
      
    // function to print circular list 
    // starting from given index ind.
    public static void print(char a[], int n, 
                                   int ind){
          
        // print from ind-th index to 
        // (n+i)th index.
        for (int i = ind; i < n + ind; i++)
            System.out.print(a[(i % n)] + " ");
    }
      
    // driver code to check the above function
    public static void main(String argc[]){
        char[] a = new char[]{ 'A', 'B', 'C'
                             'D', 'E', 'F' };
        int n = 6;
        print(a, n, 3);
    }
}
  
/* This code is contributed by Sagar Shukla */

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to demonstrate the use of 
# circular array without using extra memory space
  
# function to print circular list starting
# from given index ind.
def prints(a, n, ind):
    i = ind
      
    # print from ind-th index to (n+i)th index.
    while i < n + ind :
        print(a[(i % n)], end = " ")
        i = i + 1
  
# Driver Code
a = ['A', 'B', 'C', 'D', 'E', 'F']
n = len(a);
prints(a, n, 3);
  
# This code is contributed by rishabh_jain

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to demonstrate use of circular
// array without using extra memory space
using System;
  
public class GfG {
  
    // function to print circular list
    // starting from given index ind.
    public static void print(char[] a, int n,
                                     int ind)
    {
        // print from ind-th index to
        // (n+i)th index.
        for (int i = ind; i < n + ind; i++)
            Console.Write(a[(i % n)] + " ");
    }
  
    // driver code to check the above function
    public static void Main()
    {
        char[] a = new char[] { 'A', 'B', 'C',
                                'D', 'E', 'F' };
        int n = 6;
        print(a, n, 3);
    }
}
  
/* This code is contributed by vt_m */

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to demonstrate use 
// of circular array without using
// extra memory space
  
// function to print circular list 
// starting from given index ind.
function prin($a, $n, $ind)
{
    // print from ind-th index 
    // to (n+i)th index.
    for ($i = $ind; $i < $n + $ind; $i++)
        echo $a[($i % $n)] . " ";
}
  
// Driver Code
$a = array( 'A', 'B', 'C'
            'D', 'E', 'F' );
$n = count($a);
prin($a, $n, 3);
  
// This code is contributed by Sam007
?>

chevron_right



Output :

D E F A B C 

This approach takes O(n) time and O(1) extra space.

More problems based on circular array :

Recent articles on circular array.



My Personal Notes arrow_drop_up

Striver(underscore)79 at Codechef and codeforces D

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : Sam007