Introduction
In this problem, we are given three integers ( X ), ( Y ), and ( K ). ( X ) is the answer we obtained, ( Y ) is the correct answer, and ( K ) is the maximum allowed difference. our task is to determine if our answer ( X ) is considered correct based on the condition that the absolute difference between ( X ) and ( Y ) should be at most ( K ).
Problem Statement
Given three integers ( X ), ( Y ), and ( K ), determine if the absolute difference between ( X ) and ( Y ) is less than or equal to ( K ). If it is, print “Yes”; otherwise, print “No”.
Approach
To solve this problem, follow these steps:
- Read Input: Read the three integers ( X ), ( Y ), and ( K ).
- Calculate Absolute Difference: Compute the absolute difference between ( X ) and ( Y ) using the
Math.abs()
function. - Compare with ( K ): Check if the absolute difference is less than or equal to ( K ).
- Print Result: Print “Yes” if the condition is satisfied, otherwise print “No”.
Time Complexity:
O(1) , since we are making few constant time arithmetic operations
Space Complexity:
O(1), since we are not using any extra space
` import java.util.Scanner;
public class Codechef {
public static void main(String args) {
Scanner scanner = new Scanner(System.in);
// Read input values
int X = scanner.nextInt();
int Y = scanner.nextInt();
int K = scanner.nextInt();
// Calculate the absolute difference
int difference = Math.abs(X - Y);
// Check if the difference is within the allowed range
if (difference <= K) {
System.out.println("Yes");
} else {
System.out.println("No");
}
scanner.close();
}
}