Codechef rejection (factorial of a number) - c++

Why is codechef rejecting this solution even though it's giving desirable outputs on codechef code runner itself? (Factorial of a number)
#include <iostream>
using namespace std;
int main() {
int t;
cin>>t;
int n;
int fact=1;
for(int i=0; i<t;i++){
cin>>n;
for(int i=1; i<=n; i++){
fact=fact*i;
}cout<<fact;
cout<<endl;
fact=1;
}
return 0;
}

As you are storing the result of factorial in int variable, which has a max limit of 2147483647, so for numbers larger than this, factorial will not be computed correctly.
Try using other data types which can have max higher limits. https://learn.microsoft.com/en-us/cpp/c-language/cpp-integer-limits?view=msvc-160

Related

Why this code is returning wrong value? inputs= 5 ,{ -1,4,-6,7,-4}

#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]

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

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)

Count of Maximum code chef.Runtime Error(SIGSEGV)

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.

Newton's binomial - doesn't work for bigger numbers

I wrote a program which is supposed to print the value of Newton's binomial.
number - number of tests, t[i][0] - n, t[i][1] - k. It seems to be ok for small numbers n and k, but when I want to type bigger numbers it prints 0, 1 or small, negative integer. Basically I used long intead of int so it should work with bigger numbers. Could you explain why is that?
#include <iostream>
long fact(int x);
using namespace std;
int main()
{
int number;
cin>>number;
int t[number][2];
for(int i=0; i<number; i++)
{
cin>>t[i][0];
cin>>t[i][1];
if (t[i][0]<t[i][1]) return 0;
}
for(int i=0; i<number; i++)
{
cout<<fact(t[i][0])/(fact(t[i][0]-t[i][1])*fact(t[i][1]))<<endl;
}
return 0;
}
long fact(int x)
{
long factt=1;
for(int i=1; i<=x; i++)
{
factt=factt*i;
}
return factt;
}
#edit
Thanks for advice. I tried implementing this but it doesn't compute the binomial well. It prints 11 for n=4 and k=2. May you have a look at this?
#include <iostream>
long fact(int n, int k);
using namespace std;
int main()
{
int number;
cin>>number;
int t[number][2];
for(int i=0; i<number; i++)
{
cin>>t[i][0];
cin>>t[i][1];
if (t[i][0]<t[i][1]) return 0;
}
for(int i=0; i<number; i++)
{
cout<<fact(t[i][0],t[i][1])<<endl;
}
return 0;
}
long fact(int n, int k)
{
if(n==0 || n==k)
return 1;
else if(n>k)
return fact(n-1,k-1)+fact(n-1, k);
else
return 0;
}
Factorial grows really fast and even unsigned 64-bit integers overflow n! for n>20. The overflow free way to implement the binomial coefficient is to use this recursive definition:
binom(n, k) = binom(n-1, k-1) + binom(n-1, k)
This ensures that you get an overflow only when binom(n,k) is too large to fit in your integral type's size.
On Linux 32bit long is the same as int and fits into 32bit. On Linux 64bit long is 64bit long.
On Windows both 32bit and 64bit long is 32bits entity
You have to use long long to guaranteed to use 64bit, though it might be not enough to overcome overflow. Use recursive formula for binominal, if possible