- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find element position in given monotonic Sequence in C++
Concept
With respect of a given integer l and a monotonic increasing sequence −
f(m) = am + bm [log2(m)] + cm^3 where (a = 1, 2, 3, …), (b = 1, 2, 3, …), (c = 0, 1, 2, 3, …) Remember,here, [log2(m)] indicates, taking the log to the base 2 and round the value down.As a result of this,
if m = 1, the value is 0.
if m = 2-3, the value is 1.
if m = 4-7, the value is 2.
if m = 8-15, the value is 3.
Our task is to determine the value m such that f(m) = l, if l doesn’t belong to the sequence then we have to print 0.
It should be noted that values are in such a way that they can be represented in 64 bits and the three integers a, b and c do not exceed 100.
Input
a = 2, b = 1, c = 1, l = 12168587437017
Output
23001 f(23001) = 12168587437017
Input
a = 7, b = 3, c = 0, l = 119753085330
Output
1234567890
Methods
Using Naive Approach − With respect of given values of a, b, c, find values of f(m) for every value of m and compare it.
Using Efficient Approach − Implement Binary Search, select m = (min + max) / 2 where min and max are indicated as the minimum and maximum values possible for m then,
- If f(m) < l then increment m.
- If f(m) > l then decrement m.
- If f(m) = l then m is the required answer.
- Now repeat the above steps until and unless the required value is found or it is not possible in the sequence.
Example
// C++ implementation of the approach #include <iostream> #include <math.h> #define SMALL_N 1000000 #define LARGE_N 1000000000000000 using namespace std; // Shows function to return the value of f(m) for given values of a, b, c, m long long func(long long a1, long long b1, long long c1, long long m){ long long res1 = a1 * m; long long logVlaue1 = floor(log2(m)); res1 += b1 * m * logVlaue1; res1 += c1 * (m * m * m); return res1; } long long getPositionInSeries1(long long a1, long long b1, long long c1, long long l){ long long start1 = 1, end1 = SMALL_N; // Now if c is 0, then value of m can be in order of 10^15. // Now if c1!=0, then m^3 value has to be in order of 10^18 // so maximum value of m can be 10^6. if (c1 == 0) { end1 = LARGE_N; } long long ans1 = 0; // Now for efficient searching, implement binary search. while (start1 <= end1) { long long mid1 = (start1 + end1) / 2; long long val1 = func(a1, b1, c1, mid1); if (val1 == l) { ans1 = mid1; break; } else if (val1 > l) { end1 = mid1 - 1; } else { start1 = mid1 + 1; } } return ans1; } // Driver code int main(){ long long a1 = 2, b1 = 1, c1 = 1; long long l = 12168587437017; cout << getPositionInSeries1(a1, b1, c1, l)<<endl; long long a2 = 7, b2 = 3, c2 = 0; long long l1 = 119753085330; cout << getPositionInSeries1(a2, b2, c2, l1)<<endl; long long a3 = 6, b3 = 2, c3 = 1; long long l2 = 11975309533; cout << getPositionInSeries1(a3, b3, c3, l2)<<endl; return 0; }
Output
23001 1234567890 0