Given a range from L to R and every Xth tile is painted black and every Yth tile is painted white in that range from L to R. If a tile is painted both white and black, then it is considered to be painted grey. The task is to find the number of tiles that are colored grey in range L to R (both inclusive).Â
Examples:Â
Â
Input: X = 2, Y = 3, L = 6, R = 18 Output: 3 The grey coloured tiles are numbered 6, 12, 18 Input: X = 1, Y = 4, L = 5, R = 10 Output: 1 The only grey coloured tile is 8.
Â
Approach: Since every multiple of X is black and every multiple of Y is white. Any tile which is a multiple of both X and Y would be grey. The terms that are divisible by both X and Y are the terms that are divisible by the lcm of X and Y.
Lcm can be found out using the following formula:Â
Â
lcm = (x*y) / gcd(x, y)
GCD can be computed in logn time using Euclid’s algorithm. The number of multiples of lcm in range L to R can be found by using a common trick of:Â
Â
count(L, R) = count(R) - count(L-1)
Number of terms divisible by K less than N is:Â
Â
floor(N/K)
Below is the implementation to find the number of grey tiles:
Â
C++
// C++ implementation to find the number of// grey tiles#include <bits/stdc++.h>using namespace std;Â
// Function to count the number of grey tilesint findTileCount(int x, int y, int l, int r){Â Â Â Â int lcm = (x * y) / __gcd(x, y);Â
    // Number multiple of lcm less than L    int countl = (l - 1) / lcm;Â
    // Number of multiples of lcm less than R+1    int countr = r / lcm;    return countr - countl;}Â
// Driver codeint main(){Â Â Â Â int x = 2, y = 3, l = 6, r = 18;Â Â Â Â cout << findTileCount(x, y, l, r);Â Â Â Â return 0;} |
Java
// Java implementation to find the // number of grey tilesÂ
import java.io.*;Â
class GFG {Â Â Â Â // Function to count the number// of grey tiles static int findTileCount(int x, int y, Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â int l, int r) { Â Â Â Â int lcm = (x * y) / __gcd(x, y); Â
    // Number multiple of lcm less than L     int countl = (l - 1) / lcm; Â
    // Number of multiples of     // lcm less than R+1     int countr = r / lcm;     return countr - countl; } Â
static int __gcd(int a, int b) { Â Â Â Â Â Â Â Â Â // Everything divides 0 Â Â Â Â if (a == 0) Â Â Â Â return b; Â Â Â Â if (b == 0) Â Â Â Â return a; Â
    // base case     if (a == b)         return a; Â
    // a is greater     if (a > b)         return __gcd(a - b, b);              return __gcd(a, b - a); }Â
// Driver code               public static void main (String[] args) {Â
    int x = 2, y = 3, l = 6, r = 18;         System.out.println(findTileCount(x, y, l, r)); }} Â
// This code is contributed ajit |
Python3
# Python3 implementation to find the number of # grey tiles Â
# from math lib import gcd methodfrom math import gcdÂ
# Function to count the number of grey tiles def findTileCount(x, y, l, r) :Â
    lcm = (x * y) // gcd(x, y)Â
    # Number multiple of lcm less than L     count1 = (l - 1) // lcmÂ
    # Number of multiples of lcm less than R+1     countr = r // lcmÂ
    return countr - count1Â
Â
Â
# Driver codeif __name__ == "__main__" :Â
    x, y, l, r = 2, 3, 6, 18    print(findTileCount(x, y, l, r))Â
# This code is contributed by # ANKITRAI1 |
C#
// C# implementation to find the // number of grey tiles using System;Â
class GFG{Â Â Â Â Â // Function to count the number// of grey tiles static int findTileCount(int x, int y, Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â int l, int r) { Â Â Â Â int lcm = (x * y) / __gcd(x, y); Â
    // Number multiple of lcm less than L     int countl = (l - 1) / lcm; Â
    // Number of multiples of     // lcm less than R+1     int countr = r / lcm;     return countr - countl; } Â
static int __gcd(int a, int b) { Â Â Â Â Â Â Â Â Â // Everything divides 0 Â Â Â Â if (a == 0) Â Â Â Â return b; Â Â Â Â if (b == 0) Â Â Â Â return a; Â
    // base case     if (a == b)         return a; Â
    // a is greater     if (a > b)         return __gcd(a - b, b);              return __gcd(a, b - a); }Â
// Driver code public static void Main() { Â Â Â Â int x = 2, y = 3, l = 6, r = 18; Â Â Â Â Console.Write(findTileCount(x, y, l, r)); }} Â
// This code is contributed // by Kirti_Mangal |
PHP
<?php// PHP implementation to find the // number of grey tilesÂ
// Function to count the number// of grey tiles function findTileCount($x, $y, $l, $r) { Â Â Â Â $lcm = (int)(($x * $y) / __gcd($x, $y)); Â
    // Number multiple of lcm less than L     $countl = (int)(($l - 1) / $lcm); Â
    // Number of multiples of     // lcm less than R+1     $countr = (int)($r / $lcm);     return $countr - $countl; } Â
function __gcd($a, $b) { Â Â Â Â Â Â Â Â Â // Everything divides 0 Â Â Â Â if ($a == 0) Â Â Â Â return $b; Â Â Â Â if ($b == 0) Â Â Â Â return $a; Â
    // base case     if ($a == $b)         return $a; Â
    // a is greater     if ($a > $b)         return __gcd($a - $b, $b);              return __gcd($a, $b - $a); }Â
// Driver code $x = 2; $y = 3; $l = 6; $r = 18; echo findTileCount($x, $y, $l, $r); Â Â Â Â Â // This code is contributed// by Akanksha Rai(Abby_akku)?> |
Javascript
<script>Â
// JavaScript implementation to find the // number of grey tilesÂ
// Function to count the number// of grey tiles function findTileCount(x,y,l,r) { Â Â Â Â lcm = parseInt((x * y) / __gcd(x, y)); Â
    // Number multiple of lcm less than L     countl = parseInt((l - 1) / lcm); Â
    // Number of multiples of     // lcm less than R+1     countr = parseInt(r / lcm);     return countr - countl; } Â
function __gcd(a, b) { Â Â Â Â Â Â Â Â Â // Everything divides 0 Â Â Â Â if (a == 0) Â Â Â Â return b; Â Â Â Â if (b == 0) Â Â Â Â return a; Â
    // base case     if (a == b)         return a; Â
    // a is greater     if (a > b)         return __gcd(a - b, b);              return __gcd(a, b - a); }Â
// Driver code let x = 2;let y = 3;let l = 6; let r = 18; document.write(findTileCount(x, y, l, r)); Â
// This code is contributed by bobbyÂ
</script> |
3
Â
Time Complexity: O(log(min(x, y))), where x and y are two parameters of gcd.
Â
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!
