Find Range Value of the Expression
Given two integers L and R, the task is to calculate the value of the expression:

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.
Examples:
Input: L = 6, R = 12
Output: 0.09
Input: L = 5, R = 6
Output: 0.06
Approach: It can be observed that
.
So, ![]()
Hence, the answer will be (1 / L) – (1 / (R + 1)).
Below is the implementation of the above approach:
C++
// C++ implementation of the approach#include <bits/stdc++.h>using namespace std;// Function to return the value// of the given expressiondouble get(double L, double R){ // Value of the first term double x = 1.0 / L; // Value of the last term double y = 1.0 / (R + 1.0); return (x - y);}// Driver codeint main(){ int L = 6, R = 12; // Get the result double ans = get(L, R); cout << fixed << setprecision(2) << ans; return 0;} |
Java
// Java implementation of the approachimport java.util.*;class GFG{// Function to return the value// of the given expressionstatic double get(double L, double R){ // Value of the first term double x = 1.0 / L; // Value of the last term double y = 1.0 / (R + 1.0); return (x - y);}// Driver codepublic static void main(String []args){ int L = 6, R = 12; // Get the result double ans = get(L, R); System.out.printf( "%.2f", ans);}}// This code is contributed by Surendra_Gangwar |
Python3
# Python3 implementation of the approach# Function to return the value# of the given expressiondef get(L, R) : # Value of the first term x = 1.0 / L; # Value of the last term y = 1.0 / (R + 1.0); return (x - y);# Driver codeif __name__ == "__main__" : L = 6; R = 12; # Get the result ans = get(L, R); print(round(ans, 2));# This code is contributed by AnkitRai01 |
C#
// C# implementation of the approachusing System;public class GFG{ // Function to return the value// of the given expressionstatic double get(double L, double R){ // Value of the first term double x = 1.0 / L; // Value of the last term double y = 1.0 / (R + 1.0); return (x - y);} // Driver codepublic static void Main(String []args){ int L = 6, R = 12; // Get the result double ans = get(L, R); Console.Write( "{0:F2}", ans);}}// This code contributed by PrinciRaj1992 |
Javascript
<script>// JavaScript implementation of the approach// Function to return the value// of the given expressionfunction get(L, R){ // Value of the first term let x = 1.0 / L; // Value of the last term let y = 1.0 / (R + 1.0); return (x - y);}// Driver code let L = 6, R = 12; // Get the result let ans = get(L, R); document.write(Math.round(ans * 100) / 100);// This code is contributed by Surbhi Tyagi.</script> |
0.09
Time Complexity: O(1)


