Regular polygon using only 1s in a binary numbered circle
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 a total number of values in the array. Let a possible regular polygon in the circle has K vertices or K sides then it should satisfy two things to be the answer,
If the given array size is N, then K should divide N otherwise K vertices can’t divide N vertices in an equal manner to be a regular polygon.
Next thing is there should be one at every vertex of the chosen polygon.
After the 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.
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
bool checkPolygonWithMidpoints( int arr[], int N,
int midpoints)
{
for ( int j = 0; j < midpoints; j++)
{
int val = 1;
for ( int k = j; k < N; k += midpoints)
{
val &= arr[k];
}
if (val && N/midpoints > 2)
{
cout << "Polygon possible with side length " <<(N/midpoints) << endl;
return true ;
}
}
return false ;
}
void isPolygonPossible( int arr[], int N)
{
int limit = sqrt (N);
for ( int i = 1; i <= limit; i++)
{
if (N % i == 0)
{
if (checkPolygonWithMidpoints(arr, N, i) ||
checkPolygonWithMidpoints(arr, N, (N/i)))
return ;
}
}
cout << "Not possiblen" ;
}
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
class Test
{
static boolean checkPolygonWithMidpoints( int arr[], int N,
int midpoints)
{
for ( int j = 0 ; j < midpoints; j++)
{
int val = 1 ;
for ( int k = j; k < N; k += midpoints)
{
val &= arr[k];
}
if (val != 0 && N/midpoints > 2 )
{
System.out.println( "Polygon possible with side length " +
N/midpoints);
return true ;
}
}
return false ;
}
static void isPolygonPossible( int arr[], int N)
{
int limit = ( int )Math.sqrt(N);
for ( int i = 1 ; i <= limit; i++)
{
if (N % i == 0 )
{
if (checkPolygonWithMidpoints(arr, N, i) ||
checkPolygonWithMidpoints(arr, N, (N/i)))
return ;
}
}
System.out.println( "Not possible" );
}
public static void main(String args[])
{
int arr[] = { 1 , 0 , 1 , 0 , 1 , 0 , 1 , 0 , 1 , 1 };
isPolygonPossible(arr, arr.length);
}
}
|
Python3
from math import sqrt
def checkPolygonWithMidpoints(arr, N, midpoints) :
for j in range (midpoints) :
val = 1
for k in range (j , N, midpoints) :
val & = arr[k]
if (val and N / / midpoints > 2 ) :
print ( "Polygon possible with side length" ,
(N / / midpoints))
return True
return False
def isPolygonPossible(arr, N) :
limit = sqrt(N)
for i in range ( 1 , int (limit) + 1 ) :
if (N % i = = 0 ) :
if (checkPolygonWithMidpoints(arr, N, i) or
checkPolygonWithMidpoints(arr, N, (N / / i))):
return
print ( "Not possiblen" )
if __name__ = = "__main__" :
arr = [ 1 , 0 , 1 , 0 , 1 , 0 , 1 , 0 , 1 , 1 ]
N = len (arr)
isPolygonPossible(arr, N)
|
C#
using System;
class GFG
{
static bool checkPolygonWithMidpoints( int []arr, int N,
int midpoints)
{
for ( int j = 0; j < midpoints; j++)
{
int val = 1;
for ( int k = j; k < N; k += midpoints)
{
val &= arr[k];
}
if (val != 0 && N / midpoints > 2)
{
Console.WriteLine( "Polygon possible with " +
"side length " +
N / midpoints);
return true ;
}
}
return false ;
}
static void isPolygonPossible( int []arr,
int N)
{
int limit = ( int )Math.Sqrt(N);
for ( int i = 1; i <= limit; i++)
{
if (N % i == 0)
{
if (checkPolygonWithMidpoints(arr, N, i) ||
checkPolygonWithMidpoints(arr, N, (N / i)))
return ;
}
}
Console.WriteLine( "Not possible" );
}
static public void Main ()
{
int []arr = {1, 0, 1, 0, 1,
0, 1, 0, 1, 1};
isPolygonPossible(arr, arr.Length);
}
}
|
PHP
<?php
function checkPolygonWithMidpoints( $arr , $N ,
$midpoints )
{
for ( $j = 0; $j < $midpoints ; $j ++)
{
$val = 1;
for ( $k = $j ; $k < $N ; $k += $midpoints )
{
$val &= $arr [ $k ];
}
if ( $val && $N / $midpoints > 2)
{
echo "Polygon possible with side length " ,
( $N / $midpoints ) , "\n" ;
return true;
}
}
return false;
}
function isPolygonPossible( $arr , $N )
{
$limit = sqrt( $N );
for ( $i = 1; $i <= $limit ; $i ++)
{
if ( $N % $i == 0)
{
if (checkPolygonWithMidpoints( $arr , $N , $i ) ||
checkPolygonWithMidpoints( $arr , $N , ( $N / $i )))
return ;
}
}
echo "Not possiblen" ;
}
$arr = array (1, 0, 1, 0, 1,
0, 1, 0, 1, 1);
$N = sizeof( $arr );
isPolygonPossible( $arr , $N );
?>
|
Javascript
<script>
function checkPolygonWithMidpoints(arr, N, midpoints)
{
for (let j = 0; j < midpoints; j++)
{
let val = 1;
for (let k = j; k < N; k += midpoints)
{
val &= arr[k];
}
if (val && parseInt(N/midpoints) > 2)
{
document.write(
"Polygon possible with side length " +
parseInt(N/midpoints) + "<br>"
);
return true ;
}
}
return false ;
}
function isPolygonPossible(arr, N)
{
let limit = Math.sqrt(N);
for (let i = 1; i <= limit; i++)
{
if (N % i == 0)
{
if (checkPolygonWithMidpoints(arr, N, i) ||
checkPolygonWithMidpoints(arr, N,
parseInt(N/i)))
return ;
}
}
document.write( "Not possible" );
}
let arr = [1, 0, 1, 0, 1, 0, 1, 0, 1, 1];
let N = arr.length;
isPolygonPossible(arr, N);
</script>
|
Output
Polygon possible with side length 5
Time Complexity: O(sqrt(N)2)
Auxiliary Space: O(1)
Last Updated :
19 Sep, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...