diff --git a/Algorithms/3_sliding_window/p1.cpp b/Algorithms/3_sliding_window/p1.cpp index 5099ffd..ed73fa4 100644 --- a/Algorithms/3_sliding_window/p1.cpp +++ b/Algorithms/3_sliding_window/p1.cpp @@ -39,7 +39,7 @@ void file(){ #endif } -int bs(int a[] , int s , int e, int x){ +int binary_search(int a[] , int s , int e, int x){ if(s<=e){ int m = (s+e)/2; if(a[m]==x) @@ -79,27 +79,6 @@ int mypow(int a, int b){ //(logn) fast exponentiation } return (res%mod); } -/* -5 -8 6 -1 2 4 1 2 4 1 2 -5 3 -3 2 3 2 1 -10 4 -4 3 4 3 2 3 2 1 0 1 -15 7 -3 7 4 8 2 3 4 5 21 2 3 4 2 1 3 -7 5 -1 2 3 4 5 6 1 - -3 2 -2 2 -2 1 -3 1 -2 3 - -*/ - int32_t main(){ __; @@ -127,16 +106,3 @@ int32_t main(){ } return 0; } - - - -//add native library support -/* -#include "window.h" -using namespace_t; - -typedef struct window_t{ - int n; -}; - -*/ diff --git a/Algorithms/4_two_pointer/probs.txt b/Algorithms/4_two_pointer/probs.txt index bd2477d..b5576a0 100644 --- a/Algorithms/4_two_pointer/probs.txt +++ b/Algorithms/4_two_pointer/probs.txt @@ -11,7 +11,7 @@ 5. Motivation Problem: Given an array having N integers, you need to find out a subsequence of K integers such that these K integers have the minimum hustle. Hustle of a sequence is defined as sum of pair-wise absolute differences divided by the number of pairs. -For details on the statement, refer the problem link codeforces.com/problemset/problem/371/E +For details on the statement, please refer the problem link codeforces.com/problemset/problem/371/E diff --git a/Algorithms/binary_search/probs.txt b/Algorithms/binary_search/probs.txt index 03956b5..fc260ea 100644 --- a/Algorithms/binary_search/probs.txt +++ b/Algorithms/binary_search/probs.txt @@ -1 +1,2 @@ https://codeforces.com/problemset/problem/706/B +///// diff --git a/Algorithms/prefix_sum/1D/p3.cpp b/Algorithms/prefix_sum/1D/p3.cpp index f5d622b..c6271d5 100644 --- a/Algorithms/prefix_sum/1D/p3.cpp +++ b/Algorithms/prefix_sum/1D/p3.cpp @@ -2,74 +2,6 @@ #include using namespace std; -#define ff first -#define ss second -#define int long long -#define pb push_back -#define mp make_pair -#define pii pair -#define vi vector -#define mii map -#define pqb priority_queue -#define pqs priority_queue > -#define setbits(x) __builtin_popcountll(x) -#define zrobits(x) __builtin_ctzll(x) -#define mod 1000000007 -#define inf 1e18 -#define ps(x,y) fixed<>x; while(x--) -#define pw(b,p) pow(b,p) + 0.1 -#define __ ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); -#define rep(i,a,b) for(int i=a;i=b;i--) -#define endl "\n" -#define m(a) memset(a,0,sizeof(a)) -#define all(x) begin(x), end(x) - -void fastscan(int &x){ -bool neg = false;register int c; -x = 0;c = getchar(); -if(c=='-'){neg = true;c = getchar();} -for(; (c>47 && c<58); c=getchar()){x = x *10 + c - 48;} -if(neg){x *= -1;}} - -void file(){ - #ifndef ONLINE_JUDGE - freopen("in.txt" , "r" , stdin); - freopen("out.txt" , "w" , stdout); - #endif -} - -int bs(int a[] , int s , int e, int x){ - if(s<=e){ - int m = (s+e)/2; - if(a[m]==x) - return m; - else if(a[m] > x) - return bs(a,s,m-1,x); - else return bs(a,m+1,e,x); - } - return -1; -} - -#define tr(x) cout< blog for problems.. +https://codeforces.com/blog/entry/59915?locale=en => blog for problems...... diff --git a/AtCoder/ABC/153/A.cpp b/AtCoder/ABC/153/A.cpp new file mode 100644 index 0000000..eaeeef2 --- /dev/null +++ b/AtCoder/ABC/153/A.cpp @@ -0,0 +1,13 @@ +#include +using namespace std; + +int main() +{ + int a,b; + cin>>a>>b; + int ans = a/b; + if(a%b) + ans++; + cout< +using namespace std; + +int main() +{ + int x,n,sum,y; + cin>>x>>n; + sum = 0; + vector v(n); + for(int i=0;i>y; + sum+=y; + } + if(sum>=x) + cout<<"Yes"; + else + cout<<"No"; + return 0; +} \ No newline at end of file diff --git a/AtCoder/ABC/153/C.cpp b/AtCoder/ABC/153/C.cpp new file mode 100644 index 0000000..6664e5a --- /dev/null +++ b/AtCoder/ABC/153/C.cpp @@ -0,0 +1,25 @@ +#include +using namespace std; +#define ll long long + +int main() +{ + ll n,k; + cin>>n>>k; + vector v(n); + for(int i=0;i>v[i]; + sort(v.begin(),v.end()); + ll ans = 0; + for(int i=(int)v.size()-1;i>=0;i--) + { + if(k){ + k--; + } + else{ + ans+=v[i]; + } + } + cout< +using namespace std; +#define ll long long + +ll func(ll h){ + if(h==1) + return 1; + return 1+2*func(h/2); +} + +int main(){ + ll h; + cin>>h; + cout< +using namespace std; +#define ll long long +#define INF 0x3f3f3f3f + +vector a,b; +vector> dp; + +ll func(ll idx, int health){ + if(health<=0) + return 0; + if(idx==-1) + return INF; + if(dp[idx][health]!=-1) + return dp[idx][health]; + return dp[idx][health] = min(b[idx]+func(idx, health-a[idx]), func(idx-1,health)); +} + +int main() +{ + ll h,n; + cin>>h>>n; + a.resize(n); + b.resize(n); + dp.resize(n+1, vector (h+1,-1)); + for(int i=0;i>a[i]; + cin>>b[i]; + } + cout< +using namespace std; + +int main(){ + ll n,d,a,y,z; + cin>>n>>d>>a; + vector x(n); + vector> v(n); + for(int i=0;i>y; + cin>>z; + v[i]={y,z}; + } + sort(v.begin(), v.end()); + vector times(n); + for(int i=0;i far(n,0); + for(int i=0;i damage(n+2,0); + for(int i=0;i using namespace std; -void fastscan(int &number){ -bool negative = false; -register int c; -number = 0; -c = getchar(); -if(c=='-'){ -negative = true;c = getchar();} -for (; (c>47 && c<58); c=getchar()){ -number = number *10 + c - 48;} -if(negative){number *= -1;} -} - -#define ff first -#define ss second -#define int long long -#define pb push_back -#define mp make_pair -#define pii pair -#define vi vector -#define mii map -#define pqb priority_queue -#define pqs priority_queue > -#define setbits(x) __builtin_popcountll(x) -#define zrobits(x) __builtin_ctzll(x) -#define mod 1000000007 -#define inf 1e18 -#define ps(x,y) fixed<>x; while(x--) -#define pw(b,p) pow(b,p) + 0.1 -#define _fast_ ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); - void file(){ #ifndef ONLINE_JUDGE diff --git a/CodeChef/practice/CHEFEZQ b/CodeChef/practice/CHEFEZQ new file mode 100644 index 0000000..d4e9cb9 --- /dev/null +++ b/CodeChef/practice/CHEFEZQ @@ -0,0 +1,25 @@ +#include +using namespace std; +int main(){ + int t; + cin>>t; + while(t--){ + long int n,k,i,ans; + int flag=0; + long long int sum=0; + cin>>n>>k; + long int ar[n]; + for(i=0;i>ar[i]; + for(i=0;i +using namespace std; +int main(){ + int t; + cin>>t; + while(t--){ + long int n,i,last_two=1,temp=4; + cin>>n; + long int ar[n]; + while(last_two*2<=n) last_two*=2; + if(n==1)cout<<1; + else if(n==3)cout<<"1 3 2"; + else if(last_two==n)cout<<-1< +#define mod 1000000007 +using namespace std; + +int index(int A[], int N, int X, int P) +{ + int minm = 100000000, min_index=0; + for(int k=0;k> N >> X >> P>> K; + int A[N]; + for(int i=0;i> A[i]; + sort(A,A+N); + int I = index(A, N, X, P); + int c = 0; + if(A[I]!=X) + { + A[K-1]=X; + sort(A,A+N); + c++; + } + if(A[P-1]==X) + { + cout<<0+c; + return; + } + if(PK&&A[P-1]>X) + { + cout << -1; + return; + } + I=index(A,N,X,P)+1; + cout << abs(P-I)+c; + +} + +int main() +{ + int t; + cin >> t; + while(t--) + { + solve(); + cout << "\n"; + } + return 0; +} diff --git a/CodeChef/practice/easy/Broken_Telephone.cpp b/CodeChef/practice/easy/Broken_Telephone.cpp new file mode 100644 index 0000000..80fa52b --- /dev/null +++ b/CodeChef/practice/easy/Broken_Telephone.cpp @@ -0,0 +1,39 @@ +#include +using namespace std; +#define ll long long +void solve(); +void scan(int arr [] , int n) +{for(int i=0; i>arr[i];} +void print(int arr [] , int n) +{for(int i=0; i>t; +while(t--) +{ +solve(); +} +return 0; +} +void solve() +{ + int n; + cin>>n; + int arr[n]; + scan(arr,n); + int ans=0; + for(int i=1; i2){ + if(arr[n-1]!=arr[n-2])ans++; + } + cout<> s; + if(s[2] == s[3] && s[4]==s[5]) + cout << "Yes" << endl; + else + cout << "No" << endl; + return 0; + +} diff --git a/Companies wise Leetcode Premium Question/Adobe - LeetCode.pdf b/Companies wise Leetcode Premium Question/Adobe - LeetCode.pdf new file mode 100644 index 0000000..54a2fb9 Binary files /dev/null and b/Companies wise Leetcode Premium Question/Adobe - LeetCode.pdf differ diff --git a/Companies wise Leetcode Premium Question/Amazon - LeetCode.pdf b/Companies wise Leetcode Premium Question/Amazon - LeetCode.pdf new file mode 100644 index 0000000..9e2e6fc Binary files /dev/null and b/Companies wise Leetcode Premium Question/Amazon - LeetCode.pdf differ diff --git a/Companies wise Leetcode Premium Question/Apple - LeetCode.pdf b/Companies wise Leetcode Premium Question/Apple - LeetCode.pdf new file mode 100644 index 0000000..f219a1c Binary files /dev/null and b/Companies wise Leetcode Premium Question/Apple - LeetCode.pdf differ diff --git a/Companies wise Leetcode Premium Question/Cisco - LeetCode.pdf b/Companies wise Leetcode Premium Question/Cisco - LeetCode.pdf new file mode 100644 index 0000000..f2ed910 Binary files /dev/null and b/Companies wise Leetcode Premium Question/Cisco - LeetCode.pdf differ diff --git a/Companies wise Leetcode Premium Question/Databricks - LeetCode.pdf b/Companies wise Leetcode Premium Question/Databricks - LeetCode.pdf new file mode 100644 index 0000000..6e233fb Binary files /dev/null and b/Companies wise Leetcode Premium Question/Databricks - LeetCode.pdf differ diff --git a/Companies wise Leetcode Premium Question/Expedia - LeetCode.pdf b/Companies wise Leetcode Premium Question/Expedia - LeetCode.pdf new file mode 100644 index 0000000..2adc85b Binary files /dev/null and b/Companies wise Leetcode Premium Question/Expedia - LeetCode.pdf differ diff --git a/Companies wise Leetcode Premium Question/Facebook - LeetCode.pdf b/Companies wise Leetcode Premium Question/Facebook - LeetCode.pdf new file mode 100644 index 0000000..22317a7 Binary files /dev/null and b/Companies wise Leetcode Premium Question/Facebook - LeetCode.pdf differ diff --git a/Companies wise Leetcode Premium Question/Flipkart - LeetCode.pdf b/Companies wise Leetcode Premium Question/Flipkart - LeetCode.pdf new file mode 100644 index 0000000..4127d66 Binary files /dev/null and b/Companies wise Leetcode Premium Question/Flipkart - LeetCode.pdf differ diff --git a/Companies wise Leetcode Premium Question/GoDaddy - LeetCode.pdf b/Companies wise Leetcode Premium Question/GoDaddy - LeetCode.pdf new file mode 100644 index 0000000..bb91af9 Binary files /dev/null and b/Companies wise Leetcode Premium Question/GoDaddy - LeetCode.pdf differ diff --git a/Companies wise Leetcode Premium Question/Goldman Sachs - LeetCode.pdf b/Companies wise Leetcode Premium Question/Goldman Sachs - LeetCode.pdf new file mode 100644 index 0000000..912ed39 Binary files /dev/null and b/Companies wise Leetcode Premium Question/Goldman Sachs - LeetCode.pdf differ diff --git a/Companies wise Leetcode Premium Question/Google - LeetCode.pdf b/Companies wise Leetcode Premium Question/Google - LeetCode.pdf new file mode 100644 index 0000000..5e5790a Binary files /dev/null and b/Companies wise Leetcode Premium Question/Google - LeetCode.pdf differ diff --git a/Companies wise Leetcode Premium Question/Huawei - LeetCode.pdf b/Companies wise Leetcode Premium Question/Huawei - LeetCode.pdf new file mode 100644 index 0000000..2ce9f02 Binary files /dev/null and b/Companies wise Leetcode Premium Question/Huawei - LeetCode.pdf differ diff --git a/Companies wise Leetcode Premium Question/IBM - LeetCode.pdf b/Companies wise Leetcode Premium Question/IBM - LeetCode.pdf new file mode 100644 index 0000000..6bc416a Binary files /dev/null and b/Companies wise Leetcode Premium Question/IBM - LeetCode.pdf differ diff --git a/Companies wise Leetcode Premium Question/Intel - LeetCode.pdf b/Companies wise Leetcode Premium Question/Intel - LeetCode.pdf new file mode 100644 index 0000000..3bb42cd Binary files /dev/null and b/Companies wise Leetcode Premium Question/Intel - LeetCode.pdf differ diff --git a/Companies wise Leetcode Premium Question/Intuit - LeetCode.pdf b/Companies wise Leetcode Premium Question/Intuit - LeetCode.pdf new file mode 100644 index 0000000..3683729 Binary files /dev/null and b/Companies wise Leetcode Premium Question/Intuit - LeetCode.pdf differ diff --git a/Companies wise Leetcode Premium Question/JPMorgan - LeetCode.pdf b/Companies wise Leetcode Premium Question/JPMorgan - LeetCode.pdf new file mode 100644 index 0000000..5d857de Binary files /dev/null and b/Companies wise Leetcode Premium Question/JPMorgan - LeetCode.pdf differ diff --git a/Companies wise Leetcode Premium Question/LinkedIn - LeetCode.pdf b/Companies wise Leetcode Premium Question/LinkedIn - LeetCode.pdf new file mode 100644 index 0000000..f9e7fe5 Binary files /dev/null and b/Companies wise Leetcode Premium Question/LinkedIn - LeetCode.pdf differ diff --git a/Companies wise Leetcode Premium Question/Microsoft - LeetCode.pdf b/Companies wise Leetcode Premium Question/Microsoft - LeetCode.pdf new file mode 100644 index 0000000..670fa27 Binary files /dev/null and b/Companies wise Leetcode Premium Question/Microsoft - LeetCode.pdf differ diff --git a/Companies wise Leetcode Premium Question/Morgan Stanley - LeetCode.pdf b/Companies wise Leetcode Premium Question/Morgan Stanley - LeetCode.pdf new file mode 100644 index 0000000..086f592 Binary files /dev/null and b/Companies wise Leetcode Premium Question/Morgan Stanley - LeetCode.pdf differ diff --git a/Companies wise Leetcode Premium Question/Netflix - LeetCode.pdf b/Companies wise Leetcode Premium Question/Netflix - LeetCode.pdf new file mode 100644 index 0000000..e7e2442 Binary files /dev/null and b/Companies wise Leetcode Premium Question/Netflix - LeetCode.pdf differ diff --git a/Companies wise Leetcode Premium Question/Nvidia - LeetCode.pdf b/Companies wise Leetcode Premium Question/Nvidia - LeetCode.pdf new file mode 100644 index 0000000..e5c4a11 Binary files /dev/null and b/Companies wise Leetcode Premium Question/Nvidia - LeetCode.pdf differ diff --git a/Companies wise Leetcode Premium Question/Oracle - LeetCode.pdf b/Companies wise Leetcode Premium Question/Oracle - LeetCode.pdf new file mode 100644 index 0000000..e2a65a6 Binary files /dev/null and b/Companies wise Leetcode Premium Question/Oracle - LeetCode.pdf differ diff --git a/Companies wise Leetcode Premium Question/Paypal - LeetCode.pdf b/Companies wise Leetcode Premium Question/Paypal - LeetCode.pdf new file mode 100644 index 0000000..11397e6 Binary files /dev/null and b/Companies wise Leetcode Premium Question/Paypal - LeetCode.pdf differ diff --git a/Companies wise Leetcode Premium Question/Qualcomm - LeetCode.pdf b/Companies wise Leetcode Premium Question/Qualcomm - LeetCode.pdf new file mode 100644 index 0000000..6cb126d Binary files /dev/null and b/Companies wise Leetcode Premium Question/Qualcomm - LeetCode.pdf differ diff --git a/Companies wise Leetcode Premium Question/SAP - LeetCode.pdf b/Companies wise Leetcode Premium Question/SAP - LeetCode.pdf new file mode 100644 index 0000000..6919ae4 Binary files /dev/null and b/Companies wise Leetcode Premium Question/SAP - LeetCode.pdf differ diff --git a/Companies wise Leetcode Premium Question/Salesforce - LeetCode.pdf b/Companies wise Leetcode Premium Question/Salesforce - LeetCode.pdf new file mode 100644 index 0000000..11f2562 Binary files /dev/null and b/Companies wise Leetcode Premium Question/Salesforce - LeetCode.pdf differ diff --git a/Companies wise Leetcode Premium Question/Samsung - LeetCode.pdf b/Companies wise Leetcode Premium Question/Samsung - LeetCode.pdf new file mode 100644 index 0000000..cbaf7d2 Binary files /dev/null and b/Companies wise Leetcode Premium Question/Samsung - LeetCode.pdf differ diff --git a/Companies wise Leetcode Premium Question/Uber - LeetCode.pdf b/Companies wise Leetcode Premium Question/Uber - LeetCode.pdf new file mode 100644 index 0000000..51bec46 Binary files /dev/null and b/Companies wise Leetcode Premium Question/Uber - LeetCode.pdf differ diff --git a/Companies wise Leetcode Premium Question/VMware - LeetCode.pdf b/Companies wise Leetcode Premium Question/VMware - LeetCode.pdf new file mode 100644 index 0000000..538e762 Binary files /dev/null and b/Companies wise Leetcode Premium Question/VMware - LeetCode.pdf differ diff --git a/Companies wise Leetcode Premium Question/Visa - LeetCode.pdf b/Companies wise Leetcode Premium Question/Visa - LeetCode.pdf new file mode 100644 index 0000000..85bacd0 Binary files /dev/null and b/Companies wise Leetcode Premium Question/Visa - LeetCode.pdf differ diff --git a/Companies wise Leetcode Premium Question/Walmart Labs - LeetCode.pdf b/Companies wise Leetcode Premium Question/Walmart Labs - LeetCode.pdf new file mode 100644 index 0000000..4ee047f Binary files /dev/null and b/Companies wise Leetcode Premium Question/Walmart Labs - LeetCode.pdf differ diff --git a/Companies wise Leetcode Premium Question/Yahoo - LeetCode.pdf b/Companies wise Leetcode Premium Question/Yahoo - LeetCode.pdf new file mode 100644 index 0000000..09a3d01 Binary files /dev/null and b/Companies wise Leetcode Premium Question/Yahoo - LeetCode.pdf differ diff --git a/Companies wise Leetcode Premium Question/druva - LeetCode.pdf b/Companies wise Leetcode Premium Question/druva - LeetCode.pdf new file mode 100644 index 0000000..36d29b3 Binary files /dev/null and b/Companies wise Leetcode Premium Question/druva - LeetCode.pdf differ diff --git a/Companies wise Leetcode Premium Question/snapdeal - LeetCode.pdf b/Companies wise Leetcode Premium Question/snapdeal - LeetCode.pdf new file mode 100644 index 0000000..7a72964 Binary files /dev/null and b/Companies wise Leetcode Premium Question/snapdeal - LeetCode.pdf differ diff --git a/GFG/Queue/deq.cpp b/GFG/Queue/deq.cpp index 40d19e4..1dde238 100644 --- a/GFG/Queue/deq.cpp +++ b/GFG/Queue/deq.cpp @@ -33,14 +33,14 @@ public : int getRear(); }; -// Checks whether Deque is full or not. +// Checks whether Deque is full bool Deque::isFull() { return ((front == 0 && rear == size-1)|| front == rear+1); } -// Checks whether Deque is empty or not. +// Checks whether Deque is empty bool Deque::isEmpty () { return (front == -1); diff --git a/HackerEarth/Connected_Components_in_a_Graph.cpp b/HackerEarth/Connected_Components_in_a_Graph.cpp index 1371b7f..8d0d558 100644 --- a/HackerEarth/Connected_Components_in_a_Graph.cpp +++ b/HackerEarth/Connected_Components_in_a_Graph.cpp @@ -1,7 +1,8 @@ -/* Author +/*Author @ Pranjal Walia IIIT Bangalore */ + // https://www.hackerearth.com/problem/algorithm/connected-components-in-a-graph/description/ #include using namespace std; diff --git a/Hackerrank/language/C/Post Transition.c b/Hackerrank/language/C/Post Transition.c new file mode 100644 index 0000000..d87a5e6 --- /dev/null +++ b/Hackerrank/language/C/Post Transition.c @@ -0,0 +1,149 @@ +#include +#include +#include +#define MAX_STRING_LENGTH 6 + +struct package +{ + char* id; + int weight; +}; + +typedef struct package package; + +struct post_office +{ + int min_weight; + int max_weight; + package* packages; + int packages_count; +}; + +typedef struct post_office post_office; + +struct town +{ + char* name; + post_office* offices; + int offices_count; +}; + +typedef struct town town; + + + +void print_all_packages(town t) { + printf("%s:\n", t.name); + for(int i=0; ioffices[source_office_index].packages_count; i++){ + mxg=source->offices[source_office_index].packages[i].weight; + min=target->offices[target_office_index].min_weight; + max=target->offices[target_office_index].max_weight; + + if((min<=mxg)&&(mxg<=max)){ + count=target->offices[target_office_index].packages_count; + target->offices[target_office_index].packages=realloc(target->offices[target_office_index].packages, sizeof(package) * (count+1)); + +target->offices[target_office_index].packages[count]=source->offices[source_office_index].packages[i]; + + count=source->offices[source_office_index].packages_count; + k=i; + + while(koffices[source_office_index].packages[k]; +source->offices[source_office_index].packages[k]=source->offices[source_office_index].packages[k+1]; + source->offices[source_office_index].packages[k+1]=temp; + k++; + } + + source->offices[source_office_index].packages= + realloc(source->offices[source_office_index].packages, sizeof(package) * (count-1)); + + target->offices[target_office_index].packages_count++; + source->offices[source_office_index].packages_count--; + i--; + } + } +} +town town_with_most_packages(town* towns, int towns_count) { + int max=-1, sum, r; + while(--towns_count>-1){ + sum=0; + for(int i=0; imax){max=sum;r=towns_count;} + } + + return towns[r]; +} + +town* find_town(town* towns, int towns_count, char* name) { + int i; + while(--towns_count>-1){ + if(strcmp(name, towns[towns_count].name)==0){ + i=towns_count; + } + } + return towns+i; +} +int main() +{ + int towns_count; + scanf("%d", &towns_count); + town* towns = malloc(sizeof(town)*towns_count); + for (int i = 0; i < towns_count; i++) { + towns[i].name = malloc(sizeof(char) * MAX_STRING_LENGTH); + scanf("%s", towns[i].name); + scanf("%d", &towns[i].offices_count); + towns[i].offices = malloc(sizeof(post_office)*towns[i].offices_count); + for (int j = 0; j < towns[i].offices_count; j++) { + scanf("%d%d%d", &towns[i].offices[j].packages_count, &towns[i].offices[j].min_weight, &towns[i].offices[j].max_weight); + towns[i].offices[j].packages = malloc(sizeof(package)*towns[i].offices[j].packages_count); + for (int k = 0; k < towns[i].offices[j].packages_count; k++) { + towns[i].offices[j].packages[k].id = malloc(sizeof(char) * MAX_STRING_LENGTH); + scanf("%s", towns[i].offices[j].packages[k].id); + scanf("%d", &towns[i].offices[j].packages[k].weight); + } + } + } + int queries; + scanf("%d", &queries); + char town_name[MAX_STRING_LENGTH]; + while (queries--) { + int type; + scanf("%d", &type); + switch (type) { + case 1: + scanf("%s", town_name); + town* t = find_town(towns, towns_count, town_name); + print_all_packages(*t); + break; + case 2: + scanf("%s", town_name); + town* source = find_town(towns, towns_count, town_name); + int source_index; + scanf("%d", &source_index); + scanf("%s", town_name); + town* target = find_town(towns, towns_count, town_name); + int target_index; + scanf("%d", &target_index); + send_all_acceptable_packages(source, source_index, target, target_index); + break; + case 3: + printf("Town with the most number of packages is %s\n", town_with_most_packages(towns, towns_count).name); + break; + } + } + return 0; +} diff --git a/Hackerrank/language/C/Printing Pattern using Loops.c b/Hackerrank/language/C/Printing Pattern using Loops.c new file mode 100644 index 0000000..6a6eda7 --- /dev/null +++ b/Hackerrank/language/C/Printing Pattern using Loops.c @@ -0,0 +1,45 @@ +#include +#include +#include +#include +int main() +{ + int n,i,j,f,g; + scanf("%d",&n); + for(i=n;i>=1;i--) + { f=n; + for(j=n;j>=1;j--,f--){ + if(i=1;j--,g++){ + if(i=1;j--,f--){ + if(i=1;j--,g++){ + if(i +#include +#include +#include + +int main() { + + char *s; + s = malloc(1024 * sizeof(char)); + scanf("%[^\n]", s); + s = realloc(s, strlen(s) + 1); + //Write your logic to print the tokens of the sentence here. + for (char *c = s; *c != NULL; c++) { + if (*c == ' ') { + *c = '\n'; + } + } + printf("%s", s); + return 0; +} \ No newline at end of file diff --git a/Hackerrank/language/C/Querying the Document.c b/Hackerrank/language/C/Querying the Document.c new file mode 100644 index 0000000..eac3d7e --- /dev/null +++ b/Hackerrank/language/C/Querying the Document.c @@ -0,0 +1,148 @@ +#include +#include +#include +#include +#define MAX_CHARACTERS 1005 +#define MAX_PARAGRAPHS 5 + +char* kth_word_in_mth_sentence_of_nth_paragraph(char**** document, int k, int m, int n) { + return document[n-1][m-1][k-1]; +} + +char** kth_sentence_in_mth_paragraph(char**** document, int k, int m) { + return document[m-1][k-1]; +} + +char*** kth_paragraph(char**** document, int k) { + return document[k-1]; +} + +char** split_string(char* text, char delim) { + assert(text != NULL); + char** result = malloc(1*sizeof(char*)); + int size = 1; + + char* temp = strtok(text, &delim); + *result = temp; + + while(temp != NULL) { + size++; + result = realloc(result,size*sizeof(char*)); + temp = strtok(NULL, &delim); + result[size-1] = temp; + } + return result; +} + +char**** get_document(char* text) { + assert(text != NULL); + + char** paragraphs = split_string(text, '\n'); + int npar = 0; + while (paragraphs[npar] != NULL) { + npar++; + } + + char**** doc = malloc((npar+1)*sizeof(char***)); + doc[npar] = NULL; + + int i = 0; + while (paragraphs[i] != NULL) { + + char** sentences = split_string(paragraphs[i], '.'); + int nsen = 0; + while(sentences[nsen] != NULL) { + nsen++; + } + + doc[i] = malloc((nsen+1)*sizeof(char**)); + doc[i][nsen] = NULL; + + int j = 0; + while (sentences[j] != NULL) { + doc[i][j] = split_string(sentences[j], ' '); + j++; + } + i++; + } + + return doc; +} + +char* get_input_text() { + int paragraph_count; + scanf("%d", ¶graph_count); + + char p[MAX_PARAGRAPHS][MAX_CHARACTERS], doc[MAX_CHARACTERS]; + memset(doc, 0, sizeof(doc)); + getchar(); + for (int i = 0; i < paragraph_count; i++) { + scanf("%[^\n]%*c", p[i]); + strcat(doc, p[i]); + if (i != paragraph_count - 1) + strcat(doc, "\n"); + } + + char* returnDoc = (char*)malloc((strlen (doc)+1) * (sizeof(char))); + strcpy(returnDoc, doc); + return returnDoc; +} + +void print_word(char* word) { + printf("%s", word); +} + +void print_sentence(char** sentence) { + int word_count; + scanf("%d", &word_count); + for(int i = 0; i < word_count; i++){ + printf("%s", sentence[i]); + if( i != word_count - 1) + printf(" "); + } +} + +void print_paragraph(char*** paragraph) { + int sentence_count; + scanf("%d", &sentence_count); + for (int i = 0; i < sentence_count; i++) { + print_sentence(*(paragraph + i)); + printf("."); + } +} + +int main() +{ + char* text = get_input_text(); + char**** document = get_document(text); + + int q; + scanf("%d", &q); + + while (q--) { + int type; + scanf("%d", &type); + + if (type == 3){ + int k, m, n; + scanf("%d %d %d", &k, &m, &n); + char* word = kth_word_in_mth_sentence_of_nth_paragraph(document, k, m, n); + print_word(word); + } + + else if (type == 2){ + int k, m; + scanf("%d %d", &k, &m); + char** sentence = kth_sentence_in_mth_paragraph(document, k, m); + print_sentence(sentence); + } + + else{ + int k; + scanf("%d", &k); + char*** paragraph = kth_paragraph(document, k); + print_paragraph(paragraph); + } + printf("\n"); + } +} \ No newline at end of file diff --git a/Hackerrank/language/C/Sorting Array of Strings.c b/Hackerrank/language/C/Sorting Array of Strings.c new file mode 100644 index 0000000..532bba5 --- /dev/null +++ b/Hackerrank/language/C/Sorting Array of Strings.c @@ -0,0 +1,82 @@ +#include +#include +#include +int lexicographic_sort(const char* a, const char* b) { + return strcmp(a, b); +} + +int lexicographic_sort_reverse(const char* a, const char* b) { + return strcmp(b, a); +} + +int distinct_chars(const char *a) +{ + int dist = 0; + + while (*a != '\0') { + if (!strchr(a + 1, *a)) + dist++; + a++; + } + return dist; +} + +int sort_by_number_of_distinct_characters(const char* a, const char* b) { + int res = distinct_chars(a) - distinct_chars(b); + return (res) ? res : lexicographic_sort(a, b); +} + +int sort_by_length(const char* a, const char* b) { + int res = strlen(a) - strlen(b); + return (res) ? res : lexicographic_sort(a, b); +} + +void string_sort(char** arr, const int len,int (*cmp_func)(const char* a, const char* b)) { + int sorted = 0; + while (!sorted) { + sorted = 1; + for (int i = 0; i < len - 1; i++) { + if (cmp_func(arr[i], arr[i + 1]) > 0) { + char *tmp = arr[i]; + arr[i] = arr[i + 1]; + arr[i + 1] = tmp; + sorted = 0; + } + } + } +} + +int main() +{ + int n; + scanf("%d", &n); + + char** arr; + arr = (char**)malloc(n * sizeof(char*)); + + for(int i = 0; i < n; i++){ + *(arr + i) = malloc(1024 * sizeof(char)); + scanf("%s", *(arr + i)); + *(arr + i) = realloc(*(arr + i), strlen(*(arr + i)) + 1); + } + + string_sort(arr, n, lexicographic_sort); + for(int i = 0; i < n; i++) + printf("%s\n", arr[i]); + printf("\n"); + + string_sort(arr, n, lexicographic_sort_reverse); + for(int i = 0; i < n; i++) + printf("%s\n", arr[i]); + printf("\n"); + + string_sort(arr, n, sort_by_length); + for(int i = 0; i < n; i++) + printf("%s\n", arr[i]); + printf("\n"); + + string_sort(arr, n, sort_by_number_of_distinct_characters); + for(int i = 0; i < n; i++) + printf("%s\n", arr[i]); + printf("\n"); +} \ No newline at end of file diff --git a/Hackerrank/language/python/Designer_Door_Mat.py b/Hackerrank/language/python/Designer_Door_Mat.py new file mode 100644 index 0000000..2bae708 --- /dev/null +++ b/Hackerrank/language/python/Designer_Door_Mat.py @@ -0,0 +1,11 @@ +""" +Mr. Vincent works in a door mat manufacturing company. One day, he designed a new door mat with the following specifications: + +Mat size must be N X M. (N is an odd natural number, and M is 3 times N) +The design should have 'WELCOME' written in the center. +The design pattern should only use |, . and - characters. +""" + +n, m = map(int,input().split()) +pattern = [('.|.'*(2*i + 1)).center(m, '-') for i in range(n//2)] +print('\n'.join(pattern + ['WELCOME'.center(m, '-')] + pattern[::-1])) diff --git a/Hackerrank/problem solving/Picking_Numbers.cpp b/Hackerrank/problem solving/Picking_Numbers.cpp index eb99284..0c2479d 100644 --- a/Hackerrank/problem solving/Picking_Numbers.cpp +++ b/Hackerrank/problem solving/Picking_Numbers.cpp @@ -1,54 +1,21 @@ #include using namespace std; -#define ff first -#define ss second -#define int long long -#define pb push_back -#define mp make_pair -#define pii pair -#define vi vector -#define mii map -#define pqb priority_queue -#define pqs priority_queue > -#define setbits(x) __builtin_popcountll(x) -#define zrobits(x) __builtin_ctzll(x) -#define mod 1000000007 -#define inf 1e18 -#define ps(x,y) fixed<>x; while(x--) -#define pw(b,p) pow(b,p) + 0.1 - -void file(){ - - ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); - #ifndef ONLINE_JUDGE - freopen("input.txt" , "r" , stdin); - freopen("output.txt" , "w" , stdout); - #endif -} - int32_t main(){ - int n; - cin >>n; - mk(a,n,int); + int n; cin >>n; + int a[n]; for(int i=0 ; i> a[i]; - sort(a,a+n); int ans=0; - - for(int i=0 ; i> s; + if(s[2] == s[3] && s[4]==s[5]) + cout << "Yes" << endl; + else + cout << "No" << endl; + return 0; + +} diff --git a/Templates/cpp/template1.cpp b/Templates/cpp/template1.cpp index 5a820fd..d4b61f3 100644 --- a/Templates/cpp/template1.cpp +++ b/Templates/cpp/template1.cpp @@ -67,4 +67,26 @@ int main() fprintf(stdout,"\nTIME: %.3lf sec\n", (double)clock()/(CLOCKS_PER_SEC)); #endif return 0; -} \ No newline at end of file +} +int32_t main(){ + _; + string s; + cin >> s; + if(s[2] == s[3] && s[4]==s[5]) + cout << "Yes" << endl; + else + cout << "No" << endl; + return 0; + +} +int32_t main(){ + _; + string s; + cin >> s; + if(s[2] == s[3] && s[4]==s[5]) + cout << "Yes" << endl; + else + cout << "No" << endl; + return 0; + +} diff --git a/UVa/cp3/nnumber_theory/primes/summation_of_4_primes.cpp b/UVa/cp3/nnumber_theory/primes/summation_of_4_primes.cpp index 32ce9a8..614c267 100644 --- a/UVa/cp3/nnumber_theory/primes/summation_of_4_primes.cpp +++ b/UVa/cp3/nnumber_theory/primes/summation_of_4_primes.cpp @@ -1,5 +1,6 @@ #include using namespace std; + #define ff first #define ss second #define int long long @@ -36,10 +37,10 @@ void file(){ #ifndef ONLINE_JUDGE freopen("in.txt" , "r" , stdin); freopen("out.txt" , "w" , stdout); - #endif +#endif } -int bs(int a[] , int s , int e, int x){ +int binary_search(int a[] , int s , int e, int x){ if(s<=e){ int m = (s+e)/2; if(a[m]==x) diff --git a/codeforces/1373D_Maximum sum on even positions.cpp b/codeforces/1373D_Maximum sum on even positions.cpp new file mode 100644 index 0000000..b0d311e --- /dev/null +++ b/codeforces/1373D_Maximum sum on even positions.cpp @@ -0,0 +1,24 @@ +#include +using namespace std; +int main() +{long long int t,i; + cin>>t; + while(t--) + {long long n,s=0,ans=0; + cin>>n; + long long arr[n+1],dp[n+1]; + for(i=1;i<=n;i++) + {cin>>arr[i]; + if(i%2) + s+=arr[i];}dp[0]=0;dp[1]=0; + for(i=2;i<=n;i++) + {if(i%2) + dp[i]=max(0ll,arr[i-1]-arr[i]+dp[i-2]); + else + dp[i]=max(0ll,arr[i]-arr[i-1]+dp[i-2]); + ans=max(ans,dp[i]);} + cout< using namespace std; -void fastscan(int &x){ -bool neg = false;register int c; -x = 0;c = getchar(); -if(c=='-'){neg = true;c = getchar();} -for(; (c>47 && c<58); c=getchar()){x = x *10 + c - 48;} -if(neg){x *= -1;}} - #define ff first #define ss second #define int long long diff --git a/spoj/dquery.cpp b/spoj/dquery.cpp new file mode 100644 index 0000000..fda1fbc --- /dev/null +++ b/spoj/dquery.cpp @@ -0,0 +1,172 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace std; + +#define FOR(i,n,m) for(int i=n; im; i--) +#define pb push_back +#define ll long long + + +/*-------------------------------------------------------------------------------------------------------------------------------------- +--------------------------------------------------------------------------------------------------------------------------------------*/ +int a[1000000]; +int n; +struct nodost +{ + int sum; +}; +nodost nodo[1000000]; + +struct query +{ + int l,r,id; + + bool operator <(query b){ + return r med) + return getsum(2*indexst+1, med+1, right, l, r); + if (r <= med) + return getsum(2*indexst, left, med, l, r); + + nodost lresp = getsum(2*indexst, left, med, l, med); + nodost rresp = getsum(2*indexst+1, med+1, right, med+1, r); + nodost result= merge(lresp, rresp); + + return result; +} + +void fix(int r,int u){ + // + ROF(i,u,r){ + int x=b[i].next; + if(x>0){ + a[x]=1; + update(x,1,n,1); + } + } +} + +int main(){ + + ios_base::sync_with_stdio(false); + cin.tie(NULL); + + cin>>n; int c; + + FOR(i,1,n+1){ + + cin>>c; + + b[i]={c,ant[c]}; + ant[c]=i; + + } + + memset(ant,-1, sizeof ant); + //arreglar array a + ROF(i,n,0){ + if(ant[b[i].val]==-1){ + a[i]=1; + ant[b[i].val]=1; + }else a[i]=0; + } + + //hacer segment tree + buildst(1,n,1); + int m; + cin>>m; + int l,r,ract=n; + int re[m]; + + FOR(i,0,m){ + cin>>l>>r; + querys[i]={l,r,i}; + } + + sort(querys,querys+m); + + ROF(i,m-1,-1){ + int r=querys[i].r, l=querys[i].l, id=querys[i].id; + if(r + #include + #include + #include + #include + #include + #include + #include + + using namespace std; + + #define FOR(i,n,m) for(int i=n; im; i--) + #define pb push_back + #define ll long long + #define ri(a) scanf("%d",&a) + #define rii(a,b) scanf("%d %d",&a,&b) + + + /*-------------------------------------------------------------------------------------------------------------------------------------- + --------------------------------------------------------------------------------------------------------------------------------------*/ + ll n,ans,i; + ll a[200005]; + + int main() + { + //ios_base::sync_with_stdio(false); + //cin.tie(NULL); + + cin>>n; + + while(n!=0){ + + FOR(j,0,n) cin>>a[j]; + + ans=0,i=0; stack s; + + while(i>n; + } + + } diff --git a/template.cpp b/template.cpp index b6ced88..a56d701 100644 --- a/template.cpp +++ b/template.cpp @@ -35,6 +35,6 @@ void trace(const char* names, T&& arg1, Args&&... args){ const char* comma = strchr(names + 1, ',');cout.write(names, comma-names)<<" : "<