# Circular array

An array is called circular if we consider the first element as next of the last element. Circular arrays are used to implement queue (Refer to this and this).
An example problem :
Suppose n people are sitting at a circular table with names A, B, C, D, … Given a name, we need to print all n people (in order) starting from the given name. For example, consider 6 people A B C D E F and given name as ‘D’. People sitting in a 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 it in another array. For example for 6 people, we create below the 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 the following 6.
A B C D E F A B C D E F

Below is the implementation of the above approach.

## C++

 `// CPP program to demonstrate use of circular ` `// array using extra memory space` `#include ` `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);` `    ``print(a, n, 3);` `    ``return` `0;` `}`

## Java

 `// 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 code` `    ``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 */`

## Python3

 `# 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`

## C#

 `// 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 code` `    ``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*/`

## Javascript

 `// Function to print the circular rotation of array ` `function` `print(a, n, ind) {` `  ``// Create an auxiliary array of twice size.` `  ``let b = ``new` `Array(2 * n);`   `  ``// Copy the input array 'a' to the auxiliary array 'b' twice` `  ``// so that we can rotate the array circularly` `  ``for` `(let i = 0; i < n; i++) ` `    ``b[i] = b[n + i] = a[i];`   `  ``// print from the `ind-th` index to the `(n+ind)th` index in the auxiliary array 'b'.` `  ``let output = ``''``;` `  ``for` `(let i = ind; i < n + ind; i++)` `    ``output += b[i] + ``' '``;` `  ``console.log(output);` `}`   `// Driver code ` `let a = [``'A'``, ``'B'``, ``'C'``, ``'D'``, ``'E'``, ``'F'``];` `let n = a.length;`   `// Call the function and pass the input array, its size, and the index to rotate.` `print(a, n, 3);`

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 the 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++

 `// CPP program to demonstrate the use of circular` `// array without using extra memory space` `#include ` `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` `int` `main()` `{` `    ``char` `a[] = { ``'A'``, ``'B'``, ``'C'``, ``'D'``, ``'E'``, ``'F'` `};` `    ``int` `n = ``sizeof``(a) / ``sizeof``(a);` `    ``print(a, n, 3);` `    ``return` `0;` `}`

## Java

 `// 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` `    ``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 */`

## Python3

 `# 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`

## C#

 `// 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` `    ``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 */`

## Javascript

 `// function to print circular list` `// starting from given index ind.` `function` `print(a, n, ind) {` `  ``// print from ind-th index to` `  ``// (n+i)th index.` `  ``for` `(``var` `i = 0; i < n; i++) {` `    ``console.log(a[(ind + i) % n] + ``" "``);` `  ``}` `}`   `// Driver code` `var` `a = [``'A'``, ``'B'``, ``'C'``, ``'D'``, ``'E'``, ``'F'``];` `var` `n = 6;` `print(a, n, 3);`

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.

Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!

Previous
Next
{"ft_lang_label":"__label__en","ft_lang_prob":0.5590197,"math_prob":0.95070034,"size":4788,"snap":"2023-40-2023-50","text_gpt3_token_len":1727,"char_repetition_ratio":0.13837792,"word_repetition_ratio":0.52491105,"special_character_ratio":0.4243943,"punctuation_ratio":0.20415226,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9998436,"pos_list":[0,1,2],"im_url_duplicate_count":[null,6,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2023-12-11T00:40:26Z\",\"WARC-Record-ID\":\"<urn:uuid:94374e68-bc7a-4c2f-9c32-7719d4534f94>\",\"Content-Length\":\"411292\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:e5f3f9d3-c2c1-4a0f-bd0c-4f5078890407>\",\"WARC-Concurrent-To\":\"<urn:uuid:1a3010d4-f6d6-416d-be17-ea91dc50419c>\",\"WARC-IP-Address\":\"108.138.64.13\",\"WARC-Target-URI\":\"https://www.geeksforgeeks.org/circular-array/\",\"WARC-Payload-Digest\":\"sha1:J62RMS2PZMWKQBXUMT5LI525PHDFU7XQ\",\"WARC-Block-Digest\":\"sha1:GQXBYWSK2NMK4YWSWIHX5UIB3NMNHCLI\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2023/CC-MAIN-2023-50/CC-MAIN-2023-50_segments_1700679102697.89_warc_CC-MAIN-20231210221943-20231211011943-00425.warc.gz\"}"}