|
| 1 | +package com.kishan.scala.leetcode.juneChallenges |
| 2 | + |
| 3 | +/* |
| 4 | +* Given a sorted array and a target value, return the index if the target is found. |
| 5 | +* If not, return the index where it would be if it were inserted in order. |
| 6 | +* |
| 7 | +* You may assume no duplicates in the array. |
| 8 | +* */ |
| 9 | + |
| 10 | +/* |
| 11 | +* Input: [1,3,5,6], 5 |
| 12 | + Output: 2 |
| 13 | +* Input: [1,3,5,6], 2 |
| 14 | + Output: 1 |
| 15 | +* Input: [1,3,5,6], 7 |
| 16 | + Output: 4 |
| 17 | +* Input: [1,3,5,6], 0 |
| 18 | + Output: 0 |
| 19 | +* */ |
| 20 | + |
| 21 | +/* |
| 22 | +* Approach: |
| 23 | +* |
| 24 | +* Linear Search Approach: |
| 25 | +* |
| 26 | +* 1. We will check if the element is lesser than or equal to the first element. If yes we will return 0. Time Complexity: O(1) |
| 27 | +* 2. We will check if the element is equal to the last element. If yes we will return length - 1. Time Complexity: O(1) |
| 28 | +* 3. We will check if the element is greater than the last element. If yes we will return the length of the array. Time Complexity: O(1) |
| 29 | +* 4. Else we will iterate through the array and will check if the element is less than or equal to the current element. If yes we will return the index of the current element. Time Complexity: O(n) |
| 30 | +* |
| 31 | +* |
| 32 | +* Binary Search Algorithm: |
| 33 | +* |
| 34 | +* First 3 cases from above will be applicable here. |
| 35 | +* For the last case instead of iterating through the array. We will use Binary Search Algorithm to do that. Time Complexity: O(log(n)) |
| 36 | +* |
| 37 | +* |
| 38 | +* */ |
| 39 | + |
| 40 | + |
| 41 | +object SearchInsertPosition { |
| 42 | + |
| 43 | + def main(args: Array[String]): Unit = { |
| 44 | + val nums = Array(1, 3, 5, 6) |
| 45 | + val target = 2 |
| 46 | + val result = searchInsertBinary(nums, target) |
| 47 | + println(result, result == 1) |
| 48 | + } |
| 49 | + |
| 50 | + //Using Binary Search |
| 51 | + def searchInsertBinary(nums: Array[Int], target: Int): Int = { |
| 52 | + val length = nums.length |
| 53 | + var counter = 0 |
| 54 | + if (target <= nums.head) { |
| 55 | + return 0 |
| 56 | + } else if (target == nums.last) { |
| 57 | + return nums.length - 1 |
| 58 | + } else if (target > nums.last) { |
| 59 | + return nums.length |
| 60 | + } else { |
| 61 | + return binarySearch(nums, target) |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + //Binary Search Algorithm |
| 66 | + def binarySearch(arr: Array[Int], target: Int): Int = { |
| 67 | + var head = 0 |
| 68 | + var tail = arr.length - 1 |
| 69 | + while (head < tail) { |
| 70 | + val mid = head + (tail - head) / 2 |
| 71 | + if (target == arr(mid)) { |
| 72 | + return mid |
| 73 | + } |
| 74 | + if (target < arr(mid)) { |
| 75 | + tail = mid |
| 76 | + } else { |
| 77 | + head = mid + 1 |
| 78 | + } |
| 79 | + } |
| 80 | + if (target < arr(tail)) tail else head |
| 81 | + } |
| 82 | + |
| 83 | + //Linear Search Algorithm |
| 84 | + def searchInsert(nums: Array[Int], target: Int): Int = { |
| 85 | + val length = nums.length |
| 86 | + var counter = 0 |
| 87 | + if (target <= nums.head) { |
| 88 | + return 0 |
| 89 | + } else if (target == nums.last) { |
| 90 | + return nums.length - 1 |
| 91 | + } else if (target > nums.last) { |
| 92 | + return nums.length |
| 93 | + } |
| 94 | + for (i <- nums.indices) { |
| 95 | + if (target <= nums(i)) { |
| 96 | + return i |
| 97 | + } |
| 98 | + } |
| 99 | + return -1 |
| 100 | + } |
| 101 | +} |
0 commit comments