Array Search: Given an array, arr[] of n integers, and an integer element x, find whether element x is present in the array. Return the index of the first occurrence of x in the array, or -1 if it doesn't exist
This's Linear Search O(n)👆🏻 Try to think this problem before jumping into code🤔
input: arr[] = [1,2,3,4], x = 3 output: 2 Explanation: For array [1, 2, 3, 4], the element to be searched is 3. Since 3 is present at index 2, the output is 2. Input: arr[] = [10, 8, 30], x = 6
Output: -1
Explanation: The element to be searched is 6 and it is not present, so we return -1.
This's Linear Search O(n)👆🏻 Try to think this problem before jumping into code🤔
SamiTech Code
Array Search: Given an array, arr[] of n integers, and an integer element x, find whether element x is present in the array. Return the index of the first occurrence…
Code Implementation👆🏻
I wish you understand the logic! in the next I'll share for you some applications of Linear Search(Sequential Search)👌
class Solution:
def search(self, arr, x):
# Code with the logical error fixed
for n in range(len(arr)):
if arr[n] == x:
return n # Return the index if found
# Only return -1 after the entire loop has completed,
# meaning the element was not found in the entire list.
return -1
arr = [1, 2, 3, 4]
x = 3
# Create an instance of the class to call the instance method
sol_instance = Solution()
print(sol_instance.search(arr, x))
I wish you understand the logic! in the next I'll share for you some applications of Linear Search(Sequential Search)👌
👏3
Applications of Linear Search Algorithm:
👉🏽In The next step we'll learn about Binary Search this's very important while learning DSA😁
Unsorted Lists:
When we have an unsorted array or list, linear search is most commonly used to find any element in the collection.
Small Data Sets: Linear Search is preferred over binary search when we have small data sets with
Searching Linked Lists: In linked list implementations, linear search is commonly used to find elements within the list. Each node is checked sequentially until the desired element is found.
Simple Implementation: Linear Search is much easier to understand and implement as compared to Binary Search or Ternary Search.
👉🏽In The next step we'll learn about Binary Search this's very important while learning DSA😁
https://youtu.be/GnZ9ppr_zaI?si=No9RHS19jrAzzQzp 👉🏽Take a look this video carefully you'll get important point about Binary Search and it's usefulness.
YouTube
Binary Search - Data Structures & Algorithms Tutorial Python #13
Binary search is a popular search algorithm that can find an element using log(n) time complexity. In this video we will go over theory behind binary search, compare it with linear search and then implement binary search in python.
Code: https://githu…
Code: https://githu…
👍2
👉🏽For more info this pdf is important you can study it it's organized, clarifications, and also problem-solving. It contains Linear Search and Binary Search https://projector-video-pdf-converter.datacamp.com/29490/chapter3.pdf
As long as we learn DSA we must solve daily proble😎 👉🏽so here I've tried to solve binary search leetcode problem. https://leetcode.com/problems/binary-search/submissions/1925757948/
LeetCode
Binary Search - LeetCode
Can you solve this real interview question? Binary Search - Given an array of integers nums which is sorted in ascending order, and an integer target, write a function to search target in nums. If target exists, then return its index. Otherwise, return -1.…
SamiTech Code pinned «Array Search: Given an array, arr[] of n integers, and an integer element x, find whether element x is present in the array. Return the index of the first occurrence…»
Binary Search definition: Binary search is an efficient search algorithm that locates the position of a target value within a sorted array by repeatedly dividing the search interval in half. This "divide and conquer" strategy gives it a logarithmic time complexity of O(log N), making it significantly faster than linear search for large datasets.
👍1
Hey I was searching for two pointer technique in Youtube even google but noting changed but finally I asked Chatgpt this
I loved the way chatgpt clarify this concept for me🥰 Not only this.. and Try to ask chatgpt in your own thought then after then you'll explain about two pointer in your own word. if so the implementation part is simple. 👉🏽DO YOU KNOW AT THE FIRST THINKING IS IMPORTANT THEN YOU DO WHAT YOU THINK.👌 this's all about problem solving technique
In a way I can understand can you just give me two pointer clarification in easy way without memorizing them🥹 after then chatgpt gives me this👇 First: Forget the Name “Two Pointer”
Don’t think “two pointer technique”.
Think only this:
“How can I look at two places in the array at the same time and move intelligently?”
That’s it.
I loved the way chatgpt clarify this concept for me🥰 Not only this.. and Try to ask chatgpt in your own thought then after then you'll explain about two pointer in your own word. if so the implementation part is simple. 👉🏽DO YOU KNOW AT THE FIRST THINKING IS IMPORTANT THEN YOU DO WHAT YOU THINK.👌 this's all about problem solving technique
👏2
SamiTech Code pinned «Binary Search definition: Binary search is an efficient search algorithm that locates the position of a target value within a sorted array by repeatedly dividing the search interval in…»
Easy way to understand Two-Pointer: Step 1: Starts from human way: imagine this array [1, 3, 4, 6, 8, 10] Question:
👉 “Is there a pair that adds up to 11?” How beginner think to get 11 the same as true it follows this 1 + 3, 1 + 4, 1 + 6 ... Too many checks😇 Your brain already feels this's wasteful Step 2: In other way (this's where two pointer is important) : If I add smallest + largest, what happens? 1(smallest) + (largest)10 = 11 👆🏻This thinking is two pointer Step 3: Why moving pointer make sense? Suppose target = 9 here 1 = L and 10 = R CASE 1: 1 + 10 = 11 (too big) Question:
👉 Should you increase 1 or decrease 10? — 1 is already smallest, but big sum comes from 10 So move right pointer left 1 + 8 = 9 ✅ 👉🏽Two Pointer is not trick it's just using position + logic to avoid waste.
Thinking Problem (Two Pointer – Human Way):
You're given this sorted array: [2, 3, 5, 7, 11, 15], target sum = 10 your task explain👇: 1.Which two numbers would you look at first?
2.What is their sum?
3.Is the sum too big, too small, or exact?
4.Which number would you change and why? 👉🏽Key point: this thinking foster our thinking capacity. Therefore, just go ahead and try to explain it by your own word.👉🏽 No need to code.🧑💻
Nice I've solved this problem using two pointer technique.😁 two pointer means it points to two element at the same time . 👉🏽the problem is there is given arr[] then we check the two numbers that their sum will be equal with target. input: arr = [1, 4, 6, 8, 9, 10]
target = 10 explanations take left and right. This's why understanding about the problem is needed. ✅
target = 10 explanations take left and right. This's why understanding about the problem is needed. ✅
👍1
If we're given the array of sorted list and duplicated elements what shall do?👆🏻 In general the logic skip the duplicated and take the unique then it compared with the other element to get the target. In this process target is sum of the two numbers. So this image is solving for what we're talking about.👆🏻
SamiTech Code pinned «Easy way to understand Two-Pointer: Step 1: Starts from human way: …»