Size of longest Sequence having multiples of one of given two numbers
Last Updated :
27 Feb, 2023
Given two numbers A & B, the task is to find the length of the maximum sequence containing multiples of either A or B only from a sequence of all the multiples of A & B. Common multiples of A & B can not be included in either of the group.
Examples:
Input: A = 5, B = 7
Output: 2
Explanation: The sequence containing all multiples of 5 and 7 is: 5, 7, 10, 14, 15, 20, 21, 25, 28, 30, 35, 40, …… 3 multiples of 5 that are 30, 35, 40 are in continuous fashion, but 35 is a multiple of both 5 and 7 so it can be considered a multiple of other number 7. Thus multiples of 5 are separated. Also, 2 multiples of 5 that are 15, 20 come continuously. Thus maximum length is 2.
Input: A = 2, B = 3
Output: 1
Explanation: The sequence containing all multiples of 2 and 3 is: 2, 3, 4, 6, 8, 9, 10, 12, 14, 15, 16, 18, …… Here alternate numbers can be taken as multiple of each 2 and 3. 2, 4, 8, 10, 14… can be taken as multiple of 2 and 3, 6, 9, 12, 15…. can be taken as multiple of 3. Thus maximum length is 1.
Approach: Here 3 cases arise:
Case I: When A & B are equal. In such case, the maximum length is 1. Here alternate numbers can be taken as multiple of both A and B.
Case II: When one is the multiple of the other. Here the answer can be calculated using Mathematics. Let us understand this case with the help of an example.
Given: A = 2, B = 8, the sequence of multiples becomes: 2, 4, 6, 8, 10, 12, 14, 16, 18, …..
Here, In this case it can be observed that every fourth number is a common multiple which can be marked as a multiple of both 2 and 8. Thus, at max 3 multiples are in continuous fashion. The answer is the Quotient obtained by dividing the two numbers minus one.
Quotient = 8 / 2 = 4
Output: Quotient – 1 = 4 – 1 = 3
Case III: In all other cases the answer can be calculated using simple mathematics but here is a catch. Typically the answer looks to be the quotient obtained by dividing the bigger number by the smaller one. But If the remainder on dividing the bigger number by the smaller is greater than one, then there will be an interval where the answer will be one more than the quotient.
For example: A = 5, B = 17 the sequence of multiples becomes: 5, 10, 15, 17, 20, 25, 30, 34, 35, 40. 45, 50, 51
Here it can be seen that at most 4 numbers are in continuous fashion that are 35, 40, 45, 50, hence maximum length is 4. If bigger number is divided by the smaller number, 17 / 5 Quotient = 3, Remainder = 2. Now check if (big – (small * ans) >= 2) then, add 1 to the quotient i.e 3 + 1 = 4.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int longestSubarray( int A, int B)
{
int big = max(A, B);
int small = min(A, B);
if (big == small) {
return 1;
}
else if (big % small == 0) {
return big / small - 1;
}
else {
int ans = big / small;
if (big - (small * ans) >= 2) {
return ans + 1;
}
else {
return ans;
}
}
}
int main()
{
int a = 5;
int b = 7;
cout << longestSubarray(a, b) << endl;
return 0;
}
|
Java
import java.io.*;
class GFG {
public static void main(String[] args)
{
int a = 5 ;
int b = 7 ;
System.out.println(longestSubarray(a, b));
}
public static int longestSubarray( int A, int B)
{
int big = Math.max(A, B);
int small = Math.min(A, B);
if (big == small)
return 1 ;
else if (big % small == 0 )
return big / small - 1 ;
else {
int ans = big / small;
if (big - (small * ans) >= 2 )
return ans + 1 ;
else
return ans;
}
}
}
|
Python3
def longestSubarray(A: int , B: int ) - > int :
big = max (A, B)
small = min (A, B)
if big = = small:
return 1
elif big % small = = 0 :
return big / / small - 1
else :
ans = big / / small
if big - (small * ans) > = 2 :
return ans + 1
else :
return ans
if __name__ = = '__main__' :
a = 5
b = 7
print (longestSubarray(a, b))
|
C#
using System;
public class GFG {
static public void Main()
{
int a = 5;
int b = 7;
Console.WriteLine(longestSubarray(a, b));
}
static int longestSubarray( int A, int B)
{
int big = Math.Max(A, B);
int small = Math.Min(A, B);
if (big == small)
return 1;
else if (big % small == 0)
return big / small - 1;
else {
int ans = big / small;
if (big - (small * ans) >= 2)
return ans + 1;
else
return ans;
}
}
}
|
Javascript
function longestSubarray(A, B) {
let big = Math.max(A, B);
let small = Math.min(A, B);
if (big === small) {
return 1;
}
else if (big % small === 0) {
return Math.floor(big / small) - 1;
}
else {
let ans = Math.floor(big / small);
if (big - (small * ans) >= 2) {
return ans + 1;
} else {
return ans;
}
}
}
let a = 5;
let b = 7;
console.log(longestSubarray(a, b));
|
Time Complexity: O(1),
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...