#include<iostream>
using namespace std;
int main(){
int t;
cin>>t;
while(t--){
int n,ctr=0;
cin>>n;
int a[n],ptr=750;
for(int i=0;i<n;i++){
cin>>a[i];
if(a[i]<ptr){
ctr++;
ptr=a[i];
}
}
cout<<ctr-(n/5)+1<<"\n";
}
return 0;
}
All other answers say I would have divided by zero or accessed out of bounds but here I do not find any such situation
Related
#include<iostream>
#include<climits>
using namespace std;
int main(){
int n;
cin>>n;
int a[n];
for(int i=0;i<n;++i){
cin>>a[n];
} //array instillisation
int cursum=0;
int maxsum=INT_MIN;
for(int i=0;i<n;++i){
cursum+=a[i];
if(cursum<0){
cursum=0;
}
maxsum=max(cursum,maxsum);
}
cout<<maxsum<<endl;
return 0;
}
//this code is for maximum subarray problem using kadane's algo.My compiler is retrurning wrong output
In cin you are doing incorrect operation it should be
cin>> a[i];
what you are doing is taking the value of a[n]
With reference to the problem named "Strange List" in codeforces following is my code
But the problem is All test case is passing but the last test case it is showing"MEMORY EXCEEDED LIMIT" and the last test case value is also huge like for n it is 100000.
Please guide me the way to tackle it
#include<bits/stdc++.h>
#define ll long long int
using namespace std;
int main()
{
int t;
cin>>t;
while(t--)
{
ll ele,sum=0,i,j,h,n,x;
cin>>n>>x;
vector <ll> vec;
for(i=0;i<n;i++)
{
cin>>ele;
vec.push_back(ele);
}
for(i=0;i>=0;i++)
{
if(vec.at(i)%x==1)
{
break;
}
else
{
h=vec.at(i)/x;
for(j=0;j<x;j++)
{
vec.push_back(h);
}
}
}
for(i=0;i<vec.size();i++)
{
sum+=vec.at(i);
}
cout<<sum<<"\n";
}
}
You can use Dynamic Programming to solve this problem
Implementation of storing results can be done by using Vector of pairs
Below is the AC code demonstrates how these kinds of problems can be done efficiently in terms of memory consumption......Happy Coding :)
CODE
#include<bits/stdc++.h>
using namespace std;
#define ll long long
int main(){
ll t;
cin>>t;
while(t--){
ll n,x;
cin>>n>>x;
vector<pair<ll,ll>> a(n);
for(int i=0;i<n;i++){
cin>>a[i].first;
a[i].second=a[i].first; // Storing initial results in second part of pair
}
ll s=0;
ll d=0;
ll r=0;
ll k=0;
while(++r){
for(int i=0;i<n;i++){
if(a[i].first%x==0){
s+=a[i].second;
a[i].first=a[i].first/x;
a[i].second=(a[i].first*pow(x,r)); // updating results
}
else{
s+=a[i].second;
if(d==0)
k=i;
d=1;
}
}
if(d)
break;
}
for(int i=0;i<k;i++)
s+=a[i].second; // getting desired sum
cout<<s<<"\n"; // printing answer
}
}
So there was this code which got accepted when i took its output in a vector
#include <iostream>
#include<vector>
using namespace std;
int main(){
int t; cin >> t;
while(t--){
vector<int>v;
int n,k; cin >> n >> k;
for(int i=0;i<n;i++){
int x; cin >> x;
if(x%k==0) v.push_back(1);
else v.push_back(0);
}
for(auto x:v) cout <<x <<"";
cout << endl;
}
return 0;
}
but then there is this code gives Time Limit Exceeded error when i am direct printing it
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
int main()
{
int t;
cin>>t;
while(t--)
{
ll k,d;
int n,i;
cin>>n>>k;
for(i=0;i<n;i++)
{
cin>>d;
if(d%k==0)
cout<<"1";
else
cout<<"0";
}
cout<<"\n";
}
}
Can you tell why ?
(The contest is now over)Here is the question in case
Edit:1 I used int instead of long long as well as printf as well as cin.tie(NULL) stuff , but still to no avail
The implementation with the cout in the for loop body is going to bottle-beck on the cout output for sure, especially given a modulo operation is very cheap in contrast.
See below question as reference:
C++: Does cout statement makes code slower
Something like this would work better:
#include <bits/stdc++.h>
#include <vector>
using namespace std;
#define ll long long int
int main()
{
int t;
cin>>t;
while(t--)
{
ll k,d;
int n,i;
cin>>n>>k;
std::vector<bool> r(n);
for(i=0;i<n;i++)
{
cin>>d;
if(d%k==0)
r[i] = true;
}
for(auto i : r)
cout<<(i ? '1' : '0')<<endl;
cout<<"\n";
}
}
I am getting SIGABRT error in my code. Can anyone tell me from where it might be coming?
Here's my code:
#include<bits/stdc++.h>
using namespace std;
int main()
{
int t;
cin>>t;
while(t--)
{
int n;
cin>>n;
string s;
cin>>s;
int d=stoi(s,nullptr,2);
int f=__builtin_popcount(d);
cout<<f*(f-1)/2+f<<endl;
}
}
#include<bits/stdc++.h>
using namespace std;
int main()
{
long long int t;
cin>>t;
while(t--)
{
long long int n;
cin>>n;
string s;
cin>>s;
long long int d=stoi(s,nullptr,2);
long long int f=__builtin_popcount(d);
cout<<f*(f-1)/2+f<<endl;
}`enter code here`
}
Now Try This
Hi I started doing code chef beginner problems and got stuck at this one.I tried reducing the stack memory by declaring the arrays globally but that doesn't work too.Here is my code
#include<iostream>
using namespace std;
#define max 101
int main()
{
int t,n;
cin>>t;
while(t--)
{
int a[max];
int c[max]={0};
cin>>n;
for(int i=0;i<n;i++)
{
cin>>a[i];
}
for(int j=0;j<n;j++)
{
c[a[j]]++;
}
int temp=0;
int x=0;
for(int k=0;k<n;k++)
{
if(c[k]>temp)
{
temp=c[k];
x=k;
}
}
cout<<x<<" "<<temp<<endl;
}
}
You may need a data structure that does not limit the input value to be bounded, or just change your algorithm.
Either use std::map<int, int> in place of c to count occurence of each number, or just sort a to aggregate same values and count.