Given an array arr[] consisting of N integers such that each pair of points (x, y) represents the endpoints of the semicircle as (x, 0) and (y, 0), the task is to check if any pair of semicircles intersect or not. If found to be true, then print “Yes”. Otherwise, print “No”.
Examples:
Input: arr[] = {0, 15, 5, 10} Output: No
Input: arr[] = {0, 10, 5, 15} Output: Yes
Approach: The given problem can be solved by checking if any pairs of semicircles intersect or not and then print the elements accordingly. Follow the steps below to solve the problem:
- Store all the possible semicircles in a vector with their corresponding x coordinates and y coordinates.
- Now, generate all possible pairs of the above vectors and perform the following steps:
- Let the pair of coordinates be X and Y and check if the circles intersect or not using the following conditions:
- If the value of (X[0] < Y[0], X[1] < Y[1] and Y[0] < X[2]) or (Y[0] < X[0], Y[1] < X[1], and X[0] < Y[1]), then the semicircle intersects. Otherwise, it doesn’t intersect.
- If the above two circles intersect, then print “Yes” and break out of the loop.
- After completing the above steps, if any pair of circles doesn’t intersect, then print “No”.
C++
#include <iostream>
#include <vector>
using namespace std;
bool checkIntersection( int arr[], int N)
{
vector<pair< int , int > > vec;
for ( int i = 0; i < N - 1; i++) {
int x = arr[i], y = arr[i + 1];
int minn = min(x, y);
int maxx = max(x, y);
vec.push_back({ minn, maxx });
}
for ( int i = 0; i < vec.size(); i++) {
pair< int , int > x = vec[i];
for ( int j = 0;
j < vec.size(); j++) {
pair< int , int > y = vec[j];
bool cond1
= (x.first < y.first
and x.second < y.second
and y.first < x.second);
bool cond2
= (y.first < x.first
and y.second < x.second
and x.first < y.second);
if (cond1 or cond2) {
return true ;
}
}
}
return false ;
}
int main()
{
int arr[] = { 0, 15, 5, 10 };
int N = sizeof (arr) / sizeof ( int );
if (checkIntersection(arr, N))
cout << "Yes" ;
else
cout << "No" ;
return 0;
}
|
Java
import java.util.*;
class GFG{
static class pair
{
int first, second;
public pair( int first, int second)
{
this .first = first;
this .second = second;
}
}
static boolean checkIntersection( int arr[], int N)
{
Vector<pair > vec = new Vector<>();
for ( int i = 0 ; i < N - 1 ; i++)
{
int x = arr[i], y = arr[i + 1 ];
int minn = Math.min(x, y);
int maxx = Math.max(x, y);
vec.add( new pair( minn, maxx ));
}
for ( int i = 0 ; i < vec.size(); i++)
{
pair x = vec.elementAt(i);
for ( int j = 0 ;
j < vec.size(); j++)
{
pair y = vec.elementAt(j);
boolean cond1 = (x.first < y.first &&
x.second < y.second &&
y.first < x.second);
boolean cond2 = (y.first < x.first &&
y.second < x.second &&
x.first < y.second);
if (cond1 || cond2)
{
return true ;
}
}
}
return false ;
}
public static void main(String[] args)
{
int arr[] = { 0 , 15 , 5 , 10 };
int N = arr.length;
if (checkIntersection(arr, N))
System.out.print( "Yes" );
else
System.out.print( "No" );
}
}
|
Python3
def checkIntersection(arr, N):
vec = []
for i in range (N - 1 ):
x = arr[i]
y = arr[i + 1 ]
minn = min (x, y)
maxx = max (x, y)
vec.append([minn, maxx])
for i in range ( len (vec)):
x = vec[i]
for j in range ( len (vec)):
y = vec[j]
cond1 = (x[ 0 ] < y[ 0 ] and
x[ 1 ] < y[ 1 ] and
y[ 0 ] < x[ 1 ])
cond2 = (y[ 0 ] < x[ 0 ] and
y[ 1 ] < x[ 1 ] and
x[ 0 ] < y[ 1 ])
if (cond1 or cond2):
return True
return False
if __name__ = = '__main__' :
arr = [ 0 , 15 , 5 , 10 ]
N = len (arr)
if (checkIntersection(arr, N)):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
using System.Collections.Generic;
class GFG{
class pair
{
public int first, second;
public pair( int first, int second)
{
this .first = first;
this .second = second;
}
}
static bool checkIntersection( int []arr, int N)
{
List<pair > vec = new List<pair>();
for ( int i = 0; i < N - 1; i++)
{
int x = arr[i], y = arr[i + 1];
int minn = Math.Min(x, y);
int maxx = Math.Max(x, y);
vec.Add( new pair( minn, maxx ));
}
for ( int i = 0; i < vec.Count; i++)
{
pair x = vec[i];
for ( int j = 0;
j < vec.Count; j++)
{
pair y = vec[j];
bool cond1 = (x.first < y.first &&
x.second < y.second &&
y.first < x.second);
bool cond2 = (y.first < x.first &&
y.second < x.second &&
x.first < y.second);
if (cond1 || cond2)
{
return true ;
}
}
}
return false ;
}
public static void Main(String[] args)
{
int []arr = { 0, 15, 5, 10 };
int N = arr.Length;
if (checkIntersection(arr, N))
Console.Write( "Yes" );
else
Console.Write( "No" );
}
}
|
Javascript
<script>
function checkIntersection(arr, N)
{
let vec = [];
for (let i = 0; i < N - 1; i++) {
let x = arr[i], y = arr[i + 1];
let minn = Math.min(x, y);
let maxx = Math.max(x, y);
vec.push([ minn, maxx ]);
}
for (let i = 0; i < vec.length; i++) {
let x = vec[i];
for (let j = 0;j < vec.length; j++) {
let y = vec[j];
let cond1 = (x[0] < y[0]
&& x[1] < y[1]
&& y[0] < x[1]);
let cond2 = (y[0] < x[0]
&& y[1] < x[1]
&& x[0] < y[1]);
if (cond1 || cond2) {
return true ;
}
}
}
return false ;
}
let arr = [ 0, 15, 5, 10 ];
let N = arr.length;
if (checkIntersection(arr, N))
document.write( "Yes" );
else
document.write( "No" );
</script>
|
Time Complexity: O(N2) Auxiliary Space: O(N)
|