Skip to content
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

merging #1

Merged
merged 17 commits into from
Oct 2, 2020
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
190 changes: 190 additions & 0 deletions C++/Hashmaps/hashmap.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
/*
* Copyright 2020 Ujjwal
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include<string>
#include<stdio.h>
using namespace std;
template <class V>

class mapNode{
public:
string key;
V value;
mapNode *next;

mapNode(string key,V value){
this->key = key;
this->value = value;
next = NULL;
}

~mapNode(){
delete next;
}
};

template <class V>
class ourmap
{
mapNode<V> **buckets;
int count;
int numBuckets;

public:
ourmap(){
count = 0;
numBuckets = 5;
buckets = new mapNode<V> *[numBuckets];

for (int i = 0; i < numBuckets;i++)
{
buckets[i] = NULL;
}
}
~ourmap() {
for (int i = 0; i < numBuckets;i++)
{
delete buckets[i];
}
delete[] buckets;
}

// occupied in bucket array
int size(){
return count;
}

// get value corresponding to key
V getValue(string key){
int bucketIndex = getBucketIndex(key);
mapNode<V> *head = buckets[bucketIndex];
while(head!=NULL)
{
if(head->key==key)
return head->value;

head = head->next;
}
return 0;
}

// hashcode and compression function
private:
int getBucketIndex(string key){
int hashCode=0;

int currentCoefficient = 1;
for (int i = key.length() - 1; i >= 0;i--)
{
hashCode += key[i] * currentCoefficient;
hashCode = hashCode % numBuckets;
currentCoefficient *= 37;
currentCoefficient = currentCoefficient % numBuckets;
}
return hashCode % numBuckets;
}

// rehashing function
void rehash(){
mapNode<V> **temp = buckets;
buckets = new mapNode<V> *[2 * numBuckets];
for (int i = 0; i < 2 * numBuckets;i++)
{
buckets[i] = NULL;
}
int oldBucketCount = numBuckets;
numBuckets *= 2;
count = 0;

for (int i = 0; i < oldBucketCount;i++)
{
mapNode<V> *head = temp[i];
while(head!=NULL)
{
string key = head->key;
V value = head->value;
insert(key, value);
head = head->next;
}
}

for (int i = 0; i < oldBucketCount;i++)
{
mapNode<V> *head = temp[i];
delete head;
}
delete[] temp;
}

public:
double getLoadFactor(){
return (1.0 * count) / numBuckets;
}

// inserting element
void insert(string key, V value){
int bucketIndex = getBucketIndex(key);
mapNode<V> *head = buckets[bucketIndex];
while(head!=NULL)
{
if(head->key==key)
{
head->value = value;
return;

}
head = head->next;
}
head = buckets[bucketIndex];
mapNode<V> *node = new mapNode<V>(key, value);

node->next = head;
buckets[bucketIndex] = node;

count++;

//rehashing
double loadfactor = (1.0 * count) / numBuckets;
if(loadfactor>0.7)
rehash();
}

// remove the element
V remove(string key){
int bucketIndex = getBucketIndex(key);
mapNode<V> *head = buckets[bucketIndex];
mapNode<V> *prev = NULL;

while(head!=NULL)
{
if(head->key==key)
{ if(prev==NULL)
buckets[bucketIndex] = head->next ;
else
prev->next = head->next;

V value = head->value;
head->next = NULL;
delete head;
count--;
return value;
}
prev = head;
head = head->next;
}
return 0;
}
};
40 changes: 40 additions & 0 deletions C++/competitive/LeetCode/Best-Time-to-Buy-and-Sell-Stock-II.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright 2020 Avinash Trivedi
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.


class Solution
{
public:
int maxProfit(vector<int> &prices)
{
int sum = 0;
//If the number of element in the array are zero or one just return zero.
if (prices.size() == 1 or prices.size() == 0)
{
return 0;
}

//Traverse the array and compare the consecutive two elements .
for (int i = 0; i < prices.size() - 1; i++)
{
// If first consecuitve element is less than second subtract both and add in the sum varibale.
if (prices[i] < prices[i + 1])
{
sum += prices[i + 1] - prices[i];

}
}

//Finally return the sum.
return sum;

}
};
40 changes: 40 additions & 0 deletions C++/competitive/LeetCode/maximum-subarray.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright 2020 Avinash Trivedi
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

class Solution
{
public:
int maxSubArray(vector<int> &nums)
{
int cs = 0;
int ms = INT_MIN;
// if size of array is 1 return that elemnt .
if (nums.size() == 1)
{
return nums[0];
}

//Linearly traverse the array and add to the current sum variable and take maximum .
for (int i = 0; i < nums.size(); i++)
{
cs = cs + nums[i];

ms = max(cs, ms);
if (cs < 0)
{
cs = 0;
}
}

return ms;
}
};

65 changes: 65 additions & 0 deletions C++/competitive/SPOJ/CSUMQ-Cumulative-Sum-Query.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// Copyright 2020 Avinash Trivedi
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.


#include <iostream>


#include <bits/stdc++.h>

using namespace std;
#define pb push_back
#define intt long long
#define fori(n) for (ll i = 0; i < n; i++)
#define forj(n) for (ll j = 0; j < n; j++)
#define vl vector<ll>
#define mod 1000000007
#define mp make_pair
#define vp vector<pair<int, int>>

int main()
{


intt n;
cin >> n;
intt a[n];
//Taking the input array

for (intt i = 0; i < n; i++)
{
cin >> a[i];
}
//The next k lines each contain two numbers x and y which specify a query you must answer.

intt k;
cin >> k;
for (intt i = 0; i < k; i++)
{ //Range of query x,y.

intt x, y;
cin >> x >> y;
//Declared the sum variable

intt sum = 0;
//Traverse the array in the range x,y and keep adding the sum of elments which are in between the query range.

for (intt i = x; i <= y; i++)
{
sum += a[i];
}
//Finally return the sum of those elements.
cout << sum << endl;
}

return 0;
}

55 changes: 55 additions & 0 deletions C++/search/linear_search.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
Copyright [2020] [Kartikey Sharma]

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

#include<bits/stdc++.h>
using namespace std;

int search(vector<int> v, int find)
{
int l= v.size();
for(int i=0; i<l ; i++)
{
//return the position of the element if found
//for multiple occurences, it will only return the position of the first occurence
if(find==v[i])
return i;
}
//if not found in the array
return -1;
}

int main()
{
vector<int> v;
//array for example is: 1 3 5 2 4 6 7 9 8
v.push_back(1);v.push_back(3);v.push_back(5);
v.push_back(2);v.push_back(4);v.push_back(6);
v.push_back(7);v.push_back(9);v.push_back(8);

int n;
//to search this element
cin>>n;

int l= search(v,n);
if(l==-1)
cout<<"Not found\n";
else
cout<<n<<" is found at position "<<l<<endl;

return 0;
}


Loading