Given an array of binary integers, suppose these values are kept on the circumference of a circle at an equal distance. We need to tell whether is it possible to draw a regular polygon using only 1s as its vertices and if it is possible then print the maximum number of sides that regular polygon has.
Example:
Input : arr[] = [1, 1, 1, 0, 1, 0] Output : Polygon possible with side length 3 We can draw a regular triangle having 1s as its vertices as shown in below diagram (a). Input : arr[] = [1, 0, 1, 0, 1, 0, 1, 0, 1, 1] Output : Polygon possible with side length 5 We can draw a regular pentagon having 1s as its vertices as shown in below diagram (b).![]()
We can solve this problem by getting a relation between the number of vertices a possible polygon can have and total number of values in the array. Let a possible regular polygon in circle has K vertices or K sides then it should satisfy two things to be the answer,
If given array size is N, then K should divide N otherwise K vertices can’t divide N vertices in an equally manner to be a regular polygon.
Next thing is there should be one at every vertex of chosen polygon.
After above points, we can see that for solving this problem we need to iterate over divisors of N and then check whether every value of the array at chosen divisor’s distance is 1 or not. If it is 1 then we found our solution. We can iterate over all divisors in O(sqrt(N)) time just by iterating over from 1 to sqrt(N). you can read more about that here.
C++
// C++ program to find whether a regular polygon // is possible in circle with 1s as vertices #include <bits/stdc++.h> using namespace std; // method returns true if polygon is possible with // 'midpoints' number of midpoints bool checkPolygonWithMidpoints(int arr[], int N, int midpoints) { // loop for getting first vertex of polygon for (int j = 0; j < midpoints; j++) { int val = 1; // loop over array values at 'midpoints' distance for (int k = j; k < N; k += midpoints) { // and(&) all those values, if even one of // them is 0, val will be 0 val &= arr[k]; } /* if val is still 1 and (N/midpoints) or (number of vertices) are more than two (for a polygon minimum) print result and return true */ if (val && N/midpoints > 2) { cout << "Polygon possible with side length " << << (N/midpoints) << endl; return true; } } return false; } // method prints sides in the polygon or print not // possible in case of no possible polygon void isPolygonPossible(int arr[], int N) { // limit for iterating over divisors int limit = sqrt(N); for (int i = 1; i <= limit; i++) { // If i divides N then i and (N/i) will // be divisors if (N % i == 0) { // check polygon for both divisors if (checkPolygonWithMidpoints(arr, N, i) || checkPolygonWithMidpoints(arr, N, (N/i))) return; } } cout << "Not possiblen"; } // Driver code to test above methods int main() { int arr[] = {1, 0, 1, 0, 1, 0, 1, 0, 1, 1}; int N = sizeof(arr) / sizeof(arr[0]); isPolygonPossible(arr, N); return 0; } |
Java
// Java program to find whether a regular polygon // is possible in circle with 1s as vertices class Test { // method returns true if polygon is possible with // 'midpoints' number of midpoints static boolean checkPolygonWithMidpoints(int arr[], int N, int midpoints) { // loop for getting first vertex of polygon for (int j = 0; j < midpoints; j++) { int val = 1; // loop over array values at 'midpoints' distance for (int k = j; k < N; k += midpoints) { // and(&) all those values, if even one of // them is 0, val will be 0 val &= arr[k]; } /* if val is still 1 and (N/midpoints) or (number of vertices) are more than two (for a polygon minimum) print result and return true */ if (val != 0 && N/midpoints > 2) { System.out.println("Polygon possible with side length " + N/midpoints); return true; } } return false; } // method prints sides in the polygon or print not // possible in case of no possible polygon static void isPolygonPossible(int arr[], int N) { // limit for iterating over divisors int limit = (int)Math.sqrt(N); for (int i = 1; i <= limit; i++) { // If i divides N then i and (N/i) will // be divisors if (N % i == 0) { // check polygon for both divisors if (checkPolygonWithMidpoints(arr, N, i) || checkPolygonWithMidpoints(arr, N, (N/i))) return; } } System.out.println("Not possible"); } // Driver method public static void main(String args[]) { int arr[] = {1, 0, 1, 0, 1, 0, 1, 0, 1, 1}; isPolygonPossible(arr, arr.length); } } |
Python3
# Python3 program to find whether a # regular polygon is possible in circle # with 1s as vertices from math import sqrt # method returns true if polygon is # possible with 'midpoints' number # of midpoints def checkPolygonWithMidpoints(arr, N, midpoints) : # loop for getting first vertex of polygon for j in range(midpoints) : val = 1 # loop over array values at # 'midpoints' distance for k in range(j , N, midpoints) : # and(&) all those values, if even # one of them is 0, val will be 0 val &= arr[k] # if val is still 1 and (N/midpoints) or (number # of vertices) are more than two (for a polygon # minimum) print result and return true if (val and N // midpoints > 2) : print("Polygon possible with side length" , (N // midpoints)) return True return False # method prints sides in the polygon or print # not possible in case of no possible polygon def isPolygonPossible(arr, N) : # limit for iterating over divisors limit = sqrt(N) for i in range(1, int(limit) + 1) : # If i divides N then i and (N/i) # will be divisors if (N % i == 0) : # check polygon for both divisors if (checkPolygonWithMidpoints(arr, N, i) or checkPolygonWithMidpoints(arr, N, (N // i))): return print("Not possiblen") # Driver code if __name__ == "__main__" : arr = [1, 0, 1, 0, 1, 0, 1, 0, 1, 1] N = len(arr) isPolygonPossible(arr, N) # This code is contributed by Ryuga |
C#
// C# program to find whether // a regular polygon is possible // in circle with 1s as vertices using System; class GFG { // method returns true if // polygon is possible // with 'midpoints' number // of midpoints static bool checkPolygonWithMidpoints(int []arr, int N, int midpoints) { // loop for getting first // vertex of polygon for (int j = 0; j < midpoints; j++) { int val = 1; // loop over array values // at 'midpoints' distance for (int k = j; k < N; k += midpoints) { // and(&) all those values, // if even one of them is 0, // val will be 0 val &= arr[k]; } /* if val is still 1 and (N/midpoints) or (number of vertices) are more than two (for a polygon minimum) print result and return true */ if (val != 0 && N / midpoints > 2) { Console.WriteLine("Polygon possible with " + "side length " + N / midpoints); return true; } } return false; } // method prints sides in the // polygon or print not possible // in case of no possible polygon static void isPolygonPossible(int []arr, int N) { // limit for iterating // over divisors int limit = (int)Math.Sqrt(N); for (int i = 1; i <= limit; i++) { // If i divides N then i // and (N/i) will be divisors if (N % i == 0) { // check polygon for // both divisors if (checkPolygonWithMidpoints(arr, N, i) || checkPolygonWithMidpoints(arr, N, (N / i))) return; } } Console.WriteLine("Not possible"); } // Driver Code static public void Main () { int []arr = {1, 0, 1, 0, 1, 0, 1, 0, 1, 1}; isPolygonPossible(arr, arr.Length); } } // This code is contributed by jit_t |
PHP
<?php // PHP program to find whether // a regular polygon is possible // in circle with 1s as vertices // method returns true if polygon // is possible with 'midpoints' // number of midpoints function checkPolygonWithMidpoints($arr, $N, $midpoints) { // loop for getting first // vertex of polygon for ($j = 0; $j < $midpoints; $j++) { $val = 1; // loop over array values // at 'midpoints' distance for ($k = $j; $k < $N; $k += $midpoints) { // and(&) all those values, // if even one of them is 0, // val will be 0 $val &= $arr[$k]; } /* if val is still 1 and (N/midpoints) or (number of vertices) are more than two (for a polygon minimum) print result and return true */ if ($val && $N / $midpoints > 2) { echo "Polygon possible with side length " , ($N / $midpoints) , "\n"; return true; } } return false; } // method prints sides in // the polygon or print not // possible in case of no // possible polygon function isPolygonPossible($arr, $N) { // limit for iterating // over divisors $limit = sqrt($N); for ($i = 1; $i <= $limit; $i++) { // If i divides N then // i and (N/i) will be // divisors if ($N % $i == 0) { // check polygon for // both divisors if (checkPolygonWithMidpoints($arr, $N, $i) || checkPolygonWithMidpoints($arr, $N, ($N / $i))) return; } } echo "Not possiblen"; } // Driver Code $arr = array(1, 0, 1, 0, 1, 0, 1, 0, 1, 1); $N = sizeof($arr); isPolygonPossible($arr, $N); // This code is contributed by ajit ?> |
Output:
Polygon possible with side length 5
This article is contributed by Utkarsh Trivedi. 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
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:
- Side of a regular n-sided polygon circumscribed in a circle
- Area of largest Circle inscribe in N-sided Regular polygon
- How to discretize an Ellipse or Circle to a Polygon using C++ Graphics?
- Program to find the Circumcircle of any regular polygon
- Program to find the Perimeter of a Regular Polygon
- Program to find Area of Triangle inscribed in N-sided Regular Polygon
- Area of a n-sided regular polygon with given side length
- Area of a n-sided regular polygon with given Radius
- Length of Diagonal of a n-sided regular polygon
- Apothem of a n-sided regular polygon
- Central angle of a N sided Regular Polygon
- Check whether two convex regular polygon have same center or not
- Minimum side of square embedded in Regular polygon with N sides
- Find the angle of Rotational Symmetry of an N-sided regular polygon
- Area of a circle inscribed in a regular hexagon
- Count subarrays consisting of only 0's and only 1's in a binary array
- Program to calculate area of inner circle which passes through center of outer circle and touches its circumference
- Equation of circle when three points on the circle are given
- Check if a circle lies inside another circle or not
- Angle subtended by the chord to center of the circle when the angle subtended by the another equal chord of a congruent circle is given


