Number of solutions for x < y, where a <= x <= b and c <= y <= d and x, y are integers
Last Updated :
08 Jun, 2022
Given four integers a, b, c, d ( upto 10^6 ). The task is to Find the number of solutions for x < y, where a <= x <= b and c <= y <= d and x, y integers.
Examples:
Input: a = 2, b = 3, c = 3, d = 4
Output: 3
Input: a = 3, b = 5, c = 6, d = 7
Output: 6
Approach: Let’s iterate explicitly over all possible values of x. For one such fixed value of x, the problem reduces to how many values of y are there such that c <= y <= d and x = max(c, x + 1) and y <= d. Let’s assume that c <= d, otherwise, there are no valid values of y of course. It follows, that for a fixed x, there are d – max(c, x+1) + 1 valid values of y because the number of integers in a range [R1, R2] is given by R2 – R1 + 1.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int NumberOfSolutions( int a, int b, int c, int d)
{
int ans = 0;
for ( int i = a; i <= b; i++)
if (d >= max(c, i + 1))
ans += d - max(c, i + 1) + 1;
return ans;
}
int main()
{
int a = 2, b = 3, c = 3, d = 4;
cout << NumberOfSolutions(a, b, c, d);
return 0;
}
|
C
#include <stdio.h>
int max( int a, int b)
{
int max = a;
if (max < b)
max = b;
return max;
}
int NumberOfSolutions( int a, int b, int c, int d)
{
int ans = 0;
for ( int i = a; i <= b; i++)
if (d >= max(c, i + 1))
ans += d - max(c, i + 1) + 1;
return ans;
}
int main()
{
int a = 2, b = 3, c = 3, d = 4;
printf ( "%d" ,NumberOfSolutions(a, b, c, d));
return 0;
}
|
Java
import java.io.*;
class GFG
{
static int NumberOfSolutions( int a, int b,
int c, int d)
{
int ans = 0 ;
for ( int i = a; i <= b; i++)
if (d >= Math.max(c, i + 1 ))
ans += d - Math.max(c, i + 1 ) + 1 ;
return ans;
}
public static void main (String[] args)
{
int a = 2 , b = 3 , c = 3 , d = 4 ;
System.out.println(NumberOfSolutions(a, b, c, d));
}
}
|
Python 3
def NumberOfSolutions(a, b, c, d) :
ans = 0
for i in range (a, b + 1 ) :
if d > = max (c, i + 1 ) :
ans + = d - max (c, i + 1 ) + 1
return ans
if __name__ = = "__main__" :
a, b, c, d = 2 , 3 , 3 , 4
print (NumberOfSolutions(a, b, c, d))
|
C#
using System;
class GFG
{
static int NumberOfSolutions( int a, int b,
int c, int d)
{
int ans = 0;
for ( int i = a; i <= b; i++)
if (d >= Math.Max(c, i + 1))
ans += d - Math.Max(c, i + 1) + 1;
return ans;
}
public static void Main()
{
int a = 2, b = 3, c = 3, d = 4;
Console.WriteLine(NumberOfSolutions(a, b, c, d));
}
}
|
PHP
<?php
function NumberOfSolutions( $a , $b , $c , $d )
{
$ans = 0;
for ( $i = $a ; $i <= $b ; $i ++)
if ( $d >= max( $c , $i + 1))
$ans += $d - max( $c , $i + 1) + 1;
return $ans ;
}
$a = 2; $b = 3; $c = 3; $d = 4;
echo NumberOfSolutions( $a , $b , $c , $d );
?>
|
Javascript
<script>
function NumberOfSolutions(a, b, c, d)
{
let ans = 0;
for (let i = a; i <= b; i++)
if (d >= Math.max(c, i + 1))
ans += d - Math.max(c, i + 1) + 1;
return ans;
}
let a = 2, b = 3, c = 3, d = 4;
document.write(NumberOfSolutions(a, b, c, d));
</script>
|
Time Complexity: O(b-a) where a and b are the given integers.
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...