Skip to content

Renamed files and added 5 problems #370

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 4, 2022
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
Binary file added .DS_Store
Binary file not shown.
Binary file added CPP/.DS_Store
Binary file not shown.
Binary file added CPP/Problems/.DS_Store
Binary file not shown.
156 changes: 78 additions & 78 deletions CPP/Problems/BitonicArray.cpp
Original file line number Diff line number Diff line change
@@ -1,95 +1,95 @@
// Bitonic array is an array having strictly increasing order upto an element and then having strictly
// decreasing order. It is also called Mountain array.
// Bitonic array is an array having strictly increasing order upto an element and then having strictly
// decreasing order. It is also called Mountain array.

// Program to find element in a bitonic array
// T.C.: O(log n)
// S.C.: O(1)
#include <iostream>
#include <vector>
using namespace std;
// Program to find element in a bitonic array
// T.C.: O(log n)
// S.C.: O(1)
#include <iostream>
#include <vector>
using namespace std;

int findPeakIndex (vector<int> vect, int size) { // find index of peak element
int lo = 0, hi = size-1;
int mid = lo + (hi - lo)/2;
int findPeakIndex (vector<int> vect, int size) { // find index of peak element
int lo = 0, hi = size-1;
int mid = lo + (hi - lo)/2;

while (lo < hi) {
if (mid == size-1 || vect[mid] > vect[mid+1]) {
hi = mid;
}
else if (mid == 0 || vect[mid] > vect[mid-1]) {
lo = mid+1;
}
mid = lo + (hi - lo)/2;
while (lo < hi) {
if (mid == size-1 || vect[mid] > vect[mid+1]) {
hi = mid;
}
return lo;
else if (mid == 0 || vect[mid] > vect[mid-1]) {
lo = mid+1;
}
mid = lo + (hi - lo)/2;
}
return lo;
}

int leftPart (vector<int> vect, int target, int hi) { // search element in left part
int lo = 0;
int mid = lo + (hi - lo)/2;
while (lo <= hi) {
if (vect[mid] == target) {
return mid;
}
else if (vect[mid] > target) {
hi = mid - 1;
}
else {
lo = mid + 1;
}
mid = lo + (hi - lo)/2;
int leftPart (vector<int> vect, int target, int hi) { // search element in left part
int lo = 0;
int mid = lo + (hi - lo)/2;
while (lo <= hi) {
if (vect[mid] == target) {
return mid;
}
else if (vect[mid] > target) {
hi = mid - 1;
}
return -1;
else {
lo = mid + 1;
}
mid = lo + (hi - lo)/2;
}
return -1;
}

int rightPart (vector<int> vect, int target, int lo) { // search element in right part
int hi = vect.size()-1;
int mid = lo + (hi - lo)/2;
while (lo <= hi) {
if (vect[mid] == target) {
return mid;
}
else if (vect[mid] < target) {
hi = mid - 1;
}
else {
lo = mid + 1;
}
mid = lo + (hi - lo)/2;
int rightPart (vector<int> vect, int target, int lo) { // search element in right part
int hi = vect.size()-1;
int mid = lo + (hi - lo)/2;
while (lo <= hi) {
if (vect[mid] == target) {
return mid;
}
else if (vect[mid] < target) {
hi = mid - 1;
}
return -1;
else {
lo = mid + 1;
}
mid = lo + (hi - lo)/2;
}
return -1;
}

int findElement(vector<int> vect, int size, int target) {
// DRIVER CODE
// find peak of array
int peak = findPeakIndex (vect, size);
// compare peak element with target
if (target > vect[peak]) {return -1;}
else if (target == vect[peak]) {return peak;}
// find element in increasing/decreasing sequence
int findElement(vector<int> vect, int size, int target) {
// DRIVER CODE
// find peak of array
int peak = findPeakIndex (vect, size);
// compare peak element with target
if (target > vect[peak]) {return -1;}
else if (target == vect[peak]) {return peak;}
// find element in increasing/decreasing sequence
else {
int leftIndex = leftPart(vect, target, peak);
if (leftIndex != -1) {
return leftIndex;
}
else {
int leftIndex = leftPart(vect, target, peak);
if (leftIndex != -1) {
return leftIndex;
}
else {
return rightPart(vect, target, peak);
}
return rightPart(vect, target, peak);
}
return 0;
}
return 0;
}

int main () {
vector<int> numvect;
int num, target;
cin>>num;
cin>>target;
for (int i = 0; i < num; i++) {
int temp;
cin>>temp;
numvect.push_back(temp);
}
cout<<findElement(numvect, num, target);
return 0;
int main () {
vector<int> numvect;
int num, target;
cin>>num;
cin>>target;
for (int i = 0; i < num; i++) {
int temp;
cin>>temp;
numvect.push_back(temp);
}
cout<<findElement(numvect, num, target);
return 0;
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// https://leetcode.com/problems/two-sum/
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// https://leetcode.com/problems/binary-tree-level-order-traversal/
class Solution {
public:
vector<vector<int>> levelOrder(TreeNode* root) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// https://leetcode.com/problems/container-with-most-water/
class Solution {
public:
int maxArea(vector<int>& height) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// https://leetcode.com/problems/number-of-dice-rolls-with-target-sum/
class Solution {
public:

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// https://leetcode.com/problems/roman-to-integer/
class Solution {
public:
int romanToInt(string s) {
Expand Down
15 changes: 15 additions & 0 deletions CPP/Problems/Leetcode-1346-Check-If-N-and-Its-Double-Exist.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// https://leetcode.com/problems/check-if-n-and-its-double-exist/
class Solution {
public:
bool checkIfExist(vector<int>& arr) {
for (int i = 0; i < arr.size(); i++) {
for (int j = 0; j < arr.size(); j++) {
if (i!=j) {
if (arr[i] == 2*arr[j])
return true;
}
}
}
return false;
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// https://leetcode.com/problems/count-negative-numbers-in-a-sorted-matrix/
class Solution {
public:
int countNegatives(vector<vector<int>>& grid) {
int count = 0;

for (int row = 0; row < grid.size(); row++) {
for (int col = grid[row].size()-1; col >= 0; col--) {
if (grid[row][col] < 0)
count++;
else
break;
}
}
return count;
}
};
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
// Question Link
// https://leetcode.com/problems/maximum-product-subarray/
// ----------------------------------------------

class Solution {
public:
int maxProduct(vector<int>& nums) {
Expand Down
23 changes: 23 additions & 0 deletions CPP/Problems/Leetcode-1572-Matrix-Diagonal-Sum.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// https://leetcode.com/problems/matrix-diagonal-sum/
class Solution {
public:
int diagonalSum(vector<vector<int>>& mat) {
int rc = 0;
int TotalSum = 0;
// Primary Diagonal
while (rc < mat.size()) {
TotalSum += mat[rc][rc];
rc++;
}
// Secondary Diagonal
int r = mat.size()-1, c = 0;
while (r >= 0 && c < mat.size()) {
TotalSum += mat[r][c];
r--;
c++;
}
if (mat.size() & 1)
return TotalSum - (mat[mat.size()/2][mat.size()/2]);
return TotalSum;
}
};
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// https://leetcode.com/problems/add-two-numbers/
/**
* Definition for singly-linked list.
* struct ListNode {
Expand Down
7 changes: 7 additions & 0 deletions CPP/Problems/Leetcode-231-Power-of-Two.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// https://leetcode.com/problems/power-of-two/
class Solution {
public:
bool isPowerOfTwo(int x) {
return x && (!(x&(x-1)));
}
};
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// https://leetcode.com/problems/bitwise-xor-of-all-pairings/
class Solution {
public:
int xorAllNums(vector<int>& nums1, vector<int>& nums2) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// https://leetcode.com/problems/move-zeroes/
class Solution {
public:
void moveZeroes(vector<int>& nums) {
Expand Down
30 changes: 30 additions & 0 deletions CPP/Problems/Leetcode-287-Find-the-Duplicate-Number.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// https://leetcode.com/problems/find-the-duplicate-number/
class Solution {
public:
int findDuplicate(vector<int>& nums) {

// first thing that comes to my mind is using the set - with O(N) space
// using two for loop (N^2)
// using another array of same size and updating the value if every times
//element is there
int slow=0;
int fast=0;

do {
slow=nums[slow];
fast=nums[nums[fast]];

}while (slow !=fast);

int slow2=0;

while (slow2!=slow){
slow2=nums[slow2];
slow=nums[slow];
}

return slow;

}
};

Original file line number Diff line number Diff line change
@@ -1,36 +1,22 @@
// https://leetcode.com/problems/find-first-and-last-position-of-element-in-sorted-array/
class Solution {
public:
vector<int> searchRange(vector<int>& nums, int target) {
int n=nums.size();
int l=0,r=n-1;
int p1=-1,p2=-1;
while(l<=r){
int mid=l+(r-l)/2;
if(nums[mid]==target){
p1=mid;
r=mid-1;
}
else if(nums[mid]>target){
r=mid-1;
}
else
l=mid+1;
vector<int>ans;
auto low=lower_bound(nums.begin(),nums.end(),target);
auto upp=upper_bound(nums.begin(),nums.end(),target);
if(low==nums.end()|| *low!=target )
{
ans.push_back(-1);
ans.push_back(-1);
}
l=0,r=n-1;
while(l<=r){
int mid=l+(r-l)/2;
if(nums[mid]>=target+1){
r=mid-1;
}
else{

l=mid+1;
}

else
{
int start=low-nums.begin();
int end=upp-nums.begin()-1;
ans.push_back(start);
ans.push_back(end);
}
p2=l;
if(p1!=-1)
return {p1,p2-1};
return {-1,-1};
return ans;
}
};
Loading