Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion Contributor.md
Original file line number Diff line number Diff line change
Expand Up @@ -251,4 +251,7 @@ Added solution for custom range slider
- Added a simple Hand Cricket Game using HTML, CSS and JS

44. 1. Wangari Jane | [LinkedIn](https://www.https://www.linkedin.com/in/jane-wangari-32b284345/) | [GitHub](https://github.com/Wangarijane)
- Fixed typos and formatting in README
- Fixed typos and formatting in README

45. Niranjan patil | [LinkedIn](https://www.linkedin.com/in/niranjan-patil-8512a1374) | [GitHub](https://github.com/niranjanpatil1010)
- Added Solution for Leetcode Q35 search insert position
51 changes: 51 additions & 0 deletions dsa_in_cpp/array/search_insert_position.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#include <iostream>
#include <vector>
using namespace std;

class Solution {
public:
int searchInsert(vector<int>& nums, int target) {
int n = nums.size();
int l = 0, h = n - 1;
int index = n;

while (l <= h) {
int mid = l + (h - l) / 2;
if (nums[mid] == target) {
return mid;
}
else if (nums[mid] > target) {
index = mid;
h = mid - 1;
}
else {
index = mid + 1;
l = mid + 1;
}
}

return index;
}
};

int main() {
int n, target;
cout << "Enter number of elements in the sorted array: ";
cin >> n;

vector<int> nums(n);
cout << "Enter " << n << " elements in sorted order: ";
for (int i = 0; i < n; i++) {
cin >> nums[i];
}

cout << "Enter the target number: ";
cin >> target;

Solution sol;
int position = sol.searchInsert(nums, target);

cout << "The target should be at index: " << position << endl;

return 0;
}