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
39 changes: 39 additions & 0 deletions C++/Introduction/Variable_Sized_Array.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#include <iostream>

using namespace std;

int main(int argc, char *argv[]) {
int n;
int q;
cin >> n >> q;

// Create an array of pointers to integer arrays
// (i.e., an array of variable-length arrays)
int** outer = new int*[n];

// Fill each index of 'outer' with a variable-length array
for(int i = 0; i < n; i++) {
int k;
cin >> k;
// Create an array of length 'k' at index 'i'
outer[i] = new int[k];

// Fill each cell in the 'inner' variable-length array
for(int j = 0; j < k; j++) {
cin >> outer[i][j];
}
}

// Perform queries:
while(q-- > 0) {
int outer_index;
int inner_index;
cin >> outer_index >> inner_index;

// Find the variable-length array located at outer_index
// and print the value of the element at inner_index.
cout << outer[outer_index][inner_index] << endl;
}

return 0;
}
89 changes: 89 additions & 0 deletions C++/STL/Deque.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
#include <iostream>
#include <deque>
using namespace std;
/*void printKMax(int arr[], int n, int k){
//Write your code here.
int max=0,i=0;
deque<int> Q;
//constructing the queue
while(Q.size()!=k)
{
Q.push_front(arr[i]);
i++;
}
//iterating over the queue
while(i<=n){
//maxima
max=0;
deque<int>::iterator iter;
for(iter=Q.begin();iter!=Q.end();iter++){
if(*iter>=max) max=*iter;
}
cout<<max<<" ";
Q.push_front(arr[i]);
Q.pop_back();
i++;
}
cout<<"\n";
}*/
void printKMax(int arr[], int n, int k)
{
// Create a Double Ended Queue, Qi that will store indexes of array elements
// The queue will store indexes of useful elements in every window and it will
// maintain decreasing order of values from front to rear in Qi, i.e.,
// arr[Qi.front[]] to arr[Qi.rear()] are sorted in decreasing order
std::deque<int> Qi(k);

/* Process first k (or first window) elements of array */
int i;
for (i = 0; i < k; ++i)
{
// For very element, the previous smaller elements are useless so
// remove them from Qi
while ( (!Qi.empty()) && arr[i] >= arr[Qi.back()])
Qi.pop_back(); // Remove from rear

// Add new element at rear of queue
Qi.push_back(i);
}

// Process rest of the elements, i.e., from arr[k] to arr[n-1]
for ( ; i < n; ++i)
{
// The element at the front of the queue is the largest element of
// previous window, so print it
cout << arr[Qi.front()] << " ";

// Remove the elements which are out of this window
while ( (!Qi.empty()) && Qi.front() <= i - k)
Qi.pop_front(); // Remove from front of queue

// Remove all elements smaller than the currently
// being added element (remove useless elements)
while ( (!Qi.empty()) && arr[i] >= arr[Qi.back()])
Qi.pop_back();

// Add current element at the rear of Qi
Qi.push_back(i);
}

// Print the maximum element of last window
cout << arr[Qi.front()]<<endl;
}
int main(){

int t;
cin >> t;
while(t>0) {
int n,k;
cin >> n >> k;
int i;
int arr[n];
for(i=0;i<n;i++)
cin >> arr[i];
printKMax(arr, n, k);
t--;
}
return 0;
}

34 changes: 34 additions & 0 deletions C++/Strings/StringStream.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include <sstream>
#include <vector>
#include <iostream>
using namespace std;

vector<int> parseInts(string str) {
// Complete this function
vector<int> v1;
int i=0;
stringstream ss;
ss <<str<< ".";
int a;
char ch;
ss>>a>>ch;
v1.push_back(a);
while(ch==','){
ss>>a>>ch;
v1.push_back(a);
//i++;
}
return v1;
}

int main() {
string str;
cin >> str;
vector<int> integers = parseInts(str);
for(int i = 0; i < integers.size(); i++) {
cout << integers[i] << "\n";
}

return 0;
}
1
75 changes: 75 additions & 0 deletions C++/Strings/attribut_parser.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <map>
#include <deque>
#include <sstream>
#include <stdio.h>
using namespace std;


int main() {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
int n,q;
char ch;
map <string,string> mp;
scanf("%d %d\n",&n,&q);
string s,s1,s2,s3;
stringstream ss,ss1;
deque<string> dq;
while(n--){
getline(cin,s);
ss<<s.substr(1,s.size()-2)<<" tumera - were ";
ss>>s1;
//cout<<s1<<endl;
if(s1[0]=='/'){

dq.pop_back();
}
else{
// cout<<"AG.....";
dq.push_back(s1);
for(int i=0;i<dq.size()-1;i++)
{
ss1<<dq[i]<<".";
}
ss1<<dq[dq.size()-1];
//cout<<ss1.str()<<"\n";
// cout<<"...."+s1+"....";
while(1)
{
ss>>s2>>ch>>s3;
if(ch=='-') break;
mp.insert(make_pair(ss1.str()+"~"+s2,s3));
//cout<<ss1.str()+"~"+s2<<" "<<s3<<endl;
}
}
ss.str("");
ss.clear();
ss1.str("");
ss1.clear();
}
for(int i=0;i<dq.size();i++){
cout<<dq[i]<<".";
}
map<string,string>::iterator iter;
/*for(iter=mp.begin();iter!=mp.end();iter++){
cout<<iter->first<<endl;
}*/
while(q--){
string str2;
//getline(cin)
cin>>str2;
if(mp.find(str2)!=mp.end()){
string t=mp.find(str2)->second;
cout<<(mp.find(str2)->second).substr(1,t.size()-2)<<endl;
}
else{
cout<<"Not Found!"<<endl;
}
}
//cout<<ss.str();
return 0;
}
52 changes: 52 additions & 0 deletions algorithms/DP/sherlock_and_cost.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
nclude <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
//#include <iostream>
int max(int a,int b){
if(a<b) return b;
return a;
}
int main() {
int t;
scanf("%d",&t);
while(t--){
int n;
scanf("%d",&n);
int *v=(int *)calloc(n,sizeof(int));
for(int i=0;i<n;i++){
scanf("%d",v+i);
}
int **arr=(int **)calloc(2,sizeof(int*));
arr[0]=(int *)calloc(n-1,sizeof(int));
arr[1]=(int*)calloc(n-1,sizeof(int));
arr[0][0]=abs(v[0]-1);
arr[1][0]=max(abs(v[1]-1),abs(v[1]-v[0]));
//printf("%d %d ",arr[0][0],arr[1][0]);
for(int i=1;i<n-1;i++){
arr[0][i]=max(arr[0][i-1],abs(v[i]-1)+arr[1][i-1]);
arr[1][i]=max(arr[0][i-1]+abs(v[i+1]-1),arr[1][i-1]+abs(v[i]-v[i+1]));
// printf("%d %d_%d ",arr[0][i],arr[1][i],v[i+1]);
}
printf("%d\n",max(arr[0][n-2],arr[1][n-2]));

}
/*int t;
scanf("%d",&t);
while(t--){
int n;
scanf("%d",&n);
for(int i=0;i<n;i++){

}






}*/
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
return 0;
}