Count of Maximum code chef.Runtime Error(SIGSEGV) - c++

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.

Related

codeforces problem named Strange List the "MEMORY LIMIT EXCEEDED"

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
}
}

How to avoid TLE when array is used to store 1 *10^5 integer entries

My code is showing TLE when there when number of entries in array(n) is 1 *10^5. What should i do? I saw the submission status and in all cases it ran fine except in the last case when n is 100 000, it shows time limit error.
Problem:: https://codeforces.com/contest/474/problem/B
My solution: https://pastebin.com/5RLBirpF
Code:
#include <iostream>
#include<bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int t;
cin>>t;
int arr[t];
int arrfreq[t];
int sum=1;
for(int i=0;i<t;i++ )
{
cin>>arr[t];
sum+=arr[t];
arrfreq[i]=sum;
}
int m;
cin>>m;
int qsn[m];
int k;
for(int i =0;i<m;i++)
{
cin>>qsn[i];
}
for(int j =0;j<t;j++)
{
if(k<arrfreq[j])
{
cout<<j+1<<"\n";
break;
}
if(k==arrfreq[j])
{
cout<<j+2<<"\n";
break;
}
}
}
You are using one loop for calculating the number of pile. What you can do instead is find the answer in the same loop in which you are taking the input for qsn. Just take the input of qsn and find the pile number in that loop itself. That will reduce your code's time complexity and remove the TLE error.
I saw other solutions on the internet and found out that std::lower_bound function is what has to be used to avoid T.L.E.
But one thing, as mentioned on internet, this function returns a pointer to the lower bound, then , why are we subtracting c from it and then +1 also(in the 2nd last cout line)??(refer to the code.)
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n,i,m,j,cnt=0,ans,x,sum=0;
cin>>n;
int a[n],c[n];
for(i=0; i<n; i++){
cin>>a[i];
sum+=a[i];
c[i]=sum;
}
cin>>m;
int b[m];
for(i=0; i<m; i++) cin>>b[i];
for(j=0; j<m; j++)
{
cout<<lower_bound(c,c+n,b[j])-c+1<<endl;
}
return 0;
}

I got a RunTime error SIGCONT for the below code

#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

Why is my Sliding Window algorithm not providing correct answers?

The problem is from a recent competition on Codechef.
According to the problem, for a given array of length N, find the maximum average of the numbers of any contiguous sub-array of length between A and B.
Now here is the logic that I have used. I have in a loop iterated over the possible contiguous sub-array length K from A to B. Then for every such K, I have used the sliding window mechanism to find the maximum sum of contiguous
elements of the array. Then I check if the already stored answer (the average) is smaller than the current maximum/K and update the answer.
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int t;
cin>>t;
while(t--)
{
int n,b,a;
cin>>n>>b>>a;
long long arr[n+1];
for(int i=1;i<=n;i++)
{
cin>>arr[i];
}
double ans=0;
for(int k=a;k<=b;k++)
{
long long maxi=0;
for(int i=1;i<=k;i++)
{
maxi=maxi+arr[i];
}
long long l=maxi;
for(int i=k+1;i<=n;i++)
{
l=l+arr[i]-arr[i-k];
maxi=max(maxi,l);
}
double x=(double)(((double)(maxi))/(double)(k));
ans=max(ans,x);
}
cout<<ans<<endl;
}
}
However, after implementing my logic over as shown, I am getting wrong answers even though I do not know how different they are the actual answer since I have not obtained anything wrong in my processes of debugging.
The following is an edit:
Are my previous code and the following the same?
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int t;
cin>>t;
while(t--)
{
int n,b,a;
cin>>n>>b>>a;
long long arr[n+1];
for(int i=1;i<=n;i++)
{
cin>>arr[i];
}
long double ans=0;
for(int k=a;k<=b;k++)
{
long long l=0;
for(int i=1;i<=k;i++)
{
l=l+arr[i];
}
ans=max(ans,(long double)(((long double)(l))/(long double)(k)));
for(int i=k+1;i<=n;i++)
{
l=l+arr[i]-arr[i-k];
ans=max(ans,(long double)(((long double)(l))/(long double)(k)));
}
}
cout<<ans<<endl;
}
}
Thanks for helping guys. I tried out the following and it sort of works:
cout<<fixed<<setprecision(10);
setprecision(10)

I am getting thread1:SIGNAL sigbart in output

this is my code, PLease help me ! im using xcode.. i want to generate a sequence for a polynomial and the terms are xor'ed and made a feedback to the first input bit since it is 8 bit it is done 2^8-1 times.Alternate code will also be helpful Thanks in advance
#include "32bit.h"
#include<iostream>
using namespace std;
int main()
{
bool input[8];
int n;
bool out=0;
cout<<"Enter the no of terms ";
cin>>n;
int temp1[n];
int gen=0;
bool store[255];
cout<<"Input power of x in increasing order, Omit x^0";
for(int i=0;i<n;i++)
cin>>temp1[i];
cout<<"Enter key to generate ";
cin>>gen;
for(int m=0;m<255;m++)
{
store[m]=input[gen];
bool temp2[n];
int var=0;
for(int j=0;j<n;j++)
{
var=temp1[j];
temp2[j]=input[var];
}
int c=0;
for(int k=0;k<n;k++)
{
if(temp2[k]%2==1)
c++;
}
if(c%2==1)
out=1;
else
out=0;
for(int l=0;l<8;l++)
input[l+1]=input[l];
input[0]=out;
}
for(int p=0;p<255;p++)
cout<<store[p];
}
There is an out of bounds array access here:
for(int l=0;l<8;l++)
input[l+1]=input[l];
since input is only of size 8 and you are trying to write to input[8] (i.e. the non-existent 9th element) on the last iteration of this loop. I'm guessing it should probably be:
for(int l=0;l<7;l++)
input[l+1]=input[l];