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

sharshach #18

Open
wants to merge 23 commits into
base: master
Choose a base branch
from
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
7 changes: 7 additions & 0 deletions answering.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
answer code goes as follows

EASY PROBLEM 7

ANSWER CODE : E07.cpp OR E07.py etc

similarly M for medium and H for hard
17 changes: 17 additions & 0 deletions answers/E01.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@

#include<iostream>
//dont forget the case of a=0;
using namespace std;
long long int sol(long long int a){
if (a==0)return 0;
if(a%2==1)return 1;
else return 2*sol(a/2);
}
int main(){
long long int a;
while(!feof(stdin)){
cin>>a;
cout<<sol(a)<<endl;
}
return 0;
}
25 changes: 25 additions & 0 deletions answers/E02.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#include<iostream>
#include<cmath>
using namespace std;
int i;
#define geti(x) int x;cin>>x;
#define getl(x) long long int x;in>>x;
#define FOR(i,a,b) for(i=a;i<b;i++)
int run(){
geti(n)
int a[24][60]={0};
int h,m,maximum=0;
FOR(i,0,n-1){
cin>>h>>m;
a[h][m]++;
maximum=max(maximum,a[h][m]);
}
cout<<maximum<<endl;
return 0;
}
int main(){
geti(t)
while(t--)
run();
return 0;
}
87 changes: 87 additions & 0 deletions answers/E03_GSS1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
class tree{
public:
ll sum;
ll maxSum;
ll maxPrefixSum;
ll maxSuffixSum;
}segTree[200005];
int a[50004],n;
tree zero;
tree join(tree left,tree right){
tree sol;
sol.sum=left.sum+right.sum;
sol.maxSum=max(left.maxSum,right.maxSum);
if(left.maxSum>=0&&right.maxSum>=0)
sol.maxSum=max(sol.maxSum,left.maxSuffixSum+right.maxPrefixSum);
sol.maxPrefixSum=max(left.maxPrefixSum,left.sum+right.maxPrefixSum);
sol.maxSuffixSum=max(right.maxSuffixSum,right.sum+left.maxSuffixSum);
return sol;
}
void buildSegTree(int i,int l,int r){
if(l>r)return ;
if(l==r){
segTree[i].sum=a[l];
segTree[i].maxSum=a[l];
segTree[i].maxPrefixSum=a[l];
segTree[i].maxSuffixSum=a[l];
}
else{
int mid=(l+r)/2;
buildSegTree(2*i,l,mid);
buildSegTree(2*i+1,mid+1,r);
segTree[i]=join(segTree[2*i],segTree[2*i+1]);
}
}
tree query_old(int treeIndex,int treeLeft,int treeRight,int l,int r){
if(treeLeft>treeRight)return zero;
if(treeLeft==treeRight)return segTree[treeIndex];
int treeMid=(treeLeft+treeRight)/2;
if(treeMid<l)return query_old(2*treeIndex+1,treeMid+1,treeRight,l,r);
if(treeMid>=r)return query_old(2*treeIndex,treeLeft,treeMid,l,r);
return join(query_old(2*treeIndex,treeLeft,treeMid,l,r),query_old(2*treeIndex+1,treeMid+1,treeRight,l,r));
}

tree query_new(int node,int start,int end,int l,int r)
{
if(r<start || l>end)
{
tree zero;
zero.sum=-20000;
zero.maxSum=-20000;
zero.maxPrefixSum=-20000;
zero.maxSuffixSum=-20000;
return zero;
}
if(start>=l && end<=r)
{
return segTree[node];
}
int mid=(start+end)/2;
tree p1=query_new(2*node,start,mid,l,r);
tree p2=query_new(2*node+1,mid+1,end,l,r);
tree temp=join(p1,p2);
return temp;
}
int main(){

ios_base::sync_with_stdio(false);
cin.tie(NULL);
zero.maxPrefixSum=zero.maxSuffixSum=zero.sum=-20000;
zero.maxSum=-20000;
cin>>n;
for(int i=0;i<n;i++){
cin>>a[i];
}
buildSegTree(1,0,n-1);
int q;
cin>>q;
int l,r;
while(q--){
cin>>l>>r;
cout<<query_new(1,0,n-1,l-1,r-1).maxSum<<endl;
}
return 0;
}
21 changes: 21 additions & 0 deletions answers/E04.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include<iostream>
#include<cmath>
using namespace std;
#define geti(x) int x;cin>>x;
#define getl(x) long long int x;in>>x;
#define FOR(i,a,b) for(i=a;i<b;i++)
long long int N;
int run(){
cin>>N;
double sol2=(sqrt(1+8*N)-1)/2;
long long int sol=sol2;
if(sol+0.0!=sol2)sol+=1;
cout<<sol<<endl;
return 0;
}
int main(){
geti(t)
while(t--)
run();
return 0;
}
1 change: 1 addition & 0 deletions answers/E05.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
problem unvailable
1 change: 1 addition & 0 deletions answers/E06.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
refer E04.cpp
101 changes: 101 additions & 0 deletions answers/E07.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
#include <bits/stdc++.h>

using namespace std;

vector<string> split_string(string);

// Complete the twoArrays function below.
string twoArrays(int k, vector<int> A, vector<int> B) {
sort(A.begin(),A.end());
sort(B.begin(),B.end(),greater<int>());
int l=A.size();
while(l--){
if(A[l]+B[l]<k)return "NO";
}


return "YES";
}

int main()
{
ofstream fout(getenv("OUTPUT_PATH"));

int q;
cin >> q;
cin.ignore(numeric_limits<streamsize>::max(), '\n');

for (int q_itr = 0; q_itr < q; q_itr++) {
string nk_temp;
getline(cin, nk_temp);

vector<string> nk = split_string(nk_temp);

int n = stoi(nk[0]);

int k = stoi(nk[1]);

string A_temp_temp;
getline(cin, A_temp_temp);

vector<string> A_temp = split_string(A_temp_temp);

vector<int> A(n);

for (int i = 0; i < n; i++) {
int A_item = stoi(A_temp[i]);

A[i] = A_item;
}

string B_temp_temp;
getline(cin, B_temp_temp);

vector<string> B_temp = split_string(B_temp_temp);

vector<int> B(n);

for (int i = 0; i < n; i++) {
int B_item = stoi(B_temp[i]);

B[i] = B_item;
}

string result = twoArrays(k, A, B);

fout << result << "\n";
}

fout.close();

return 0;
}

vector<string> split_string(string input_string) {
string::iterator new_end = unique(input_string.begin(), input_string.end(), [] (const char &x, const char &y) {
return x == y and x == ' ';
});

input_string.erase(new_end, input_string.end());

while (input_string[input_string.length() - 1] == ' ') {
input_string.pop_back();
}

vector<string> splits;
char delimiter = ' ';

size_t i = 0;
size_t pos = input_string.find(delimiter);

while (pos != string::npos) {
splits.push_back(input_string.substr(i, pos - i));

i = pos + 1;
pos = input_string.find(delimiter, i);
}

splits.push_back(input_string.substr(i, min(pos, input_string.length()) - i + 1));

return splits;
}
93 changes: 93 additions & 0 deletions answers/E08_FREQUENT.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
#include<bits/stdc++.h>
using namespace std;
typedef int ll;
class tree{
public:
ll maxFrequency;
ll leftFrequency;
ll rightFrequency;
ll rightElement;
ll leftElement;
};
tree *segTree;
int a[100004],n;
tree zero;
tree join(tree left,tree right){
tree sol;
sol.maxFrequency=max(left.maxFrequency,right.maxFrequency);
if(left.rightElement==right.leftElement)
sol.maxFrequency=max(sol.maxFrequency,left.rightFrequency+right.leftFrequency);
sol.leftFrequency=left.leftElement==right.leftElement?left.leftFrequency+right.leftFrequency:left.leftFrequency;
sol.rightFrequency=left.rightElement==right.rightElement?left.rightFrequency+right.rightFrequency:right.rightFrequency;
sol.leftElement=left.leftElement;
sol.rightElement=right.rightElement;
return sol;
}
void buildSegTree(int i,int l,int r){
if(l>r)return ;
if(l==r){
segTree[i].rightElement=a[l];
segTree[i].leftElement=a[l];
segTree[i].maxFrequency=1;
segTree[i].leftFrequency=segTree[i].rightFrequency=1;
}
else{
int mid=(l+r)/2;
buildSegTree(2*i,l,mid);
buildSegTree(2*i+1,mid+1,r);
segTree[i]=join(segTree[2*i],segTree[2*i+1]);
}
}
tree query_old(int treeIndex,int treeLeft,int treeRight,int l,int r){
if(treeLeft>treeRight)return zero;
if(treeLeft==treeRight)return segTree[treeIndex];
int treeMid=(treeLeft+treeRight)/2;
if(treeMid<l)return query_old(2*treeIndex+1,treeMid+1,treeRight,l,r);
if(treeMid>=r)return query_old(2*treeIndex,treeLeft,treeMid,l,r);
return join(query_old(2*treeIndex,treeLeft,treeMid,l,r),query_old(2*treeIndex+1,treeMid+1,treeRight,l,r));
}

tree query_new(int node,int start,int end,int l,int r)
{
if(r<start || l>end)
{
tree zero;
zero.rightElement=0;
zero.leftElement=0;
zero.maxFrequency=0;
zero.leftFrequency=zero.rightFrequency=0;
return zero;
}
if(start>=l && end<=r)
{
return segTree[node];
}
int mid=(start+end)/2;
tree p1=query_new(2*node,start,mid,l,r);
tree p2=query_new(2*node+1,mid+1,end,l,r);
tree temp=join(p1,p2);
return temp;
}
int main(){

ios_base::sync_with_stdio(false);
cin.tie(NULL);
int q;
cin>>n;
while(n){
tree myTree[4*n];
segTree=myTree;
cin>>q;
for(int i=0;i<n;i++){
cin>>a[i];
}
buildSegTree(1,0,n-1);
int l,r;
while(q--){
cin>>l>>r;
cout<<query_new(1,0,n-1,l-1,r-1).maxFrequency<<endl;
}
cin>>n;
}
return 0;
}
Loading