Skip to content
Open
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
54 changes: 54 additions & 0 deletions C++/First and last occurence in sorted array.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
class Solution {
public:
int firstoccurence(vector<int>& nums,int target){
int s = 0;
int e = nums.size()-1;
int ans = -1;
while(s<=e){
int mid = s + (e-s)/2;
if(nums[mid] == target){
ans = mid ;
e = mid - 1;
}
else if(target<nums[mid]){
e = mid -1;
}
else
s = mid +1;

}
return ans;
}
int lastoccurence(vector<int>& nums,int target){
int s = 0;
int e = nums.size()-1;
int ans = -1;
while(s<=e){
int mid = s + (e-s)/2;
if(nums[mid] == target){
ans = mid ;
s = mid + 1;
}
else if(target<nums[mid]){
e = mid -1;
}
else
s = mid +1;

}
return ans;
}
vector<int> searchRange(vector<int>& nums, int target) {
vector<int> ans(2,-1);
int first = firstoccurence(nums,target);
if(first == -1)
return ans;
int last = lastoccurence(nums,target);
ans[0]=first;
ans[1]=last;

return ans;


}
};
14 changes: 14 additions & 0 deletions C++/Kadane's algo(Maximum Subarray).cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class Solution {
public:
int maxSubArray(vector<int>& nums) {
int mth = nums[0];
int msf = nums[0];
for(int i = 1 ; i < nums.size() ; i ++){
mth+=nums[i];
mth = max(mth,nums[i]);
msf = max(msf,mth);
}
return msf;

}
};
25 changes: 25 additions & 0 deletions C++/Reverse LinkedList.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode* reverseList(ListNode* head) {
ListNode * curr = head;
ListNode* prev = NULL;

while(curr){
ListNode *n = curr->next;
curr->next = prev;
prev = curr;
curr = n;
}
return prev;
}
};