Given a range, we need to check if is their any pairs in the segment whose GCD is divisible by k.
Examples:
Input : l=4, r=6, k=2
Output : YES
There are two numbers 4 and 6 whose GCD is 2 which is divisible by 2.
Input : l=3 r=5 k=4
Output : NO
There is no such pair whose gcd is divisible by 5.
Basically we need to count such numbers in range l to r such that they are divisible by k. Because if we choose any such two numbers then their gcd is also a multiple of k. Now if count of such numbers is greater than one then we can form pair, otherwise it’s impossible to form a pair (x, y) such that gcd(x, y) is divisible by k.
Below is the implementation of above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool Check_is_possible( int l, int r, int k)
{
int count = 0;
for ( int i = l; i <= r; i++) {
if (i % k == 0)
count++;
}
return (count > 1);
}
int main()
{
int l = 4, r = 12;
int k = 5;
if (Check_is_possible(l, r, k))
cout << "YES\n" ;
else
cout << "NO\n" ;
return 0;
}
|
Java
class GFG {
public boolean Check_is_possible( int l, int r,
int k) {
int count = 0 ;
for ( int i = l; i <= r; i++) {
if (i % k == 0 ) {
count++;
}
}
return (count > 1 );
}
public static void main(String[] args) {
GFG g = new GFG();
int l = 4 , r = 12 ;
int k = 5 ;
if (g.Check_is_possible(l, r, k)) {
System.out.println( "YES" );
} else {
System.out.println( "NO" );
}
}
}
|
Python3
def Check_is_possible(l, r, k):
count = 0 ;
for i in range (l, r + 1 ):
if (i % k = = 0 ):
count + = 1 ;
return (count > 1 );
l = 4 ;
r = 12 ;
k = 5 ;
if (Check_is_possible(l, r, k)):
print ( "YES" );
else :
print ( "NO" );
|
C#
using System;
class GFG
{
public bool Check_is_possible( int l, int r,
int k)
{
int count = 0;
for ( int i = l; i <= r; i++)
{
if (i % k == 0)
count++;
}
return (count > 1);
}
public static void Main()
{
GFG g = new GFG();
int l = 4, r = 12;
int k = 5;
if (g.Check_is_possible(l, r, k))
Console.WriteLine( "YES\n" );
else
Console.WriteLine( "NO\n" );
}
}
|
PHP
<?php
function Check_is_possible( $l , $r , $k )
{
$count = 0;
for ( $i = $l ; $i <= $r ; $i ++)
{
if ( $i % $k == 0)
$count ++;
}
return ( $count > 1);
}
$l = 4; $r = 12;
$k = 5;
if (Check_is_possible( $l , $r , $k ))
echo "YES\n" ;
else
echo "NO\n" ;
?>
|
Javascript
<script>
function Check_is_possible(l , r , k) {
var count = 0;
for (i = l; i <= r; i++) {
if (i % k == 0) {
count++;
}
}
return (count > 1);
}
var l = 4, r = 12;
var k = 5;
if (Check_is_possible(l, r, k)) {
document.write( "YES" );
} else {
document.write( "NO" );
}
</script>
|
Time Complexity: O(r – l + 1)
Auxiliary Space: O(1), since no extra space has been taken.
An efficient solution is based on the efficient approach discussed here.
C++
#include <bits/stdc++.h>
using namespace std;
int Check_is_possible( int l, int r, int k)
{
int div_count = (r / k) - (l / k);
if (l % k == 0)
div_count++;
return (div_count > 1);
}
int main()
{
int l = 30, r = 70, k = 10;
if (Check_is_possible(l, r, k))
cout << "YES\n" ;
else
cout << "NO\n" ;
return 0;
}
|
Java
class GFG {
static boolean Check_is_possible( int l, int r, int k) {
int div_count = (r / k) - (l / k);
if (l % k == 0 ) {
div_count++;
}
return (div_count > 1 );
}
public static void main(String[] args) {
int l = 30 , r = 70 , k = 10 ;
if (Check_is_possible(l, r, k)) {
System.out.println( "YES" );
} else {
System.out.println( "NO" );
}
}
}
|
Python3
def Check_is_possible(l, r, k):
div_count = (r / / k) - (l / / k)
if l % k = = 0 :
div_count + = 1
return div_count > 1
if __name__ = = "__main__" :
l, r, k = 30 , 70 , 10
if Check_is_possible(l, r, k) = = True :
print ( "YES" )
else :
print ( "NO" )
|
C#
using System;
public class GFG {
static bool Check_is_possible( int l, int r, int k) {
int div_count = (r / k) - (l / k);
if (l % k == 0) {
div_count++;
}
return (div_count > 1);
}
public static void Main() {
int l = 30, r = 70, k = 10;
if (Check_is_possible(l, r, k)) {
Console.WriteLine( "YES" );
} else {
Console.WriteLine( "NO" );
}
}
}
|
PHP
<?php
function Check_is_possible( $l , $r , $k )
{
$div_count = (int)( $r / $k ) -
(int)( $l / $k );
if ( $l % $k == 0)
$div_count ++;
return ( $div_count > 1);
}
$l = 30;
$r = 70;
$k = 10;
if (Check_is_possible( $l , $r , $k ))
echo "YES\n" ;
else
echo "NO\n" ;
?>
|
Javascript
<script>
function Check_is_possible(l , r , k)
{
var div_count = (r / k) - (l / k);
if (l % k == 0) {
div_count++;
}
return (div_count > 1);
}
var l = 30, r = 70, k = 10;
if (Check_is_possible(l, r, k)) {
document.write( "YES" );
} else {
document.write( "NO" );
}
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!