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:
$end = $mid – 1;
$start = $mid + 1;
// Driver code
$a = 2;
$b = 1;
$c = 1;
$k = 12168587437017;
print(getPositionInSeries($a, $b, $c, $k));
// This code is contributed by mits
- 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
- Count Derangements (Permutation such that no element appears in its original position)
- Find position of the given number among the numbers made of 4 and 7
- 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
- Find nth term of the Dragon Curve Sequence
- Find a peak element
- Find the element that appears once
- Find the only repetitive element between 1 to n-1
- Find maximum element of each row in a matrix
- Find k-th smallest element in given n ranges
- Find the odd appearing element in O(Log n) time