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
- k-th missing element in increasing sequence which is not present in a given sequence
- Find a valid parenthesis sequence of length K from a given valid parenthesis sequence
- Check if the bracket sequence can be balanced with at most one change in the position of a bracket | Set 2
- Find position of an element in a sorted array of infinite numbers
- Find the position of the last removed element from the array
- Find position of the given number among the numbers made of 4 and 7
- Find the position of box which occupies the given ball
- Find the position of the given row in a 2-D array
- Find the position of the given Prime Number
- Find position of given term in a series formed with only digits 4 and 7 allowed
- Count Derangements (Permutation such that no element appears in its original position)
- Shuffle the position of each Array element by swapping adjacent elements
- Count of right shifts for each array element to be in its sorted position
- Find bitonic point in given bitonic sequence
- Program to find sum of the given sequence
- Find if the given number is present in the infinite sequence or not
- Find the final sequence of the array after performing given operations
- Find F(n) when F(i) and F(j) of a sequence are given
- Find sum in range L to R in given sequence of integers
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.