# Python Program to Find a Fixed Point in a given Array or List

An array is a type of variable that can hold several values at once.

One element in a fixed point array is given as if its value is the same as its index. If a value is present, the code will return it; otherwise, it will return -1. We have an array of x unique numbers sorted in ascending order in this. In the following code, we create a function that returns a fixed point integer and returns -1 if there is no fixed point integer. The fixed point index is defined as an index I such that array[i] equals i.

Given a list, the task is to find a fixed point in the given list in Python if the fixed point is present in the given list then print -1.

Examples:

Example1:

Input:

Given List = [19, 24, 25, 3, 81, 144, 600, 900, 225, 4, 9, 1, 16, 49, 23, 49, 25, 10, 25]

Output:

The Fixed point present in the given list is [ 3 ]

Example2:

Input:

Given list = [7 , 8, 45, 19, 21, 23, 6 ,7, 5,]

Output:

The Fixed point present in the given list is [ 6 ]

## Program to Find a Fixed Point in a given Array or ListÂ Python

Below are the ways to find a Fixed Point in the given list in Python.

Grab the opportunity to learn all effective java programming language concepts from basic to advance levels by practicing these Java Program Examples with Output

### Method #1: Using For Loop (Static Input)

Approach:

• Give the list as static input and store it in a variable.
• Create a function findFixed() which accepts the given list as an argument and returns -1 if the list does not contain a fixed point else print the fixed point.
• Pass the given list as an argument to the findFixed() function.
• Inside the findFixed() function calculate the length of the given list.
• Loop till length of the given list using For loop.
• Check if the element is equal to the index(iterator value of the For loop).
• If it is true then return index.
• Return -1 after the end of the For loop.
• Print the Fixed-point Index
• The Exit of the Program.

Below is the implementation:

# Create a function findFixed() which accepts the given list
# as an argument and returns -1
# if the list does not contain a fixed point else print the fixed point.

def findFixed(gvnlst):
# Inside the findFixed() function calculate the length of the given list.
lstleng = len(gvnlst)
# Loop till length of the given list using For loop.
for m in range(lstleng):
# Check if the element is equal to the index(iterator value of the For loop).
if gvnlst[m] is m:
# If it is true then return index.
return m
# Return -1 after the end of the For loop.
return -1

# Give the list as static input and store it in a variable.
gvnlst = [19, 24, 25, 3, 81, 144, 600, 900,
225, 4, 9, 1, 16, 49, 23, 49, 25, 10, 25]
# Pass the given list as an argument to the findFixed() function.
fxdpoint = findFixed(gvnlst)
print('The Fixed point present in the given list is [', fxdpoint, ']')


Output:

The Fixed point present in the given list is [ 3 ]

### Method #2: Using For Loop (User Input)

Approach:

• Give the list as user input using list(),map(),input(),and split() functions.
• Store it in a variable.
• Create a function findFixed() which accepts the given list as an argument and returns -1 if the list does not contain a fixed point else print the fixed point.
• Pass the given list as an argument to the findFixed() function.
• Inside the findFixed() function calculate the length of the given list.
• Loop till the length of the given list using For loop.
• Check if the element is equal to the index(iterator value of the For loop).
• If it is true then return index.
• Return -1 after the end of the For loop.
• Print the Fixed-point Index
• The Exit of the Program.

Below is the implementation:

# Create a function findFixed() which accepts the given list
# as an argument and returns -1
# if the list does not contain a fixed point else print the fixed point.

def findFixed(gvnlst):
# Inside the findFixed() function calculate the length of the given list.
lstleng = len(gvnlst)
# Loop till length of the given list using For loop.
for m in range(lstleng):
# Check if the element is equal to the index(iterator value of the For loop).
if gvnlst[m] is m:
# If it is true then return index.
return m
# Return -1 after the end of the For loop.
return -1

# Give the list as user input using list(),map(),input(),and split() functions.
# Store it in a variable.
gvnlst = list(map(int, input(
'Enter some random List Elements separated by spaces = ').split()))
# Pass the given list as an argument to the findFixed() function.
fxdpoint = findFixed(gvnlst)
print('The Fixed point present in the given list is [', fxdpoint, ']')


Output:

Enter some random List Elements separated by spaces = 7 8 45 19 21 23 6 7 5
The Fixed point present in the given list is [ 6 ]

Related Programs: