Given an integer k and a monotonic increasing sequence:
f(n) = an + bn [log2(n)] + cn^3 where (a = 1, 2, 3, …), (b = 1, 2, 3, …), (c = 0, 1, 2, 3, …)
Here, [log2(n)] means, taking the log to the base 2 and round the value down Thus,
if n = 1, the value is 0.
if n = 2-3, the value is 1.
if n = 4-7, the value is 2.
if n = 8-15, the value is 3.
The task is to find the value n such that f(n) = k, if k doesn’t belong to the sequence then print 0.
Note: Values are in such a way that they can be expressed in 64 bits and the three integers a, b and c do not exceed 100.
Input: a = 2, b = 1, c = 1, k = 12168587437017
f(23001) = 12168587437017
Input: a = 7, b = 3, c = 0, k = 119753085330
Naive Approach: Given values of a, b, c, find values of f(n) for every value of n and compare it.
Efficient Approach: Use Binary Search, choose n = (min + max) / 2 where min and max are the minimum and maximum values possible for n then,
- If f(n) < k then increment n.
- If f(n) > k then decrement n.
- If f(n) = k then n is the required answer.
- Repeat the above steps until the required value is found or it is not possible in the sequence.
Below is the implementation of the above approach:
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.
- Find the Kth position element of the given sequence
- Find the position of the last removed element from the array
- Find position of an element in a sorted array of infinite numbers
- k-th missing element in increasing sequence which is not present in a given sequence
- Check if the bracket sequence can be balanced with at most one change in the position of a bracket | Set 2
- Count of right shifts for each array element to be in its sorted position
- Count Derangements (Permutation such that no element appears in its original position)
- Shuffle the position of each Array element by swapping adjacent elements
- Find the position of the given row in a 2-D array
- Find the position of the given Prime Number
- Find the position of box which occupies the given ball
- Find the value at kth position in the generated array
- Find out the correct position of the ball after shuffling
- Find position of the given number among the numbers made of 4 and 7
- Find position of given term in a series formed with only digits 4 and 7 allowed
- Find F(n) when F(i) and F(j) of a sequence are given
- Program to find sum of the given sequence
- Find n-th term in sequence 1, 1, 2, 1, 2, 3, 1, 2, 3, 4, ....
- Find Recurring Sequence in a Fraction
- Program to find Nth term of the Van Eck's Sequence
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.