Given 5 integers say A, B, C, D, and E which represents the cubic equation
, the task is to find the integral solution for this equation. If there doesn’t exist any integral solution then print “NA”.
Examples:
Input: A = 1, B = 0, C = 0, D = 0, E = 27
Output: 3Input: A = 1, B = 0, C = 0, D = 0, E = 16
Output: NA
Approach: The idea is to use binary search. Below are the steps:
- Initialise the start and end variable as 0 & 105 respectively.
- Find the middle(say mid) value of start and end check if it satisfy the given equation or not.
- If current mid satisfy the given equation the print the mid value.
- Else if the value of f(x) is less than E then update start as mid + 1.
- Else Update end as mid – 1.
- If we can’t find any integral solution for the above equation then print “-1”.
Below is the implementation of the above approach:
C++
// C++ program for the above approach #include <bits/stdc++.h> using namespace std; // Function to find the value at x of // the given equation long long int check(int A, int B, int C, int D, long long int x) { long long int ans; // Find the value equation at x ans = (A * x * x * x + B * x * x + C * x + D); // Return the value of ans return ans; } // Function to find the integral // solution of the given equation void findSolution(int A, int B, int C, int D, int E) { // Initialise start and end int start = 0, end = 100000; long long int mid, ans; // Implement Binary Search while (start <= end) { // Find mid mid = start + (end - start) / 2; // Find the value of f(x) using // current mid ans = check(A, B, C, D, mid); // Check if current mid satisfy // the equation if (ans == E) { // Print mid and return cout << mid << endl; return; } if (ans < E) start = mid + 1; else end = mid - 1; } // Print "NA" if not found // any integral solution cout << "NA"; } // Driver Code int main() { int A = 1, B = 0, C = 0; int D = 0, E = 27; // Function Call findSolution(A, B, C, D, E); } |
Java
// Java program for the above approach import java.util.*; class GFG{ // Function to find the value at x of // the given equation static long check(int A, int B, int C, int D, long x) { long ans; // Find the value equation at x ans = (A * x * x * x + B * x * x + C * x + D); // Return the value of ans return ans; } // Function to find the integral // solution of the given equation static void findSolution(int A, int B, int C, int D, int E) { // Initialise start and end long start = 0, end = 100000; long mid, ans; // Implement Binary Search while (start <= end) { // Find mid mid = start + (end - start) / 2; // Find the value of f(x) using // current mid ans = check(A, B, C, D, mid); // Check if current mid satisfy // the equation if (ans == E) { // Print mid and return System.out.println(mid); return; } if (ans < E) start = mid + 1; else end = mid - 1; } // Print "NA" if not found // any integral solution System.out.println("NA"); } // Driver Code public static void main(String args[]) { int A = 1, B = 0, C = 0; int D = 0, E = 27; // Function Call findSolution(A, B, C, D, E); } } // This code is contributed by Code_Mech |
Python3
# Python3 program for the above approach # Function to find the value at x of # the given equation def check(A, B, C, D, x) : ans = 0; # Find the value equation at x ans = (A * x * x * x + B * x * x + C * x + D); # Return the value of ans return ans; # Function to find the integral # solution of the given equation def findSolution(A, B, C, D, E) : # Initialise start and end start = 0; end = 100000; mid = 0; ans = 0; # Implement Binary Search while (start <= end) : # Find mid mid = start + (end - start) // 2; # Find the value of f(x) using # current mid ans = check(A, B, C, D, mid); # Check if current mid satisfy # the equation if (ans == E) : # Print mid and return print(mid); return; if (ans < E) : start = mid + 1; else : end = mid - 1; # Print "NA" if not found # any integral solution print("NA"); # Driver Code if __name__ == "__main__" : A = 1; B = 0; C = 0; D = 0; E = 27; # Function Call findSolution(A, B, C, D, E); # This code is contributed by AnkitRai01 |
C#
// C# program for the above approach using System; class GFG{ // Function to find the value at x of // the given equation static long check(int A, int B, int C, int D, long x) { long ans; // Find the value equation at x ans = (A * x * x * x + B * x * x + C * x + D); // Return the value of ans return ans; } // Function to find the integral // solution of the given equation static void findSolution(int A, int B, int C, int D, int E) { // Initialise start and end long start = 0, end = 100000; long mid, ans; // Implement Binary Search while (start <= end) { // Find mid mid = start + (end - start) / 2; // Find the value of f(x) using // current mid ans = check(A, B, C, D, mid); // Check if current mid satisfy // the equation if (ans == E) { // Print mid and return Console.WriteLine(mid); return; } if (ans < E) start = mid + 1; else end = mid - 1; } // Print "NA" if not found // any integral solution Console.Write("NA"); } // Driver Code public static void Main() { int A = 1, B = 0, C = 0; int D = 0, E = 27; // Function Call findSolution(A, B, C, D, E); } } // This code is contributed by Code_Mech |
3
Time Complexity: O(log N)
Recommended Posts:
- Form the Cubic equation from the given roots
- Find the quadratic equation from the given roots
- Program to find the Roots of Quadratic equation
- Find initial integral solution of Linear Diophantine equation if finite solution exists
- Check if roots of a Quadratic Equation are reciprocal of each other or not
- Roots of the quadratic equation when a + b + c = 0 without using Shridharacharya formula
- Number of integral solutions of the equation x1 + x2 +.... + xN = k
- Absolute difference between sum and product of roots of a quartic equation
- Boundary Value Analysis : Nature of Roots of a Quadratic equation
- Number of non-negative integral solutions of sum equation
- Number of integral solutions for equation x = b*(sumofdigits(x)^a)+c
- Check if roots of a Quadratic Equation are numerically equal but opposite in sign or not
- Find cubic root of a number
- Find if two given Quadratic equations have common roots or not
- Find the number of primitive roots modulo prime
- Find integral points with minimum distance from given set of integers using BFS
- Find the missing value from the given equation a + b = c
- Find the number of solutions to the given equation
- Find count of numbers from 0 to n which satisfies the given equation for a value K
- Program to find number of solutions in Quadratic Equation
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


