Skip to main content

✅ Solution Implement Both Linear and Binary Search Day 3 DSA with Java


🔹 Linear Search in Java


public class LinearSearch { public static int linearSearch(int[] arr, int key) { for (int i = 0; i < arr.length; i++) { if (arr[i] == key) return i; } return -1; // Not found } public static void main(String[] args) { int[] arr = {5, 3, 8, 2, 9}; int key = 8; int index = linearSearch(arr, key); System.out.println("Element found at index: " + index); } }

🔹 Binary Search in Java (Iterative)


public class BinarySearch { public static int binarySearch(int[] arr, int key) { int start = 0, end = arr.length - 1; while (start <= end) { int mid = start + (end - start) / 2; if (arr[mid] == key) return mid; else if (arr[mid] < key) start = mid + 1; else end = mid - 1; } return -1; } public static void main(String[] args) { int[] arr = {1, 3, 5, 7, 9}; int key = 5; int index = binarySearch(arr, key); System.out.println("Element found at index: " + index); } }

2. LeetCode Problems

🔸 Search Insert Position

Problem: Return the index if the target is found. If not, return the index where it would be inserted.


public class SearchInsertPosition { public static int searchInsert(int[] nums, int target) { int low = 0, high = nums.length - 1; while (low <= high) { int mid = low + (high - low) / 2; if (nums[mid] == target) return mid; else if (nums[mid] < target) low = mid + 1; else high = mid - 1; } return low; } public static void main(String[] args) { int[] nums = {1, 3, 5, 6}; int target = 2; System.out.println("Insert position: " + searchInsert(nums, target)); } }

🔸 First Bad Version

Assume isBadVersion(version) API exists.


public class FirstBadVersion { static int bad = 4; // Let's assume version 4 is the first bad version public static boolean isBadVersion(int version) { return version >= bad; } public static int firstBadVersion(int n) { int low = 1, high = n; while (low < high) { int mid = low + (high - low) / 2; if (isBadVersion(mid)) high = mid; else low = mid + 1; } return low; } public static void main(String[] args) { System.out.println("First bad version: " + firstBadVersion(10)); } }

🌱 3. Explore: Lower Bound & Upper Bound

These are binary search variants often used in competitive programming.

🔸 Lower Bound (First element ≥ target)


public static int lowerBound(int[] arr, int target) { int low = 0, high = arr.length; while (low < high) { int mid = low + (high - low) / 2; if (arr[mid] < target) low = mid + 1; else high = mid; } return low; // returns the index }

🔸 Upper Bound (First element > target)


public static int upperBound(int[] arr, int target) { int low = 0, high = arr.length; while (low < high) { int mid = low + (high - low) / 2; if (arr[mid] <= target) low = mid + 1; else high = mid; } return low; // returns the index }

You can test both with:


int[] arr = {1, 3, 3, 5, 7}; System.out.println("Lower Bound of 3: " + lowerBound(arr, 3)); // Output: 1 System.out.println("Upper Bound of 3: " + upperBound(arr, 3)); // Output: 3

Comments

Popular posts from this blog

Raster scan Vs Vector Scan

1. Raster Scan Display   How It Works : A raster scan display works by painting an image on the screen pixel by pixel, row by row. It follows a systematic pattern where the electron beam (in CRT monitors) or the display elements (in modern LCD/LED screens) sweep across the screen from left to right, top to bottom, in a series of horizontal lines (scan lines). This process is akin to how a traditional TV screen works.   Process : The display draws the image starting from the top-left corner, moving to the right, then moves to the next row below, and repeats this process until the entire screen is filled. This pattern creates a grid of pixels, where each pixel can have a color and brightness level.   Characteristics : Pixel-based : The screen consists of a grid of pixels, and each pixel can have a distinct color and intensity. Continuous Image : Raster scan displays are capable of displaying detailed and complex images, including photographs and videos, because they break t...

Inheritance

*■ Inheritance*  • Inheritance is a concept in OOP that allows a class to inherit properties and behaviors (methods) from another class. • A class that inherits from another class is called a derived class (or subclass) • The class which gets inherited by another class is called the base class (or superclass). • Inheritance is possible only if there is is-a relationship between parent and child class. • constructors are not inherited in derived class, however the derived class can call default constructor implicitly and if there's a parameterised constructors in bass class then derived class can call it using 'base' keyword.  ____________________________________________  *➤ Rules of Inheritance*  1) C# supports single inheritance, meaning a class can inherit from only one base class. 2) A parent class constructor must be accessible in child class otherwise  inheritance will be not possible. 3) every class, whether user-defined or predefined implicitly derives fr...

unit -1 Introduction of Image processing

  What is Image Processing? Image processing is a method to perform operations on an image to enhance it or extract useful information. It is a type of signal processing where the input is an image, and the output may be either an image or characteristics/features associated with that image. Goals of Image Processing Image Enhancement : Improving visual appearance (e.g., contrast, sharpness) Image Restoration : Removing noise or distortion Image Compression : Reducing the amount of data required to represent an image Feature Extraction : Identifying objects, edges, or patterns Image Analysis : Understanding and interpreting image content Object Recognition : Detecting and identifying objects in an image What is an Image? An image is a two-dimensional function f(x, y) , where x and y are spatial coordinates, and f is the intensity (brightness or color) at that point. For digital images, both x, y, and f are finite and discrete. Types of Image Representation...