Floating point Exception in c++ because of divide statement - c++

I have searched google but I was unable to find the solution to my problem.
Here is my code-
#include <bits/stdc++.h>
#include<algorithm>
#include<cstdlib>
#include<cstdio>
using namespace std;
long long buyMaximumProducts(int n, long k, vector <int> a) {
// Complete this function
vector<pair<int, int>> p;
long i;
for(i=0; i<n; i++) {
p.push_back(make_pair(a[i], i+1));
}
sort(p.begin(), p.end());
if(k < p[0].first)
return 0;
long long sum=0,stocks=0;
for(i=0;i<n;i++)
{
if((sum+p[i].first*p[i].second) <= k)
{
sum+=p[i].first*p[i].second;
stocks+=p[i].second;
}
else
break;
}
long long amtleft=k-sum;
**stocks+=(long long)(amtleft/p[i].first);**
return stocks;
}
int main() {
int n;
cin >> n;
vector<int> arr(n);
for(int arr_i = 0; arr_i < n; arr_i++){
cin >> arr[arr_i];
}
long long k;
cin >> k;
long long result = buyMaximumProducts(n, k, arr);
cout << result << endl;
return 0;
}
I'm getting floating point exception. I think the error is coming because of the star statement. Can anyone please tell me what could be the plausible reason and how to remove it?

The program contains at least 3 fault.
long long k;
cin >> k;
long long result = buyMaximumProducts(n, k, arr);
long long buyMaximumProducts(int n, long k, vector <int> a) {
k is 'long long' but parameter k is only 'long'.
for(i=0;i<n;i++) {
if((sum+p[i].first*p[i].second) <= k) {
sum+=p[i].first*p[i].second;
stocks+=p[i].second;
} else
break;
}
if we never get to the 'break' then 'i' is not valid for
stocks+=(long long)(amtleft/p[i].first);
causing an exception.
and if
p[i].first
is zero you get an divide by zero exception.

Related

C++ program test case acceptance

I've attempted to solve a coding contest out of curiosity. Out there, there's one of the problems which couldn't pass all the test cases for my solution. Could there be any improvement in my solution that you would suggest that might help? TIA. The problem along with my solution is stated down below.
The Constraints:
1 ≤ M,N ≤ 10^7,
1 ≤ a[i],b[i] ≤ 10^7
The Output:
Print a single int that the maximum cool value that he
can obtain after buying two items with the given
amount of money
And my solution is:
#include<bits/stdc++.h>
using namespace std;
int main()
{
long long N, M, C, count;
cin>>N>>M>>C;
long long a[N], b[M];
for(long long i=0; i<N; i++){
cin>>a[i];
}
for(long long i=0; i<M; i++){
cin>>b[i];
}
sort(a, a+N);
sort(b, b+M);
if(a[N-1] + b[M-1] <= C){
count = (N-1) + (M-1);
cout<<count<<endl;
}
else{
cout<<"Not even closer"<<endl;
}
return 0;
}
Can anyone suggest what I could improve here to pass all the test?
After sorting, the price a[i] has a coolness equal to i.
Therefore, a solution consists in sorting a and b, and then maximize i+j such that a[i]+a[j] <= C
This last optimisation is performed by a simple for loop, with two indices, one for a and one for b
Complexity is dominated by sorting: O(NlogN + MlogM)
Note: the code currently posted in the question was edited after some comments, and do not correspond to the original code
#include <iostream>
#include <vector>
#include <algorithm>
int main() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(NULL);
int N, M, C;
std::cin >> N >> M >> C;
std::vector<int> a(N), b(M);
for (int i = 0; i < N; i++){
std::cin >> a[i];
}
for (int i = 0; i < M; i++){
std::cin >> b[i];
}
sort(a.begin(), a.end());
sort(b.begin(), b.end());
if(a[0] + b[0] > C) {
std::cout <<"E kemon aynabaji!!!" << std::endl;
return 0;
}
int max_cool = 0;
int iB = M-1;
for (int iA = 0; iA < N; ++iA) {
while (iB >= 0 && (a[iA] + b[iB] > C)) {
iB--;
}
if (iB < 0) break;
int cool = iA + iB;
if (cool > max_cool) max_cool = cool;
}
std::cout << max_cool << std::endl;
return 0;
}

Outputing 64bit integers in c++

I wrote the following code-
#include <iostream>
#include <tuple>
int sumarr(int64_t arr[], int length)
{
long long int sum= 0;
for(int i=0; i<length; i++)
{
sum += arr[i];
}
return sum;
}
void minimaxval(int64_t arr[], int length,int64_t *x,int64_t *y)
{
long long int c=0;
long long int d=10e9;
for(int j=0; j<5; j++)
{
if(arr[j] >= c)
{
c = arr[j];
}
if (arr[j] <= d)
{
d= arr[j];
}
}
*x=c;
*y=d;
}
int main()
{
int64_t arr[5];
int64_t p, q;
int64_t sum;
for(int k=0; k<5; k++){
std::cin>> arr[k];
if (arr[k]<1 || arr[k]>10e9){return 0;}
}
sum= sumarr(arr, 5);
minimaxval (arr, 5, &p, &q);
std::cout << sum-p << " " << (sum-q);
return 0;
}
This is my first question on stackoverflow. So I dont know a lot about format.
Input for the que. will be 5 space-separated integers.
Output should be the minimum sum and the maximum sum we can get by removing only one integer from the list.
It does not show any compiler issues.
It works fine for smaller integers but outputs a negative value for larger integers.
Where did I go wrong.
In 'summar' you have wrong return type. If you change it to int64_t everything will be fine.
P. S. And yes this is also my first answer)

Princess Farida on Spoj

I stuck at a problem SPOJ.
I checked all the test cases passing all of them, but I am still getting "WA" on spoj.
I know it can be solved using dynamic programming, but I am practicing memoization.
Here is my code:
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
vector <int> dp(1000000);
long long int maxloot(vector<int> &loot, int n) {
if (n == 0)
return 0;
if (n == 1)
return loot[0];
if (n == 2)
return max(loot[0], loot[1]);
if (dp[n] != -1)
return dp[n];
long long int take = loot[n - 1] + maxloot(loot, n - 2);
long long int leave = maxloot(loot, n - 1);
return dp[n]= max(take, leave);
}
int main() {
int t;
cin >> t;
int p = 1;
while (t--) {
int n;
cin >> n;
vector <int> loot;
for (int i = 0; i < n; i++) {
int temp;
cin >> temp;
loot.push_back(temp);
}
dp.assign(1000000, -1);
cout <<"Case "<<p<<": "<< maxloot(loot, n)<<endl;
p++;
dp.clear();
}
}
Test case 1:
5
1 2 3 4 5
Test case 2:
1
10
output 1:
9
output 2:
10
You are using wrong data type to store value in vector dp.
As the sum of coins can go up to (10^9*10^2=10^11) therefore int would not be able to store it .Try using long long int instead as it would not lead to overflow condition.
SAME CODE AS YOURS(using long long int got accepted):
USE: vector< long long int>dp(1000000)
ACCEPTED CODE:
#include<iostream>
#include<vector>
#include<algorithm>
#define ull unsigned long long
using namespace std;
vector <long long int> dp(1000000);
long long int maxloot(vector<int> &loot, int n) {
if (n == 0)
return 0;
if (n == 1)
return loot[0];
if (n == 2)
return max(loot[0], loot[1]);
if (dp[n] != -1)
return dp[n];
long long int take = loot[n - 1] + maxloot(loot, n - 2);
long long int leave = maxloot(loot, n - 1);
return dp[n]= max(take, leave);
}
int main() {
int t;
cin >> t;
int p = 1;
while (t--) {
int n;
cin >> n;
vector <int> loot;
for (int i = 0; i < n; i++) {
int temp;
cin >> temp;
loot.push_back(temp);
}
dp.assign(1000000, -1);
cout <<"Case "<<p<<": "<< maxloot(loot, n)<<endl;
p++;
dp.clear();
}
}

CPP Prime Generator SPOJ Sieve

I did implement Sieve prime generator. The code is pretty fast and consumes less memory. https://www.spoj.com/problems/PRIME1/
But I get "Wrong Answer" when I submit the solution. People online seem to just make the set the max to 32000 and run the solution. I don't exactly get where am I actually wrong? Or Is it just an extra newline (if possible) that makes the solution to be incorrect?
#include <iostream>
#include <unordered_map>
using namespace std;
int main(void) {
unordered_map<long long int, bool> notPrime;
notPrime[0] = true;
notPrime[1] = true;
for (long long int x = 2; x < 100000; x++) {
if (notPrime[x]) continue;
for (long long int u = 2 * x; u < 100000; u += x)
notPrime[u] = true;
}
int n;
cin >> n;
while (n--) {
long long int s, e;
cin >> s >> e;
if (s < 0)
s = 0;
for (long long int i = s; i <= e; i++) {
if (!notPrime[i]) {
cout << i << '\n';
}
}
if (n)
puts("");
}
return 0;
}

Saving unknown amount of integers without taking too much time/memory

#include <iostream>
using namespace std;
unsigned long long divsum(unsigned long long x);
int main()
{
unsigned long long x;
cin >> x;
unsigned long long y[200000];
for (unsigned int i = 0; i < x; i++)
cin >> y[i];
for (unsigned int i = 0; i < x; i++){
cout << divsum(y[i]) << endl;
}
return 0;
}
unsigned long long divsum(unsigned long long x){
int sum = 0;
for(unsigned int i = 1; i <= x/2; i++){
if(x % i == 0)
sum += i;
}
return sum;
}
I'm doing an online exercise and it says there are possible 2000000 cases in the first line, so I made an array of that amount, however, when I submit the solution it exceeds the time.. so I was wondering what is an alternative and faster way to do this? The program works fine right now, except it exceeds the time limit of the website.
You can allocate the array dynamically, so it will work better in cases where x < 200000
int main()
{
unsigned long long x;
cin >> x;
unsigned long long *y = new unsigned long long[x];
// you can check if new didn't throw an exception here
for (unsigned int i = 0; i < x; i++)
cin >> y[i];
for (unsigned int i = 0; i < x; i++){
cout << divsum(y[i]) << endl;
}
delete[] y;
return 0;
}
Since you know the size of the array, try vector and reserve.
int main()
{
unsigned long long x;
cin >> x;
unsigned long long var;
vector<unsigned long long> y;
y.reserve(x);
for (unsigned int i = 0; i < x; i++){
cin >> y[i];
}for (unsigned int i = 0; i < x; i++){
cout << divsum(var) << endl;
}
return 0;
}
And deal in const &
const unsigned long long & divsum(const unsigned long long & x){
int sum = 0;
unsigned long long x2 = x/2
for(unsigned int i = 1; i <= x2; i++){
if(x % i == 0)
sum += i;
}
return sum;
}
I think your assignment is more complex than you think. Your divsum(x) function should return sum of all divisors of x, right? In this case it's better to factorize the x, and calculate this sum using all the prime numbers (with powers), product of which is equal to the x. Take a look at:
http://en.wikipedia.org/wiki/Divisor_function
There are methods for recursive factorization - for example, if you have factorized all numbers until n, you can quickly find factorization for (n + 1). You also need to generate primes, Erathosphene sieve for first 2000000 numbers would be fine.