Checking the validity of a two-integer equation
Last Updated :
11 Nov, 2023
Given two integers n and k, and an equation 2x + ky = n (1 ≤ k ≤ n). The task is to check whether the equation is valid equation if x and y are whole numbers.
Examples:
Input: n = 8, k = 7
Output: YES
Explanation: The equation is 2x + 7y = 8. The value of y = 0, and x = 4, is a valid answer, and hence the equation is a valid equation.
Input: n = 5, k = 2
Output: NO
Explanation: The equation is 2x + 2y = 5. On trying all values of x and y, we can say that, no value of x and y, satisfies the above equation.
Naive Approach: The basic way to solve the problem is as follows:
Rewrite the given equation as, x = (n – ky)/2, we have been given the values of n and k, hence, only y is the variable left on the RHS side. Initialize a variable as n, and decrement the value k from n, each time, and check whether that number is divisible by 2. If at any point the number is divisible by 2, then we have got a valid answer, else no valid answer.
Below is the implementation for the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void validEquation( int n, int k)
{
while (n >= 0) {
if (n % 2 == 0) {
break ;
}
n = n - k;
}
if (n >= 0) {
cout << "YES" << endl;
}
else {
cout << "NO" << endl;
}
}
int main()
{
int n = 8, k = 7;
validEquation(n, k);
return 0;
}
|
Java
import java.util.*;
public class Main {
public static void validEquation( int n, int k)
{
while (n >= 0 ) {
if (n % 2 == 0 ) {
break ;
}
n = n - k;
}
if (n >= 0 ) {
System.out.println( "YES" );
}
else {
System.out.println( "NO" );
}
}
public static void main(String[] args)
{
int n = 8 , k = 7 ;
validEquation(n, k);
n = 5 ;
k = 2 ;
validEquation(n, k);
}
}
|
Python3
def validEquation(n, k):
while n > = 0 :
if n % 2 = = 0 :
break
n - = k
if n > = 0 :
print ( "YES" )
else :
print ( "NO" )
n = 8
k = 7
validEquation(n, k)
|
C#
using System;
class GFG {
public static void validEquation( int n, int k)
{
while (n >= 0) {
if (n % 2 == 0) {
break ;
}
n = n - k;
}
if (n >= 0) {
Console.WriteLine( "YES" );
}
else {
Console.WriteLine( "NO" );
}
}
public static void Main( string [] args)
{
int n = 8, k = 7;
validEquation(n, k);
n = 5;
k = 2;
validEquation(n, k);
}
}
|
Javascript
function validEquation(n, k) {
while (n >= 0) {
if (n % 2 === 0) {
break ;
}
n = n - k;
}
if (n >= 0) {
console.log( "YES" );
} else {
console.log( "NO" );
}
}
const n = 8;
const k = 7;
validEquation(n, k);
|
Time Complexity: O(n)
Auxiliary Space: O(1)
Efficient Approach: To solve the problem follow the below idea:
This problem can be solved by some basic maths and even-odd observations. As discussed earlier, the equation can be written as x = (n-ky)/2, hence (n-ky) needs to be an even number, for a valid answer in x. It also means that when this equation is invalid if (n-ky) is an odd number. We know, that for the subtraction of two numbers to be odd, one term should be an even number and another term should be an odd number.
Illustration:
- Now, let us consider a case when n is odd, and k is even, for e.g. n = 5, and k = 2, the equation is x = (5-2y)/2, there is no value for y, for which we could have a valid equation.
- Now, we might be thinking of another case, when n is even, and k is odd, for e.g. n = 6, and k = 3, the equation is x = (6-3y)/2, but the equation gives a valid answer, if y = 0, x = 3, hence it’s a valid equation.
- After the above observations, we can say that, we will only get an invalid equation if n is odd and k is even, else the equation is always valid.
Below are the steps for the above approach:
- Check if n is odd and k is even, then it is an invalid equation and prints NO, else prints YES.
Below is the implementation for the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void validEquation( int n, int k)
{
if (n % 2 != 0 && k % 2 == 0) {
cout << "NO" << endl;
}
else {
cout << "YES" << endl;
}
}
int main()
{
int n = 8, k = 7;
validEquation(n, k);
return 0;
}
|
Java
import java.util.*;
public class Main {
static void validEquation( int n, int k)
{
if (n % 2 != 0 && k % 2 == 0 ) {
System.out.println( "NO" );
}
else {
System.out.println( "YES" );
}
}
public static void main(String[] args)
{
int n = 8 , k = 7 ;
validEquation(n, k);
n = 5 ;
k = 2 ;
validEquation(n, k);
}
}
|
Python3
def validEquation(n, k):
if n % 2 ! = 0 and k % 2 = = 0 :
print ( "NO" )
else :
print ( "YES" )
if __name__ = = "__main__" :
n = 8
k = 7
validEquation(n, k)
|
C#
using System;
public class Program
{
public static void Main( string [] args)
{
int n = 8;
int k = 7;
ValidEquation(n, k);
}
public static void ValidEquation( int n, int k)
{
if (n % 2 != 0 && k % 2 == 0)
{
Console.WriteLine( "NO" );
}
else
{
Console.WriteLine( "YES" );
}
}
}
|
Javascript
function validEquation(n, k) {
if (n % 2 !== 0 && k % 2 === 0) {
console.log( "NO" );
} else {
console.log( "YES" );
}
}
let n = 8, k = 7;
validEquation(n, k);
n = 5;
k = 2;
validEquation(n, k);
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Approach 3:
- The approach to check whether 2x + ky = n is a valid equation or not is to solve the equation for x and check if the obtained value of x is an integer or not.
- The equation can be rearranged as: x = (n – ky) / 2.
- If (n – ky) is even, then x will be an integer, otherwise not.
- Hence, the validity of the equation depends on the parity of (n – ky).
Here’s the codes for this approach:
C++
#include <bits/stdc++.h>
using namespace std;
void validEquation( int n, int k)
{
int x,y;
x = (n - k*y)/2;
if ((n-k*y)%2==0)
cout<< "YES" <<endl;
else
cout<< "NO" <<endl;
}
int main()
{
int n = 8, k =7;
validEquation(n, k);
return 0;
}
|
Java
import java.util.*;
public class ValidEquation {
public static void validEquation( int n, int k) {
int x, y = 0 ;
x = (n - k * y) / 2 ;
if ((n - k * y) % 2 == 0 )
System.out.println( "YES" );
else
System.out.println( "NO" );
}
public static void main(String[] args) {
int n = 8 , k = 7 ;
validEquation(n, k);
}
}
|
Python
def valid_equation(n, k):
y = 0
x = (n - k * y) / / 2
if (n - k * y) % 2 = = 0 :
print ( "YES" )
else :
print ( "NO" )
if __name__ = = "__main__" :
n = 8
k = 7
valid_equation(n, k)
|
C#
using System;
class Program
{
static void ValidEquation( int n, int k)
{
int y;
y = (n % 2 == 0) ? 0 : 1;
if ((n - k * y) % 2 == 0)
{
Console.WriteLine( "YES" );
}
else
{
Console.WriteLine( "NO" );
}
}
static void Main()
{
int n = 8, k = 7;
ValidEquation(n, k);
}
}
|
Javascript
function validEquation(n, k) {
let y = 0;
let x = (n - k * y) / 2;
if ((n - k * y) % 2 === 0) {
console.log( "YES" );
} else {
console.log( "NO" );
}
}
let n = 8;
let k = 7;
validEquation(n, k);
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...