Untitled

mail@pastecode.io avatar
unknown
plain_text
5 months ago
703 B
3
Indexable
#include <iostream>
#include <vector>
using namespace std;

int findFixedPoint(const vector<int>& arr) {
    int low = 0, high = arr.size() - 1;

    while (low <= high) {
        int mid = low + (high - low) / 2;
        if (arr[mid] == mid) {
            return mid;
        } else if (arr[mid] > mid) {
            high = mid - 1;
        } else {
            low = mid + 1;
        }
    }

    return -1; // No fixed point found
}

int main() {
    vector<int> arr = {-10, -5, 0, 3, 7};
    int fixedPoint = findFixedPoint(arr);
    if (fixedPoint != -1) {
        cout << "Fixed Point is " << fixedPoint << endl;
    } else {
        cout << "No Fixed Point found." << endl;
    }
    return 0;
}
Leave a Comment