tutorialcup@gmail.com

Find a Fixed point in a given sorted array

Arrays
Reverse Array
Pair Sum Equals Number
Floor and Ceil
Largest Sum Subarray
Missing Number
Odd Occuring Number
Sort by Frequency
First and Second Smallest
Majority Element
Difference between elements
Arrange Even and Odd
Closest Sum Elements
Distinct Elements
First Repeating Number
Common Elements in Arrays
Smallest Missing Number
Max Adjacent Sum
Find Occurrences of Number
Rotate Image
Distance Between Numbers
Pair with given difference
Product Array Puzzle
Replace Elements
First Repeating Element
Move Zeroes to End
Pythagorean Triplets
Maximum Average Subarray
Subarray Sum Equal to X
Leaders in Array
Largest Pair Sum
Segregate 0s 1s and 2s
Find Duplicates
Consecutive Elements Check
Triplet with given Sum
Binary Search in Sorted Array
Fixed point in sorted array
Reorder Elements by Indexes
Merging two sorted arrays
Find next greater number
Reorder array using given indexes
Count of triplets with sum less than given value
Merge two sorted arrays
Subarray and Subsequence
Rearrange array maximum minimum form
Find the lost element from a duplicated array
Count minimum steps to get the given array
Maximum element in an array which is increasing and then decreasing
Minimum number of jumps to reach the end of an array
Subarray with given sum
Length of Longest Increasing Subsequence
Smallest positive number missing unsorted array
The Celebrity Problem
Sorted subsequence of size 3
Partition Problem
Find pair with given difference
Maximum length of chain pairs
Four elements that sum to given
Maximum circular subarray sum
Count possible triangles
Longest Increasing Subsequence
Petrol Bunks Tour
Tug of War
Counting Sort
Maximum Repeating Number
Positive Negative Arrangement
Find a Peak element
Elements More Than n/k
Max Product Subsequence
Longest Bitonic subarray in an array
Number of smaller elements on right side
Implement two stacks in an array
Maximum sum increasing subsequence
Find the two numbers with odd occurrences in an unsorted array
Largest subarray with equal number of 0's and 1's
Maximum product subarray
Replace every element with the greatest on the right side in an array
Sorting a k sorted array
Find the row with maximum number of 1's
Shuffle a given array
Iterative Implementation of quick sort
Arrange given numbers to form the biggest number
Pancake sorting
Pancake sorting Problem
Maximum Subarray Sum using Divide and Conquer
Merge Overlapping Intervals
Stock Buy Sell to Maximize Profit
Sort Elements by frequency
Print all possible combinations of r elements in a given array of size n
Monotonically increasing function
Minimum element in a sorted and rotated array
Merge k Sorted Arrays
Flip Zeroes for Consecutive 1's
Least Average Subarray
Longest span with same sum in two binary arrays
Form minimum number from given sequence of D's and I's
Number of strictly increasing subarrays
Minimum difference between any two elements in an array
Number of pairs with given sum
Make Array Palindrome
Dynamic Programming, Longest Bitonic SubSequence

Given an array of n distinct elements which is sorted in ascending order, find the fixed point in the given array, where fixed point means the element value is same as the index

Example1

input :

arr[] = {0,4,8,2,9}

output :

0 is a fixed point in this array because value and index both are same which is 0.

Example 2

Input : {2,7,9,3,10,8}

Output : 3 is a fixed point in this array because value is 3 and index is 3.

Algorithm 1(Linear Search) :

Time Complexity : O(n)

1. Till the end of the array, for each element
    a. check if the element value is same as the index value, If true print the element.

C++ Program

#include <bits/stdc++.h>
using namespace std;

int main()
{
	int arr[] =  { -10, -5, 0, 3, 7 , 9 , 11 , 13}; //array
	int N = sizeof(arr)/sizeof(arr[0]); //size of array
	
	for(int i = 0; i < N; i++)
		{
			if(arr[i] == i)
				{
					cout << arr[i] << " is a fixed point in the array \n ";
					return 0;
				}
		}
	cout<<"No fixed point in the array \n";

	return 0;
}
Try It

Algorithm 2(Binary Search) :

Time Complexity : O(logn)

Assign the low variable and high variable to the starting and ending of the array
Till low is less than high
1. First check if the middle element is fixed point or not, if yes print the mid element.
    a. if the mid element value is less than the mid index. If yes, update low to mid +1
    b. if the mid element value is greater than the mid index. If yes, update high to mid -1

C++ Program

#include <bits/stdc++.h>
using namespace std;

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

int main()
{
	int arr[] =  { -10, -5, 0, 2, 3 , 4,  5,  7 , 11 , 13}; //array
	int N = sizeof(arr)/sizeof(arr[0]); //size of array
	
	int index = bin_search_fixed(arr,0,N-1);
	if(index >= 0)
		cout << arr[index] << " is a fixed point in the array \n ";
		
	else
		cout<<"No fixed point in the array \n";
	
	return 0;
}
Try It


C++ Online Test - Check Your Rank across the World


Next > < Prev
Scroll to Top