Skip to main content

Linear Search problem practice In DSA

Linear Search — Practice Problems in Data structures & algorithms (DSA)

Practice Sheet: Applying Linear Search to common array problems

Goal: Learn how to scan an array from left to right and apply Linear Search to find a first index, check presence, count occurrences, find all indexes, and find a maximum value.

Problem 1 — Find First Index

Given the array [15, 8, 22, 10, 7, 22] and target 22.

  Return the first index of 22.

  Expected answer: 2

Problem 2 — Check Presence

Given the array [4, 9, 12, 6, 18, 3] and target 15.

  Return True if 15 is present.

  Return False if 15 is not present.

  Expected answer: False

Problem 3 — Count Occurrences

Given the array [5, 2, 5, 8, 5, 9, 2] and target 5.

  Return the number of occurrences of 5.

  Expected answer: 3

Problem 4 — Find All Indexes

Given the array [3, 7, 3, 10, 3, 5, 3] and target 3.

  Return all indexes where 3 occurs.

  Expected answer: [0, 2, 4, 6]

Problem 5 — Find Maximum

Given the array [12, 45, 7, 89, 23, 56].

  Use the Linear Search approach to find the maximum element.

  Return the maximum element.

  Expected answer: 89

How to Practice These Problems

Use the same simple process for each problem:

Step

What to do

1. Understand

Identify the array and the target/requirement.

2. Apply Linear

Search

Check elements from left to right.

3. Record the result

Depending on the question, save the first index, True/False, count, all indexes, or maximum.

4. Code it

Write the solution yourself in Python.

5. Check complexity

For a basic Linear Search solution, time is generally O(n); extra space is generally O(1), except when the task requires storing multiple indexes/results.

Important: If the question asks only whether an element is present, you can stop when the first match is found. If it asks for the count or all indexes, you must continue checking the whole array.

 

Answer:

from array import*

arr=array('i',[12,5,8,20,15,7])

for x in arr:

    if x==20:

        print("yes 20 present in array",arr.index(20))

        break

    else:

        print("not present")

       

arr1=array('i', [15,8,22,10,7,22])

print("The first index of 22 is",arr1.index(22))

 

arr2=array('i',[4,9,12,6,18,3])

for i in range(len(arr2)):

    if i==15:

        print("if present")

    else:print("if not present")

 

arr3=array('i',[5,2,5,8,5,9,2,5,5])

print("The number of occurrence of 5 is",arr3.count(5))

count=0

for co in arr3:

    if co==5:

     count=count+1

print("the number of occurnce of 5 is",count)  

 

 

arr4=array('i',[3,7,3,10,3,5,3])

count=0

a=[]

for index,co in enumerate(arr4):

    if co==3:

     count=count+1

     a.append(index)

print("the number of occurnce of 3 is",count)

print("index of 3 is",a)

 

arr5=array('i',[12,45,7,89,23,56])

print(max(arr5))

print(sorted(arr5)[len(sorted(arr5))-1])

Comments

Popular posts from this blog

Python Variables and Data Types Explained with Examples

🚀 Welcome to NiceinPythonlearn | 📘 Learn Python with Easy Tutorials, Notes & Practice Questions | 💻 Build Real-World Projects | 💬 Enjoying the content?Share your feedback! | 📝 If you have any questions, suggestions, or face any problems, please contact us using the Contact Form or leave a comment. We will do our best to solve it together. 🤝 | ⭐ Follow us for new updates and keep learning! Introduction Variables and Data Types are the fundamental concepts of Python programming. Variables are used to store data, while Data Types define the kind of data a variable can hold. These concepts form the foundation of every Python program, making code simple, organized, efficient, and easy to understand and maintain. Roman English: Variables data store karte hain, aur Data Types batate hain data kis type ka hai. Ye Python programming ki basic foundation hain. Learning Objectives After completing this topic, you will be able to: Underst...

Python input and output (I/O) Statements Explained with Examples

Introduction Python Input and Output are the foundation of interactive programming. Input allows users to enter information, while output displays results on the screen. By learning these concepts, you can create programs that communicate with users, solve real-world problems, and build applications that respond to user input effectively. Hinglish:Python Input aur Output interactive programming ki foundation hai. Input ka use karke user se information li jaati hai, aur Output ka use karke screen par result dikhaya jaata hai. In concepts ko seekhne ke baad aap aise programs bana sakte ho jo users se communicate karein, real-world problems solve karein, aur user ke input ke hisaab se response dene wale applications develop karein. Learning Objectives After completing this lesson, you will be able to: 1.Understand the concept of Input and Output in Python. 2.Use the input() function to take data from users. 3.Use the print() function to display info...

Python Print Statement and Comments Explained with Examples

1) Print Statement Function in Python A print statement Function is used to show output on the screen. In Python, we write print() with a small letter p, not a capital letter. Roman : Print statement Function screen par output dikhane ke liye use hota hai. Python me print() small letter p se likhte hain, kyunki Python case-sensitive hota hai. Example of Print Statement Function in Python : print("Hello World") Output(show on Terminal): Hello World Another Example of Print Statement Function in Python : print(10) print(2.0) print(True) print(10+5) print(2*5) print(0-0) Output(show on Terminal): 10 2.0 True 15 10 0 2) Comments in Python (Single and Multiples): Single Line Comments using Hash(#) in Python A comment is used to explain a line of code. In Python, a comment is written using #(hash...