chore(CPlusPlus): added search insert position (#508)
Co-authored-by: Rahul Rajeev Pillai <66192267+Ashborn-SM@users.noreply.github.com>pull/520/head
parent
1bcf9483da
commit
f05e993bdd
|
@ -0,0 +1,69 @@
|
|||
/*
|
||||
Description: Given a sorted array of distinct integers and a target value,
|
||||
return the index if the target is found. If not, return the index where it
|
||||
would be if it were inserted in order.
|
||||
|
||||
Approach: Using Binary search to solve in least time complexity.
|
||||
|
||||
Time Complexity: O(log n)
|
||||
*/
|
||||
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
using namespace std;
|
||||
|
||||
//function
|
||||
int searchInsert(vector<int>& nums, int target) {
|
||||
//starting index
|
||||
int start=0;
|
||||
//ending index
|
||||
int end=nums.size()-1;
|
||||
while(start<=end){
|
||||
//calculating mid element
|
||||
int mid = start + (end-start)/2;
|
||||
//if target found, return the index
|
||||
if(nums[mid]==target){
|
||||
return mid;
|
||||
}
|
||||
if(nums[mid]>target){
|
||||
end=mid-1;
|
||||
}
|
||||
else{
|
||||
start=mid+1;
|
||||
}
|
||||
}
|
||||
//returning end+1 so that we can get the index where the target element can be inserted
|
||||
return end+1;
|
||||
|
||||
}
|
||||
|
||||
//main starts
|
||||
int main() {
|
||||
cout << "Enter number of elements in the array\n";
|
||||
int n;
|
||||
cin>>n;
|
||||
cout<<"Enter the array elements\n";
|
||||
vector<int> nums(n);
|
||||
for(int i=0;i<n;i++){
|
||||
cin>>nums[i];
|
||||
}
|
||||
cout<<"Enter the target element\n";
|
||||
int target;
|
||||
cin>>target;
|
||||
cout<<"The target element can be inserted at: "<<searchInsert(nums,target);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
Example 1
|
||||
Input: nums = [1,3,5,6], target = 5
|
||||
Output: 2
|
||||
(Since the target element is present it will return its index)
|
||||
|
||||
Example 2:
|
||||
Input: nums = [1,3,5,6], target = 2
|
||||
Output: 1
|
||||
(Since the target element is not present in the nums array, it will return the position where the target element can be inserted)
|
||||
We can insert 2 at position 1 - [1,2,3,5,6]
|
||||
|
||||
*/
|
|
@ -18,6 +18,7 @@
|
|||
14. [Maximum Difference](Arrays/maximum-difference.cpp)
|
||||
15. [Occurrence of one in sorted array](Arrays/occurence-of-one-in-sorted-array.cpp)
|
||||
16. [Segregate 0s and 1s](Arrays/segregate-0-and-1.cpp)
|
||||
17. [Search insert position](Arrays/search-insert-position.cpp)
|
||||
|
||||
## Dynamic-Programming
|
||||
|
||||
|
@ -123,11 +124,11 @@
|
|||
3. [Prime Sieve](Maths/prime-sieve.cpp)
|
||||
4. [Fibonacci Series](Maths/fibonaccci-series.cpp)
|
||||
5. [Binomial Coefficient](Maths/binomial-coefficient.cpp)
|
||||
5. [Armstrong Number](Maths/armstrong.cpp)
|
||||
6. [Palindrome](Maths/palindrome.cpp)
|
||||
7. [Reverse digit of a number](Maths/reverse-digits.cpp)
|
||||
8. [Missing number](Maths/missing-number.cpp)
|
||||
9. [Factorial of a number](Maths/factorial.cpp)
|
||||
6. [Armstrong Number](Maths/armstrong.cpp)
|
||||
7. [Palindrome](Maths/palindrome.cpp)
|
||||
8. [Reverse digit of a number](Maths/reverse-digits.cpp)
|
||||
9. [Missing number](Maths/missing-number.cpp)
|
||||
10. [Factorial of a number](Maths/factorial.cpp)
|
||||
|
||||
# Recursion
|
||||
|
||||
|
|
Loading…
Reference in New Issue