So I am working on a problem at HackerEarth which requires me to store an integer array of size 10^9.
I am trying to store the array using vector but I get runtime error SIGABRT when I try to Submit my Solution.
Here is my code:
#include<iostream>
#include<vector>
using namespace std;
int main()
{
unsigned int h, c, q;
cin >> h >> c >> q;
vector<unsigned int> minht(h + 1);
for (unsigned int i = 0; i <= h; i++)
minht[i] = 0;
unsigned int hc, st, et;
for (unsigned int i = 1; i <= c; i++)
{
cin >> hc >> st >> et;
for (unsigned int j = 1; j <= h; j++)
{
if (j >= st && j <= et)
{
if (hc > minht[j])
minht[j] = hc;
}
}
}
unsigned int hq, tq;
for (unsigned int i = 1; i <= q; i++)
{
cin >> hq >> tq;
if (hq > minht[tq])
cout << "YES" << endl;
else
cout << "NO" << endl;
}
}
The code works fine when value of h is not very large(10^9) but I get SIGABRT when value of h is very large.
Any Suggestions?
EDIT:
So I tried rewriting my entire code and my new code looks like this:
#include<iostream>
#include<vector>
using namespace std;
int main()
{
unsigned int h,c,q,mh=0;
cin>>h>>c>>q;
vector<unsigned int> hc(c+1);
vector<unsigned int> st(c+1);
vector<unsigned int> et(c+1);
for(unsigned int i=1;i<=c;i++)
{
cin>>hc[i]>>st[i]>>et[i];
if(mh<hc[i])mh=hc[i];
}
unsigned int ht,ti;
for(unsigned int i=1;i<=q;i++)
{
int flag=0;int ct=0;
cin>>ht>>ti;
if(ht<=mh)
{
for(unsigned int j=1;j<=c;j++)
{
if(ti>=st[j] && ti<=et[j])
{
if(ht<=hc[j])
{
ct=1;break;
}
}
}
}
if(ct==0)cout<<"YES\n";
else cout<<"NO\n";
}
}
and the solution was now accepted.
What I did was redesign the algorithm so that instead of having to store 1 VERY LARGE array of 4 GB, I had to store 3 LARGE arrays, each of maximum 40 MB.
So the problem is now solved but I but I still don't think that problem was related to memory issues.
Because when I tried to create a int vector of size 10E8, just to test out the online compiler, I got a specific Memory Limit Exceeded Error. But when I changed the size to 10E9 I got SIGABRT again. So although the memory issue is there, SIGABRT is being caused by declaring an array of 10E9 size.
Any idea what could be causing this??
Also, I first created the program on my own compiler and then submitted it online. I can't debug the program on my setup since the program requires me to enter 10E7 data values before I encounter the error, so I thought that it would be easier to do it on HackerEarth.
Related
I am trying to use pointers whenever possible in the following code and am having difficulty figuring out how, exactly, to institute the pointers and how to return a pointer value at the end of my first function. I have done some research on the subject but none of the methods I found have been helpful so far, so I was hoping you may have some specialized tips.
Note: I am a beginner.
#include <iostream>
using namespace std;
int mode(int *pies[], int size) {
int count = 1;
int max = 0;
int *mode=pies[0];
for (int i=0; i<size-1; i++)
{
if (pies[i] == pies[i+1])
{
count++;
if (count>max)
{
max = count;
mode = pies[i];
}
}
else
count = 1;
}
return *mode;
}
int main() {
int n;
cout<<"Input the number of people: "<<endl;
cin>>n;
int survey[n];
cout << "Enter the amount of pie eaten by each person:" << endl;
for(int i = 0; i < n; i++) {
cout <<"Person "<<(i + 1)<< ": "<<endl;
cin>>survey[i];
}
cout<<"Mode: "<<mode(survey, n)<< endl;
return 0;
}
Here is an attempt to answer.
In your main(), you call the mode() function with mode(survey, n) while int survey[n]; is an array of int, so you may use int mode(int *pies, int size) instead of int mode(int *pies[], int size) (as the array int survey[n] can be implicitly converted into pointer).
However, you need to modify two more things in your function:
int *mode=pies[0]; is wrong as pies[0] is the first element of an array of int, thus is an int, while int* mode is a pointer on an int which is incompatible. mode should be an int to receive pies[0]. The correct code is then int mode = pies[0].
Your function signature is int mode(int *pies, int size), thus, again, you should return an int. You should then just return mode;
These are only hints on how to make the code compile.
Your next step is to formalize what you would like it to do and then modify the code accordingly
NB: The correct practice is to think about what you would like to achieve first and then code afterwards (but let us say that this is for the sake of helping each other)
To get started using pointers, you may look at some simple tutorials at first:
http://www.cplusplus.com/doc/tutorial/arrays/
https://www.programiz.com/c-programming/c-pointers
https://www.programiz.com/c-programming/c-pointers-arrays
https://www.geeksforgeeks.org/pointer-array-array-pointer/
https://www.geeksforgeeks.org/how-to-return-a-pointer-from-a-function-in-c/
https://www.tutorialspoint.com/cprogramming/c_return_pointer_from_functions.htm
Here is the modified code with the stated modifications above (it compiles):
#include <iostream>
using namespace std;
int mode(int *pies, int size) {
int count = 1;
int max = 0;
int mode=pies[0];
for (int i=0; i<size-1; i++)
{
if (pies[i] == pies[i+1])
{
count++;
if (count>max)
{
max = count;
mode = pies[i];
}
}
else
count = 1;
}
return mode;
}
int main() {
int n;
cout<<"Input the number of people: "<<endl;
cin>>n;
int survey[n];
cout << "Enter the amount of pie eaten by each person:" << endl;
for(int i = 0; i < n; i++) {
cout <<"Person "<<(i + 1)<< ": "<<endl;
cin>>survey[i];
}
cout<<"Mode: "<<mode(survey, n)<< endl;
return 0;
}
Hi I'm trying to solve a algorithm problem and when I submit my code on an online judge I keep on getting a runtime error. I have no idea why it is happening.
Here is the problem that I'm trying to solve.
The code is as follows. It works fine for the sample input and outputs in visual studio. I haven't yet met inputs and outputs that does not work well or actually meet the runtime error. Only the online judge is giving the runtime error so I can't figure out why.
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
int n;
int m;
int c1;
int c2;
cin >> n >> m >> c1 >> c2;
vector<int> p = {};
vector<int> q = {};
for (int i = 0; i < n; ++i)
{
int temp;
cin >> temp;
p.push_back(temp);
}
for (int i = 0; i < m; ++i)
{
int temp;
cin >> temp;
q.push_back(temp);
}
vector<int> distance = {};
for (int i = 0; i < p.size(); ++i)
{
for (int j = 0; j < q.size(); ++j)
{
distance.push_back(abs(p[i] - q[j]) + abs(c1 - c2));
}
}
sort(distance.begin(), distance.end());
int min = distance[0];
int count = 0;;
for (int i = 0; i < static_cast<int>(distance.size()); ++i)
{
if (distance[0] == distance[i])
count++;
else
break;
}
cout << min << " " << count << endl;
return 0;
}
If n and m are both the maximum allowed value of 500,000 then distance will have 500,000 * 500,000 elements which will use 1TB of memory. Due to vector growing as you push_back you could actually need around 2TB of memory in total. The online judge presumably doesn't allow you to use this much memory.
If you rethink your algorithm to not store the values of distance it will probably work.
You should lways use reserve on std::vector if you know the size in advance as it should cause the vector to allocate exactly the right amount of memory and avoid copying to new blocks of memory as the vector grows.
OK I'm still a beginner, and i have alot to learn. Still in my first programming class and was wondering if i could get some help on an assignmet. I DON'T WANT YOU TO DO IT FOR ME just some help. I'm supposed to making a lottery type game using arrays and functions. Here's what i have so far:
#include<iostream>
#include<random>
#include<ctime>
using namespace std;
void getPlayersNumbers(int playerArray[], int size);
void getComputersNumbers(int computerArray[], int size);
bool WinningNumber(int playerArray[], int computerArray[], int size);
int main() {
const int SIZE = 5;
int userNumbers[SIZE];
int computerNumbers[SIZE];
getComputersNumbers(computerNumbers, SIZE);
return 0;
}
void getPlayersNumbers(int playerArray[], int size) {
int playersNumbers;
cout << "Please enter 5 numbers for a chance to win!!\n";
for (int i = 0; i < size; i++) {
cin >> ???
}
}
void getComputersNumbers(int computerArray[], int size) {
mt19937 randomGenerator(time(0));
uniform_int_distribution<int> randomNumbers(1, 5);
int computerNumbers;
for (int i = 0; i < size; i++) {
computerNumbers = randomNumbers(randomGenerator);
computerArray[i] = computerNumbers;
cout << computerNumbers << " ";
}
cout << endl;
}
bool winningNumbers(int playerArray[], int computerArray[], int size) {
}
My getComputerNumbers function is working just fine. The one I'm having trouble with is my getPlayerNumbers function. How would i go about getting the uses numbers from them, and keeping them so when i call the function i can compare them to the random numbers in my getComputerNumbers function? Now i already know how I'm going to go about comparing the numbers. That's what my that third function winningNumbers is for. I just Need help with the getPlayersNumbers.
Also if you see anything else that i can do to make this code better let me know.
Thanks again!!
If you want to store user's number into an array:
for (int i = 0; i < size; i++) {
cin >> userNumbers[i]
}
when you want to access a particular number out of the five, you use:
userNumbers[n], where n ranges from 0 to 4
Just do:
void getPlayersNumbers(int* playerArray, int size)
{
cout << "Please enter 5 numbers for a chance to win!!\n";
for (int i = 0; i < size; i++) {
cin >> playerArray [i];
}
}
Thanks to #user4581301 for pointing out a better solution using std::vector
std::vector getPlayersNumbers(int size)
{
std::vector myNumberList;
cout << "Please enter 5 numbers for a chance to win!!\n";
for (int i = 0; i < size; i++) {
int number;
cin >> number;
myNumberList.push_back (number);
}
return myNumberList;
}
STL makes working with arrays a whole lot easier and also it provides additional functions that are optimized for their use case
I am trying to solve this problem arraysub on spoj. I have coded it using a sliding window concept and I am using a max-heap to store the elements of the current window. The problem constraints are of the order 10^5 and I am getting SIGABRT error for my submission. I've read on wiki that this error results if there's a memory leak or problem initialising large arrays. How do I deal with this? long long, long long int, long long unsigned int, doesn't seem to work. Please help. Below is my code:
#include<iostream>
#include<vector>
#include<queue>
using namespace std;
typedef pair<long, long> Pair;
long *a = new long[1000001], *output = new long[1000001];
long n, k;
void slidingWindowMax()
{
priority_queue<Pair> Q;
for(int i=0;i<k; i++)
Q.push(Pair(a[i], i));
for(int i=k; i<n; i++)
{
Pair p = Q.top();
output[i-k] = p.first;
while(p.second <= i-k)
{
Q.pop();
p = Q.top();
}
Q.push(Pair(a[i], i));
}
output[n-k] = Q.top().first;
for(int i=0;i<(n-k+1);i++)
{
cout << output[i] << " ";
}
}
int main()
{
cin >> n;
for(int i=0;i<n;i++)
cin >> a[i];
cin >> k;
slidingWindowMax();
return 0;
}
I want to enter n times values for c and e arrays. The following program doesn't allow me to even enter the value of 'n'. Could you tell me where is the mistake?
#include <iostream>
using namespace std;
int main()
{
int n,c[n],e[n];
cin>>n;
for(int i=0;i<n;i++){
cin>>c[i]>>e[i];
}
return 0;
}
"n" should be defined before using it to fix array size. Also, const int or constant should be used to declare array size not plain int.
In order to use plain datatype, you can initialize array dynamically like
vector<int> a(n); or
int a = new int[n]
int n,c[n],e[n];
This declaration creates arrays c and e on stack with random size, because n as an automatic variable is initialized with random value. Instead you need to dynamically create arrays on heap or use std::vector.
Example:
#include <iostream>
#include <vector>
using namespace std;
int main() {
// your code goes here
int n;
vector<int> v;
std::cin >> n;
v.resize( n);
for( int i = 0; i < n; ++i) {
cin >> v[i];
}
for( int i = 0; i < n; ++i) {
cout << v[i];
}
return 0;
}
http://ideone.com/QhgfNv
In the line of
int n,c[n],e[n];
Computer don't know the exact value of 'n', so it can't alloc memory of array.
The simplest solution is create array with fixed number, and check n after you know the value of n as follows:
int n, c[1024], e[1024];
cin >> n;
if (n > 1024) { /* error */ }
The other way is malloc memory after u know the value of n:
int n;
cin >> n;
int *c = new int[n];
int *e = new int[n];
xxxx
delete [] c;
delete [] e;
You can try something like this:
#include <iostream>
using namespace std;
int main()
{
int temp = 100; /*Random value*/
int c[temp];
int e[temp];
int n;
cin>>n;
for(int i=0;i<n;i++){
cin>>c[i]>>e[i];
}
return 0;
}
Now, I chose temp as 100, but you can do big as your int can store. Now, if n is lower than temp, your for cycle will let you save your values without troubles.