Don’t worry, unlock all articles / blogs on PrepInsta by just simply logging in on our website
Find the Smallest and largest element in an array using Python
September 30, 2024
Smallest and Largest Element in an array using Python
Here, in this page we will discuss the program to find the smallest and largest element in an array using python programming language. We will discuss different algorithms to find the smallest and largest element of the given input array.
Method Discussed are given below :
Method 1 : Using Iteration.
Method 2 : Using Sort() function.
Method 3 : Using max() and min() function
Method 1 :
Take a variable say mini to store the minimum element of the array and maxi to store the maximum element.
size=int(input(“enter the lentgh of array”))
arr=[]
for i in range(size):
arr.append(int(input(“Enter the Values–>”)))
arr.sort()
print(“the smallest value”,min(arr),”the gretaes value”,max(arr))
The written code will mislead students a lot please do change with something like this:
n=int(input(“Enter number of elements:”))
a=[int(i) for i in input(“Enter space seprated element:”).split()]
s=set(a)
ls=list(s)
ls.sort()
print(“Largest element=”,ls[-1])
print(“Smallest element=”,ls[0])
The written will mislead students a lot please do change with something like this:
n=int(input(“Enter number of elements:”))
a=[int(i) for i in input(“Enter space seprated element:”).split()]
s=set(a)
ls=list(s)
ls.sort()
print(“Largest element=”,ls[-1])
print(“Smallest element=”,ls[0])
import numpy as np
n=int(input())
a=np.array([input().split() for i in range(n)] ,int)
print(min(a),max(a),sep=’\n’)
size=int(input(“enter the lentgh of array”))
arr=[]
for i in range(size):
arr.append(int(input(“Enter the Values–>”)))
arr.sort()
print(“the smallest value”,min(arr),”the gretaes value”,max(arr))
The written code will mislead students a lot please do change with something like this:
n=int(input(“Enter number of elements:”))
a=[int(i) for i in input(“Enter space seprated element:”).split()]
s=set(a)
ls=list(s)
ls.sort()
print(“Largest element=”,ls[-1])
print(“Smallest element=”,ls[0])
The written will mislead students a lot please do change with something like this:
n=int(input(“Enter number of elements:”))
a=[int(i) for i in input(“Enter space seprated element:”).split()]
s=set(a)
ls=list(s)
ls.sort()
print(“Largest element=”,ls[-1])
print(“Smallest element=”,ls[0])