Given a circular array of size N. The task is to check if, for every index i starting from 0 to N-1, there exists an index j which is not equal to i such that sum of all the numbers in the clockwise direction from i to j is equal to the sum of all numbers in the anticlockwise direction from i to j.

**Examples:**

Input:a[] = {1, 4, 1, 4}

Output:Yes

The circular array is 1->4->1->4.

for index 0, j will be 2, then sum of elements from index 0 to 2 in clockwise direction will be 1 + 4 + 1 = 6 and the sum of elements from index 0 to 2 in anticlockwise direction will be 1 + 4 + 1 = 6.for index 1, j will be 3, then sum of elements from index 1 to 3 in clockwise direction will be 4 + 1 + 4 = 9 and the sum of elements from index 1 to 3 in anticlockwise direction will be 4 + 1 + 4 = 9.

for index 2, j will be 0, then sum of elements from index 2 to 0 in clockwise direction will be 1 + 4 + 1 = 6 and the sum of elements from index 2 to 0 in anticlockwise direction will be 1 + 4 + 1 = 6

for index 3, j will be 1, then sum of elements from index 3 to 1 in clockwise direction will be 4 + 1 + 4 = 9 and the sum of elements from index 3 to 1 in anticlockwise direction will be 4 + 1 + 4 = 9

Input:a[] = {1, 1, 1, 1, 1, 1}

Output:Yes

**Approach: **

- When N is odd, the answer will be “NO” always as it is not possible to find an index j for every index i.
- If N is even, then check if the opposite element is exactly same for every index i, then the answer is “YES”.
- If the any of the index’s opposite element i.e.,
**a[(i+(n/2))] is not equal to a[i]**, then the answer will be “NO”.

Below is the implementaion of above approach:

## C++

`// C++ program to check if the ` `// number lies in given range ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function that returns the maximum element. ` `bool` `check(` `int` `a[], ` `int` `n) ` `{ ` ` ` ` ` `// check for odd ` ` ` `if` `(n % 2 == 1) ` ` ` `return` `false` `; ` ` ` ` ` `// check if the opposite element is same ` ` ` `// as a[i] ` ` ` `for` `(` `int` `i = 0; i < n / 2; i++) { ` ` ` `if` `(a[i] != a[i + (n / 2)]) ` ` ` `return` `false` `; ` ` ` `} ` ` ` ` ` `return` `true` `; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `a[] = { 1, 4, 1, 4 }; ` ` ` ` ` `int` `n = ` `sizeof` `(a) / ` `sizeof` `(a[0]); ` ` ` ` ` `if` `(check(a, n)) ` ` ` `cout << ` `"YES"` `; ` ` ` `else` ` ` `cout << ` `"NO"` `; ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`//Java program to check if the ` `//number lies in given range ` ` ` `public` `class` `GFG { ` ` ` ` ` `//Function that returns the maximum element. ` ` ` `static` `boolean` `check(` `int` `a[], ` `int` `n) ` ` ` `{ ` ` ` ` ` `// check for odd ` ` ` `if` `(n % ` `2` `== ` `1` `) ` ` ` `return` `false` `; ` ` ` ` ` `// check if the opposite element is same ` ` ` `// as a[i] ` ` ` `for` `(` `int` `i = ` `0` `; i < n / ` `2` `; i++) { ` ` ` `if` `(a[i] != a[i + (n / ` `2` `)]) ` ` ` `return` `false` `; ` ` ` `} ` ` ` ` ` `return` `true` `; ` ` ` `} ` ` ` ` ` `//Driver code ` ` ` `public` `static` `void` `main(String[] args) { ` ` ` ` ` `int` `a[] = { ` `1` `, ` `4` `, ` `1` `, ` `4` `}; ` ` ` ` ` `int` `n = a.length; ` ` ` ` ` `if` `(check(a, n)) ` ` ` `System.out.println(` `"YES"` `); ` ` ` `else` ` ` `System.out.println(` `"NO"` `); ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

## Python 3

`# Python 3 Program to check if the ` `# number lies in given range ` ` ` `# Function that returns the maximum element. ` `def` `check(a, n) : ` ` ` ` ` `# check for odd ` ` ` `if` `n ` `%` `2` `=` `=` `1` `: ` ` ` `return` `False` ` ` ` ` `# check if the opposite element is same ` ` ` `# as a[i] ` ` ` `for` `i ` `in` `range` `(n` `/` `/` `2` `) : ` ` ` `if` `a[i] !` `=` `a[i ` `+` `(n` `/` `/` `2` `)]: ` ` ` `return` `False` ` ` ` ` `return` `True` ` ` `# Driver Code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` `a ` `=` `[ ` `1` `, ` `4` `, ` `1` `, ` `4` `] ` ` ` ` ` `n ` `=` `len` `(a) ` ` ` ` ` `if` `check(a, n) : ` ` ` `print` `(` `"YES"` `) ` ` ` `else` `: ` ` ` `print` `(` `"NO"` `) ` ` ` `# This code is contributed by ANKITRAI1 ` |

*chevron_right*

*filter_none*

## C#

`// C# program to check if the ` `// number lies in given range ` ` ` `class` `GFG ` `{ ` ` ` `// Function that returns the ` `// maximum element. ` `static` `bool` `check(` `int` `[] a, ` `int` `n) ` `{ ` ` ` `// check for odd ` `if` `(n % 2 == 1) ` ` ` `return` `false` `; ` ` ` `// check if the opposite ` `// element is same as a[i] ` `for` `(` `int` `i = 0; i < (` `int` `)n / 2; i++) ` `{ ` ` ` `if` `(a[i] != a[i + (` `int` `)(n / 2)]) ` ` ` `return` `false` `; ` `} ` ` ` `return` `true` `; ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main() ` `{ ` ` ` `int` `[] a = ` `new` `int` `[]{ 1, 4, 1, 4 }; ` ` ` ` ` `int` `n = a.Length; ` ` ` ` ` `if` `(check(a, n)) ` ` ` `System.Console.WriteLine(` `"YES"` `); ` ` ` `else` ` ` `System.Console.WriteLine(` `"NO"` `); ` `} ` `} ` ` ` `// This code is contributed by mits ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to check if the ` `// number lies in given range ` ` ` `// Function that returns the ` `// maximum element. ` `function` `check(` `$a` `, ` `$n` `) ` `{ ` ` ` ` ` `// check for odd ` ` ` `if` `(` `$n` `% 2 == 1) ` ` ` `return` `false; ` ` ` ` ` `// check if the opposite ` ` ` `// element is same as a[i] ` ` ` `for` `(` `$i` `= 0; ` `$i` `< ` `$n` `/ 2; ` `$i` `++) ` ` ` `{ ` ` ` `if` `(` `$a` `[` `$i` `] != ` `$a` `[` `$i` `+ (` `$n` `/ 2)]) ` ` ` `return` `false; ` ` ` `} ` ` ` ` ` `return` `true; ` `} ` ` ` `// Driver code ` `$a` `= ` `array` `( 1, 4, 1, 4 ); ` ` ` `$n` `= sizeof(` `$a` `); ` ` ` `if` `(check(` `$a` `, ` `$n` `)) ` ` ` `echo` `"YES"` `; ` `else` ` ` `echo` `"NO"` `; ` ` ` `// This code is contributed ` `// by Akanksha Rai(Abby_akku) ` |

*chevron_right*

*filter_none*

**Output:**

YES

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Count pairs in an array such that both elements has equal set bits
- Number of permutations such that sum of elements at odd index and even index are equal
- Maximum sum in an array such that every element has exactly one adjacent element to it
- Partition an array of non-negative integers into two subsets such that average of both the subsets is equal
- Count ways to partition a string such that both parts have equal distinct characters
- Check if the array has an element which is equal to sum of all the remaining elements
- Divide array in two Subsets such that sum of square of sum of both subsets is maximum
- Check whether it is possible to make both arrays equal by modifying a single element
- Rearrange array such that even index elements are smaller and odd index elements are greater
- Remove minimum number of elements such that no common element exist in both array
- Check if the array has an element which is equal to product of remaining elements
- Check if the array has an element which is equal to XOR of remaining elements
- Check if array elements are consecutive in O(n) time and O(1) space (Handles Both Positive and negative numbers)
- Permute two arrays such that sum of every pair is greater or equal to K
- Count of array elements which is smaller than both its adjacent elements
- Number of sub-sequence such that it has one consecutive element with difference less than or equal to 1
- Array Index with same count of even or odd numbers on both sides
- Index of the elements which are equal to the sum of all succeeding elements
- Smallest number to be added in first Array modulo M to make frequencies of both Arrays equal
- Count of contiguous subarrays possible for every index by including the element at that index

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.