A rational is represented as p/qb, for example 2/3. Given a sorted array of rational numbers, how to search an element using Binary Search. Use of floating point arithmetic is not allowed.
Example:
Input: arr[] = {1/5, 2/3, 3/2, 13/2}
x = 3/2
Output: Found at index 2
We strongly recommend you to minimize your browser and try this yourself first.
To compare two rational numbers p/q and r/s, we can compare p*s with q*r.
C++
// C program for Binary Search for Rationalnal Numbers // without using floating point arithmetic #include <stdio.h> struct Rational { int p; int q; }; // Utility function to compare two Rationalnal numbers // 'a' and 'b'. It returns // 0 --> When 'a' and 'b' are same // 1 --> When 'a' is greater //-1 --> When 'b' is greate int compare(struct Rational a, struct Rational b) { // If a/b == c/d then a*d = b*c: // method to ignore division if (a.p * b.q == a.q * b.p) return 0; if (a.p * b.q > a.q * b.p) return 1; return -1; } // Returns index of x in arr[l..r] if it is present, else // returns -1. It mainly uses Binary Search. int binarySearch(struct Rational arr[], int l, int r, struct Rational x) { if (r >= l) { int mid = l + (r - l)/2; // If the element is present at the middle itself if (compare(arr[mid], x) == 0) return mid; // If element is smaller than mid, then it can // only be present in left subarray if (compare(arr[mid], x) > 0) return binarySearch(arr, l, mid-1, x); // Else the element can only be present in right // subarray return binarySearch(arr, mid+1, r, x); } return -1; } // Driver method int main() { struct Rational arr[] = {{1, 5}, {2, 3}, {3, 2}, {13, 2}}; struct Rational x = {3, 2}; int n = sizeof(arr)/sizeof(arr[0]); printf("Element found at index %d", binarySearch(arr, 0, n-1, x)); } |
chevron_right
filter_none
Java
// Java program for Binary Search for Rationalnal Numbers // without using floating point arithmetic class GFG { static class Rational { int p; int q; public Rational(int p, int q) { this.p = p; this.q = q; } }; // Utility function to compare two Rationalnal numbers // 'a' and 'b'. It returns // 0 -. When 'a' and 'b' are same // 1 -. When 'a' is greater //-1 -. When 'b' is greate static int compare(Rational a, Rational b) { // If a/b == c/d then a*d = b*c: // method to ignore division if (a.p * b.q == a.q * b.p) return 0; if (a.p * b.q > a.q * b.p) return 1; return -1; } // Returns index of x in arr[l..r] if it is present, else // returns -1. It mainly uses Binary Search. static int binarySearch(Rational arr[], int l, int r, Rational x) { if (r >= l) { int mid = l + (r - l)/2; // If the element is present at the middle itself if (compare(arr[mid], x) == 0) return mid; // If element is smaller than mid, then it can // only be present in left subarray if (compare(arr[mid], x) > 0) return binarySearch(arr, l, mid - 1, x); // Else the element can only be present in right // subarray return binarySearch(arr, mid + 1, r, x); } return -1; } // Driver method public static void main(String[] args) { Rational arr[] = {new Rational(1, 5), new Rational(2, 3), new Rational(3, 2), new Rational(13, 2)}; Rational x = new Rational(3, 2); int n = arr.length; System.out.printf("Element found at index %d", binarySearch(arr, 0, n - 1, x)); } } // This code is contributed by Rajput-Ji |
chevron_right
filter_none
C#
// C# program for Binary Search for Rationalnal Numbers // without using floating point arithmetic using System; class GFG { class Rational { public int p; public int q; public Rational(int p, int q) { this.p = p; this.q = q; } }; // Utility function to compare two Rationalnal numbers // 'a' and 'b'. It returns // 0 -. When 'a' and 'b' are same // 1 -. When 'a' is greater //-1 -. When 'b' is greate static int compare(Rational a, Rational b) { // If a/b == c/d then a*d = b*c: // method to ignore division if (a.p * b.q == a.q * b.p) return 0; if (a.p * b.q > a.q * b.p) return 1; return -1; } // Returns index of x in arr[l..r] if it is present, else // returns -1. It mainly uses Binary Search. static int binarySearch(Rational []arr, int l, int r, Rational x) { if (r >= l) { int mid = l + (r - l)/2; // If the element is present at the middle itself if (compare(arr[mid], x) == 0) return mid; // If element is smaller than mid, then it can // only be present in left subarray if (compare(arr[mid], x) > 0) return binarySearch(arr, l, mid - 1, x); // Else the element can only be present in right // subarray return binarySearch(arr, mid + 1, r, x); } return -1; } // Driver method public static void Main(String[] args) { Rational []arr = {new Rational(1, 5), new Rational(2, 3), new Rational(3, 2), new Rational(13, 2)}; Rational x = new Rational(3, 2); int n = arr.Length; Console.Write("Element found at index {0}", binarySearch(arr, 0, n - 1, x)); } } // This code is contributed by 29AjayKumar |
chevron_right
filter_none
Output:
Element found at index 2
Thanks to Utkarsh Trivedi for suggesting above solution.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
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.
Recommended Posts:
- Find max of two Rational numbers
- Find LCM of rational numbers
- Meta Binary Search | One-Sided Binary Search
- Unbounded Binary Search Example (Find the point where a monotonically increasing function becomes positive first time)
- as_integer_ratio() in Python for reduced fraction of a given rational
- Maximum rational number (or fraction) from an array
- Interpolation search vs Binary search
- Why is Binary Search preferred over Ternary Search?
- Linear Search vs Binary Search
- Sublist Search (Search a linked list in another list)
- Repeatedly search an element by doubling it after every successful search
- Best First Search (Informed Search)
- Longest Common Prefix using Binary Search
- Finding minimum vertex cover size of a graph using binary search
- Binary Search using pthread
- Find square root of number upto given precision using binary search
- Calculating n-th real root using binary search
- Create a Sorted Array Using Binary Search
- Longest substring with K unique characters using Binary Search
- Check if an array is sorted and rotated using Binary Search

