Find if the given number is present in the infinite sequence or not

Given three integers A, B and C. In an infinite sequence, A is the first number, C is the common difference (Si – Si – 1 = C). The task is to check if the number B will appear in the sequence or not.

Examples:

Input: A = 1, B = 7, C = 3
Output: Yes
The sequence will be 1, 4, 7, 10, …

Input: A = 1, B = -4, C = 5
Output: No

Approach: There are two cases:

  1. When C = 0, print Yes if A = B else No as the sequence will consist only the number A
  2. When C > 0, for any non-negative integer k the equation B = A + k * C must be satisfied i.e. (B – A) / C must be a non-negative integer.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
  
// Function that returns true if
// the sequence will contain B
bool doesContainB(int a, int b, int c)
{
    if (a == b)
        return true;
  
    if ((b - a) * c > 0 && (b - a) % c == 0)
        return true;
  
    return false;
}
  
// Driver code
int main()
{
    int a = 1, b = 7, c = 3;
  
    if (doesContainB(a, b, c))
        cout << "Yes";
    else
        cout << "No";
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of the approach
class GFG 
{
  
    // Function that returns true if 
    // the sequence will contain B 
    static boolean doesContainB(int a, int b, int c) 
    {
        if (a == b) 
        {
            return true;
        }
  
        if ((b - a) * c > 0 && (b - a) % c == 0
        {
            return true;
        }
  
        return false;
    }
  
    // Driver code 
    public static void main(String[] args) 
    {
        int a = 1, b = 7, c = 3;
  
        if (doesContainB(a, b, c)) 
        {
            System.out.println("Yes");
        
        else 
        {
            System.out.println("No");
        }
    }
}
  
// This code contributed by Rajput-Ji

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 implementation of the approach
  
# Function that returns true if
# the sequence will contain B
def doesContainB(a, b, c):
    if (a == b):
        return True
  
    if ((b - a) * c > 0 and (b - a) % c == 0):
        return True
  
    return False
  
# Driver code
if __name__ == '__main__':
    a, b, c = 1, 7, 3
  
    if (doesContainB(a, b, c)):
        print("Yes")
    else:
        print("No")
  
# This code is contributed by 29AjayKumar

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation of the approach
using System;
  
class GFG 
{
  
    // Function that returns true if 
    // the sequence will contain B 
    static bool doesContainB(int a, int b, int c) 
    {
        if (a == b) 
        {
            return true;
        }
  
        if ((b - a) * c > 0 && (b - a) % c == 0) 
        {
            return true;
        }
  
        return false;
    }
  
    // Driver code 
    public static void Main() 
    {
        int a = 1, b = 7, c = 3;
  
        if (doesContainB(a, b, c)) 
        {
            Console.WriteLine("Yes");
        
        else
        {
            Console.WriteLine("No");
        }
    }
}
  
/* This code contributed by PrinciRaj1992 */

chevron_right


PHP

0 &&
($b – $a) % $c == 0)
return true;

return false;
}

// Driver code
$a = 1; $b = 7; $c = 3;

if (doesContainB($a, $b, $c))
echo “Yes”;
else
echo “No”;

// This code is contributed
// by Akanksha Rai
?>

Output:

Yes


My Personal Notes arrow_drop_up

pawanasipugmailcom

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.





Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.