Check if chords of a Circle are symmetric after some rotation
Last Updated : 23 Jul, 2025
Given two Integers N and M, N indicating equidistant points on circumference of a circle and M indicating number of chords formed with those points. Also given is a vector of pairs C containing position of chords. The task is to rotate the circle by any degree, say X, where 0 < X < 360, and check if the chords of are still symmetric to the original circle.
Example:
Input: N = 12, M = 6, C = {{1, 3}, {3, 7}, {5, 7}, {7, 11}, {9, 11}, {11, 3}}; Output: YES
OriginalAfter Rotation
Input: N = 10, M = 3, C = {{1, 2}, {3, 2}, {7, 2}} Output: NO
No rotational symmetry possible
Naive Approach: Rotate for every distance K in the range[1, N] and check for each point [a, b] if the rotated point [a + K, b + K] exits. If there exists any k then print YES else print NO Time Complexity: O(N*M)
Efficient Approach: It is enough to check for the divisors of N. Let us suppose if we rotate the image by K units then the whole image will be divided into N/K blocks. Then if K is not a divisor of N, there will be an asymmetric block of length less than K and the image will never be symmetric to the original figure. So calculate all the divisors of N and check for each chord the rotated chord exists or not.
Below is the implementation of the above approach:
C++
// C++ Program to for the above approach#include<bits/stdc++.h>usingnamespacestd;// Utility function to calculate// divisors of a number in O(sqrt(N))vector<int>calculateDivisors(intN){vector<int>div;for(inti=1;i*i<=N;i++){if(N%i==0){div.push_back(i);if(N/i!=i&&i!=1){div.push_back(N/i);}}}returndiv;}intcheckRotationallySymmetric(vector<pair<int,int>>A,intN,intM){// Maintain a set to check quickly// the presence of a chordset<pair<int,int>>st;for(inti=0;i<M;i++){--A[i].first,--A[i].second;if(A[i].first>A[i].second){swap(A[i].first,A[i].second);}st.insert(A[i]);}// Calculate the divisors of N.vector<int>div=calculateDivisors(N);// Iterate through the divisorsfor(autox:div){boolexist=1;for(inti=0;i<M;i++){intdx=(A[i].first+x)%N;intdy=(A[i].second+x)%N;if(dx>dy){swap(dx,dy);}if(st.find({dx,dy})!=st.end()){// There exists a valid// chord after rotation}else{// There is no valid chord after rotationexist=false;break;}}// if there exist another chord after// rotation for every other chord print// YES and exit the functionif(exist){cout<<"YES";return0;}}cout<<"NO";return0;}// Driver Codeintmain(){intN=12,M=6;vector<pair<int,int>>C={{1,3},{3,7},{5,7},{7,11},{9,11},{11,3}};checkRotationallySymmetric(C,N,M);return0;}
Java
importjava.util.ArrayList;importjava.util.HashSet;importjava.util.List;importjava.util.Set;classGFG{// function to calculate divisorsstaticList<Integer>calculateDivisors(intN){List<Integer>div=newArrayList<>();// checking all possible divisorsfor(inti=1;i*i<=N;i++){if(N%i==0){div.add(i);if(N/i!=i&&i!=1){div.add(N/i);}}}returndiv;}staticintcheckRotationallySymmetric(List<int[]>A,intN,intM){// Maintain a set to check quickly// the presence of a chordSet<String>st=newHashSet<>();for(inti=0;i<M;i++){A.get(i)[0]=A.get(i)[0]-1;A.get(i)[1]=A.get(i)[1]-1;if(A.get(i)[0]>A.get(i)[1]){inttemp=A.get(i)[0];A.get(i)[0]=A.get(i)[1];A.get(i)[1]=temp;}st.add(A.get(i)[0]+" "+A.get(i)[1]);}// Calculate the divisors of N.List<Integer>div=calculateDivisors(N);// Iterate through the divisorsfor(intx:div){booleanexist=true;for(inti=0;i<M;i++){intdx=(A.get(i)[0]+x)%N;intdy=(A.get(i)[1]+x)%N;if(dx>dy){inttemp=dx;dx=dy;dy=temp;}if(st.contains(dx+" "+dy)){// There exists a valid chord after// rotation}else{// There is no valid chord after// rotationexist=false;break;}}// if there exist another chord after rotation// for every other chord print YES and exit the// functionif(exist){System.out.println("YES");return0;}}System.out.println("NO");return0;}// Driver Codepublicstaticvoidmain(String[]args){intN=12,M=6;List<int[]>C=newArrayList<>();C.add(newint[]{1,3});C.add(newint[]{3,7});C.add(newint[]{5,7});C.add(newint[]{7,11});C.add(newint[]{9,11});C.add(newint[]{11,3});// function callcheckRotationallySymmetric(C,N,M);}}// This code is contributed by phasing17
Python3
# Python3 program to implement the approachfromtypingimportList,TupledefcalculateDivisors(N:int)->List[int]:""" Utility function to calculate divisors of a number in O(sqrt(N)) """div=[]foriinrange(1,int((N**0.5)+1)):ifN%i==0:div.append(i)ifN//i!=iandi!=1:div.append(N//i)returndivdefcheckRotationallySymmetric(A:List[Tuple[int,int]],N:int,M:int)->None:""" Main function to check rotationally symmetric """# Maintain a set to check quickly# the presence of a chordst=set()foriinrange(M):A[i]=(A[i][0]-1,A[i][1]-1)ifA[i][0]>A[i][1]:A[i]=(A[i][1],A[i][0])st.add(tuple(A[i]))# Calculate the divisors of N.div=calculateDivisors(N)# Iterate through the divisorsforjinrange(len(div)):exist=Trueforiinrange(M):dx=(A[i][0]+div[j])%Ndy=(A[i][1]+div[j])%Nifdx>dy:# swapping dx and dy. temp=dxdx=dydy=tempiftuple((dx,dy))inst:# There exists a valid chord after rotationpasselse:# There is no valid chord after rotationexist=Falsebreak# if there exist another chord after rotation for every other chord print YES and exit the functionifexist:print("YES")returnprint("NO")return# Driver codeN=12M=6C=[(1,3),(3,7),(5,7),(7,11),(9,11),(11,3)]checkRotationallySymmetric(C,N,M)# This code is contributed by phasing17
C#
// C# code to implement the above approachusingSystem;usingSystem.Collections.Generic;classGFG{// function to calculate divisorsstaticList<int>calculateDivisors(intN){List<int>div=newList<int>();// checking all possible divisorsfor(inti=1;i*i<=N;i++){if(N%i==0){div.Add(i);if(N/i!=i&&i!=1){div.Add(N/i);}}}returndiv;}staticintcheckRotationallySymmetric(List<Tuple<int,int>>A,intN,intM){// Maintain a set to check quickly// the presence of a chordHashSet<Tuple<int,int>>st=newHashSet<Tuple<int,int>>();for(inti=0;i<M;i++){A[i]=newTuple<int,int>(A[i].Item1-1,A[i].Item2-1);if(A[i].Item1>A[i].Item2){A[i]=newTuple<int,int>(A[i].Item2,A[i].Item1);}st.Add(A[i]);}// Calculate the divisors of N.List<int>div=calculateDivisors(N);// Iterate through the divisorsforeach(varxindiv){boolexist=true;for(inti=0;i<M;i++){intdx=(A[i].Item1+x)%N;intdy=(A[i].Item2+x)%N;if(dx>dy){Tuple<int,int>temp=newTuple<int,int>(dy,dx);dx=temp.Item1;dy=temp.Item2;}if(st.Contains(newTuple<int,int>(dx,dy))){// There exists a valid// chord after rotation}else{// There is no valid chord after// rotationexist=false;break;}}// if there exist another chord after// rotation for every other chord print// YES and exit the functionif(exist){Console.WriteLine("YES");return0;}}Console.WriteLine("NO");return0;}// Driver CodestaticvoidMain(string[]args){intN=12,M=6;List<Tuple<int,int>>C=newList<Tuple<int,int>>{newTuple<int,int>(1,3),newTuple<int,int>(3,7),newTuple<int,int>(5,7),newTuple<int,int>(7,11),newTuple<int,int>(9,11),newTuple<int,int>(11,3)};// function callcheckRotationallySymmetric(C,N,M);}}// This code is contributed by phasing17
JavaScript
// JavaScript Program to for the above approach// Utility function to calculate// divisors of a number in O(sqrt(N))functioncalculateDivisors(N){letdiv=newArray();for(leti=1;i*i<=N;i++){if(N%i==0){div.push(i);if(Math.floor(N/i)!=i&&i!=1){div.push(Math.floor(N/i));}}}returndiv;}functioncheckRotationallySymmetric(A,N,M){// Maintain a set to check quickly// the presence of a chordletst=newSet();for(leti=0;i<M;i++){A[i][0]=A[i][0]-1;A[i][1]=A[i][1]-1;if(A[i][0]>A[i][1]){lettemp=A[i][0];A[i][0]=A[i][1];A[i][1]=temp;}st.add(A[i].join(''));}// Calculate the di ors of N.letdiv=calculateDivisors(N);// Iterate through the divisorsfor(letj=0;j<div.length;j++){letexist=true;for(leti=0;i<M;i++){letdx=(A[i][0]+div[j])%N;letdy=(A[i][1]+div[j])%N;if(dx>dy){// swapping dx and dy. lettemp=dx;dx=dy;dy=temp;}if(st.has([dx,dy].join(''))){// There exists a valid// chord after rotation}else{// console.log([dx, dy]);// There is no valid chord after rotationexist=false;break;}}// if there exist another chord after// rotation for every other chord print// YES and exit the functionif(exist){console.log("YES");return0;}}console.log("NO");return0;}// Driver CodeletN=12,M=6;letC=[[1,3],[3,7],[5,7],[7,11],[9,11],[11,3]];checkRotationallySymmetric(C,N,M);// The code is contributed by Gautam goel (gautamgoel962)
Output
YES
Time Complexity: O(M*sqrt(N)*log M) Space Complexity: O(M)