In this blog post, we tackle the “Relatively Prime” problem from LeetCode, offering a detailed solution using Java. Discover how to determine if two numbers are relatively prime with an efficient algorithm. We break down the problem and provide clear Java code to help you understand and implement the solution.

Introduction

In this blog post, we will explore a solution to the “Relatively Prime” problem from LeetCode using Java. This problem involves determining whether two given integers are relatively prime, meaning that their greatest common divisor (GCD) is 1.

image 22

Problem Statement

Given two integers, determine whether they are relatively prime. Two numbers are relatively prime if their greatest common divisor (GCD) is 1.

Keywords

Relatively Prime, Java, LeetCode, Algorithm, Number Theory, Coding Solutions, Java Programming, Euclidean Algorithm, GCD, Data Structures

Approach

To solve this problem, we can use the Euclidean algorithm to compute the GCD of the two numbers. If the GCD is 1, then the numbers are relatively prime; otherwise, they are not.

Steps to Solve the Problem

  1. Define a function to compute the GCD of two numbers using the Euclidean algorithm.
  2. Use the GCD function to check if the two given numbers are relatively prime.
  3. Return true if the GCD is 1, indicating the numbers are relatively prime; otherwise, return false.
image 23

Java Implementation

Here is the Java code to solve the “Relatively Prime” problem:

public class RelativelyPrime {
// Function to compute the GCD using the Euclidean algorithm
public static int gcd(int a, int b) {
if (b == 0) {
return a;
}
return gcd(b, a % b);
}

// Function to check if two numbers are relatively prime
public static boolean areRelativelyPrime(int num1, int num2) {
    return gcd(num1, num2) == 1;
}

public static void main(String[] args) {
    int num1 = 14;
    int num2 = 25;

    boolean result = areRelativelyPrime(num1, num2);
    System.out.println("Are " + num1 + " and " + num2 + " relatively prime? " + result);
}

}

image 24

Explanation

  • GCD Function: We define a recursive function gcd that computes the greatest common divisor of two numbers using the Euclidean algorithm. This function repeatedly replaces the larger number with the remainder of the division until one of the numbers becomes zero.
  • Relatively Prime Check: We define a function areRelativelyPrime that checks if the GCD of two numbers is 1. If it is, the numbers are relatively prime.
  • Main Method: In the main method, we test our functions with two example numbers, 14 and 25, and print whether they are relatively prime.

Conclusion

This approach efficiently solves the “Relatively Prime” problem using the Euclidean algorithm to compute the GCD. By determining if the GCD is 1, we can easily check if two numbers are relatively prime. This method ensures an optimal solution with a time complexity of O(log(min(a, b))), where a and b are the input numbers.

By following this approach, you can handle various pairs of integers and determine their relative primality with confidence.

Read More …

Solving the Square of a Rotated Array Problem – https://kamleshsingad.com/solving-the-square-of-a-rotated-array-problem/

Differences Between Arrays and Strings in Java – https://kamleshsingad.com/differences-between-arrays-and-strings-in-java/

Mastering Bit Manipulation: Squaring a Number Using Bitwise Operations in Java – https://kamleshsingad.com/mastering-bit-manipulation-squaring-a-number-using-bitwise-operations-in-java/

LEAVE A REPLY

Please enter your comment!
Please enter your name here