Find if a number is part of AP whose first element and difference are given

Given three non-negative integers a, d and x. Here, a is the first element, d is the difference of an AP (Arithmetic Progression). We need to find if x is part of the given AP or not.

Examples : 

Input : a = 1, d = 3, x = 7
Output : Yes
7 is part of given AP, 1 + 3 + 3 = 7

Input : a = 10, d = 0, x = 10
Output : Yes

Firstly, in case d = 0, we should output Yes if a = x else answer is No. For non-zero d, if x belongs to sequence x = a + n * d where n is non-negative integer, only if (n – a) / d is non-negative integer. 

C++




// C++ program to check if x exist
// or not in the given AP.
#include <bits/stdc++.h>
using namespace std;
  
// returns yes if exist else no.
bool isMember(int a, int d, int x)
{
  
    // If difference is 0, then x must
    // be same as a.
    if (d == 0)
        return (x == a);
  
    // Else difference between x and a
    // must be divisible by d.
    return ((x - a) % d == 0 && (x - a) / d >= 0);
}
  
// Driver code.
int main()
{
    int a = 1, x = 7, d = 3;
    if (isMember(a, d, x))
        cout << "Yes";
    else
        cout << "No";
    return 0;
}


Java




// Java program to check if x exist
// or not in the given AP.
class GFG {
  
    // returns yes if exist else no.
    static boolean isMember(int a, int d, int x)
    {
  
        // If difference is 0, then x must
        // be same as a.
        if (d == 0)
            return (x == a);
  
        // Else difference between x and a
        // must be divisible by d.
        return ((x - a) % d == 0 && (x - a) / d >= 0);
    }
  
    // Driver code.
    public static void main(String args[])
    {
        int a = 1, x = 7, d = 3;
        if (isMember(a, d, x))
            System.out.println("Yes");
        else
            System.out.println("No");
    }
}
  
// This code is contributed by Nikita Tiwari


Python3




# Python3 code to check if x exist 
# or not in the given AP.
  
def isMember(a, d, x):
      
    # If difference is 0, then x
    # must be same as a.
    if d == 0:
        return x == a
      
    # Else difference between x 
    # and a must be divisible by d.
    return ((x - a) % d == 0 and 
        int((x - a) / d) >= 0)
  
# Driver code
a = 1
x = 7
d = 3
  
if isMember(a, d, x):
    print( "Yes")
else:
    print("No")
  
# This code is contributed by "Abhishek Sharma 44"


C#




// C# program to check if x exist
// or not in the given AP.
using System;
class GFG {
  
    // returns yes if exist else no.
    static bool isMember(int a, int d, int x)
    {
        // If difference is 0, then x must
        // be same as a.
        if (d == 0)
            return (x == a);
  
        // Else difference between x and a
        // must be divisible by d.
        return ((x - a) % d == 0 && (x - a) / d >= 0);
    }
  
    // Driver code.
    public static void Main()
    {
        int a = 1, x = 7, d = 3;
        if (isMember(a, d, x))
            Console.WriteLine("Yes");
        else
            Console.WriteLine("No");
    }
}
  
// This code is contributed by vt_m.


PHP




<?php
// PHP program to check  
// if x exist or not in 
// the given AP.
  
// returns yes if exist 
// else no.
function isMember($a, $d, $x)
{
  
    // If difference is 0, then 
    // x must be same as a
    if ($d == 0)
    return ($x == $a);
      
// Else difference between x
// and a must be divisible by d. 
return (($x - $a) % $d == 0 && 
        ($x - $a) / $d >= 0);
}
  
// Driver code.
$a = 1; $x = 7; $d = 3;
if (isMember($a, $d, $x))
    echo "Yes";
else
    echo "No";
  
// This code is contributed by aj_36
?>


Javascript




<script>
    // Javascript program to check if x exist
    // or not in the given AP.
      
    // returns yes if exist else no.
    function isMember(a, d, x)
    {
  
        // If difference is 0, then x must
        // be same as a.
        if (d == 0)
            return (x == a);
  
        // Else difference between x and a
        // must be divisible by d.
        return ((x - a) % d == 0 && (x - a) / d >= 0);
    }
      
    let a = 1, x = 7, d = 3;
    if (isMember(a, d, x))
        document.write("Yes");
    else
        document.write("No");
      
    // This code is contributed by divyeshrabadiya07.
</script>


Output

Yes

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 zambiatek!

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Check Also
Close
Back to top button