Sigabrt runtime error occurs of a fatal error, because of an assert statement not returning true? Or use of excessive memory, I'm not able to figure out what I'm doing wrong here, help me out?
( problem 1343 C on codeforces) link
so here's the code.
#include <iostream>
#include <stdlib.h>
#include<vector>
using namespace std;
int check(int i,vector<int> a) {
if (a[i] > 0) {
return 1;
}
else return 0;
}
int main() {
int t;
cin >> t;
while (t--)
{
long int n;
cin >> n;
vector<int> a(n), b;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
int i = 0;
while (i < n)
{
int max = a[i];
int s = check(i,a);
i++;
while (i<n && check(i,a)== s) {
if (a[i] > max)max = a[i];
i++;
}
b.push_back(max);
}
int s = 0;
for (int k = 0; k< b.size(); k++) {
s += b[i];
}
cout << s << endl;
}
}
I have debugged your code and also the modified code has been accepted for the above question.
Mistakes you made:
1. In the below loop, value at i'th index of vector<int> b is being added to long int s. Instead, b[k] should be added to long int s because the variable being used in the loop is k not i.
for (int k = 0; k< b.size(); k++) {
s += b[i];
}
2. In the question, range of variable n is given as (1 ≤ n ≤ 2.10^5). So, it is safe to use int n instead of long int n. Also, when I submitted my code on codeforces it gave me signed integer overflow error when I used long int n.
3. You need to use long long s instead of long int s because the value of each element of array A lies between (−10^9 ≤ a[i] ≤ 10^9 , ai ≠ 0) and when we add the elements it can easily surpass int and long int ranges.
4. Although, the answer got accepted when I used vector<int> a in the function
int check(int i,vector<int> a) {
if (a[i] > 0) {
return 1;
}
else return 0;
}
But as the user Scheff has said and is correct that it comes with a penalty in space and time, you should use call by reference i.e. vector<int> &a.
Modified Code:
#include <iostream>
#include <stdlib.h>
#include<vector>
using namespace std;
int check(int i, vector<int> &a) {
if (a[i] > 0) {
return 1;
}
else return 0;
}
int main() {
int t;
cin >> t;
while (t--)
{
int n;
cin >> n;
vector<int> a(n), b;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
int i = 0;
while (i < n)
{
int max = a[i];
int s = check(i,a);
i++;
while ((i<n) && (check(i,a)== s)) {
if (a[i] > max)
max = a[i];
i++;
}
b.push_back(max);
}
long long s = 0;
for (int k = 0; k< b.size(); k++) {
s += b[k];
}
cout << s << endl;
}
}
Screenshot of Accepted Answer:
Related
I tried to solve the problem but my code still contains some bugs. Why isn't it running?
Here is the link of the question website: https://www.hackerearth.com/practice/data-structures/hash-tables/basics-of-hash-tables/practice-problems/algorithm/pair-sums/?
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
const int n = 1e7 + 10;
int hsh[n];
int main()
{
int n, k;
cin >> n >> k;
int A[n];
for (int i = 0; i < n; i++)
{
cin >> A[i];
}
for (int i = 0; i < n; i++)
{
hsh[A[i]] = k - A[i];
}
int t = 0;
for (int i = 0; i < n; i++)
{
if (hsh[A[i]] == k - hsh[hsh[A[i]]])
{
cout << "YES";
t = 1;
break;
}
}
if (t == 0)
{
cout << "NO";
}
return 0;
}
The problem is that while hsh[A[i]] is always valid, hsh[hsh[A[i]] is not.
Consider the following input:
1 1
10000
This does the following:
A[0] = 10000;
...
hsh[10000] = 1 - 10000; // = -99999
...
if (hsh[10000] == 1 - hsh[-99999]) {...}
So your code is reading out of bounds of the array hsh[]. Make sure you check first if hsh[A[i]] >= 0.
Note that your code is more complicated than necessary; you can do a single loop over the input to check if there is a matching pair:
#include <iostream>
static constexpr int max_k = 2e6;
static bool seen[max_k + 1];
int main()
{
int n, k;
std::cin >> n >> k;
for (int i = 0; i < n; ++i)
{
int A;
std::cin >> A;
if (A <= k && seen[k - A]) {
std::cout << "YES\n";
return 0;
}
seen[A] = true;
}
std::cout << "NO\n";
}
As you see in my code, I have to return an array through return function. What necessary thing do I have to change in my code to disable the warning of return type? When I run this code, it gives me a warning. I have to remove this warning:
warning: no return statement in function returning non-void [-Wreturn-type]
#include <bits/stdc++.h>
using namespace std;
int sort(int a[], int n)
{
int zeros = 0;
for (int i = 0; i < n; i++)
{
if (a[i] == 0)
{
zeros++;
}
}
int k = 0;
while (zeros--)
{
a[k++] = 0;
}
while (k < n)
{
a[k++] = 1;
}
}
int main()
{
int n;
cout << "Enter the n value : ";
cin >> n;
int a[n];
cout << "Enter the array elements : ";
for (int i = 0; i < n; i++)
{
cin >> a[i];
}
sort(a, n);
for (int i = 0; i < n; i++)
{
cout << a[i];
}
return 0;
}
int sort(int a[], int n)
Your sort function is declared to return an int, but you didn't return anything. If you don't want to return anything, declare it using void.
Also, VLA is not valid C++. You should use std::vector instead.
So, your code should look like this.
#include <iostream>
#include <vector>
void sort(std::vector<int> &a) // no need for n
{
int zeros = 0;
for (int i = 0; i < a.size(); i++) // use member function size() instead for n
{
if (a[i] == 0)
{
zeros++;
}
}
int k = 0;
while (zeros--)
{
a[k++] = 0;
}
while (k < n)
{
a[k++] = 1;
}
}
int main()
{
int n;
std::cout << "Enter the n value : ";
std::cin >> n;
std::vector<int> a(n);
std::cout << "Enter the array elements : ";
for (int i = 0; i < n; i++)
{
cin >> a[i];
}
sort(a);
for (cons auto &i : a) // range-base for loop, recommend
{
std::cout << a[i];
}
return 0;
}
Note: using namespace std; and #include <bits/stdc++.h> are both bad practice, so avoid using it.
In an array of N elements (N is given), find the smallest element from the first zero element to the end of the array. If there are no zero elements in the array, display a message about it.
Can someone fix this for me please?
#include<iostream>
using namespace std;
int main() {
int i, n;
cout << "N= "; cin >> n;
if (n > 0) {
int *a = new int[n];
for (i = 0; i < n; i++) {
cin >> a[i];
}
for (i = 0; i < n; i++) {
if (a[0] > a[i])
a[0] = a[i];
}
cout << "\nMin:" << a[0];
delete[] a;
}
return 0;
}
I couldn't find the problem with your code so I am showing how I approached finding a minimum element from a given array:
#include<iostream>
using namespace std;
int main()
{
int n;
cin>>n;
int arr[n];
for(int i=0;i<n;i++){
cin>>arr[i];
}
int minNo=INT16_MAX; // to assign max. possible value to minNo
for(int i=0;i<n;i++){
minNo=min(arr[i],minNo);
}
cout<<minNo<<endl;
return 0;
}
You can simply check it in the same loop in which you looking for smallest element.
#include <iostream>
#include <climits>
using namespace std;
int main() {
int i, n;
bool isZeroElement = false;
cout << "N= "; cin >> n;
if (n > 0) {
int *a = new int[n];
for (i = 0; i < n; i++) {
cin >> a[i];
}
int minElement = INT_MAX;
for (i = 0; i < n; i++) {
if (!isZeroElement && a[i] == 0)
{
isZeroElement = true;
continue;
}
if (isZeroElement && minElement > a[i])
minElement = a[i];
}
if (!isZeroElement)
cout << "There is no zero element in the array\n";
else
cout << "\nMin:" << minElement;
delete[] a;
}
return 0;
}
This is a factorial program,I made on a coding site.Kindly help me find why its wrong?
#include <iostream>
using namespace std;
int main()
{
int i, j, f = 1;
int t, a[100];
cin >> t;//enter test cases
for (i = 0; i < t; i++)
cin >> a[i];//enter all the cases one by one
for (i = 0; i < t; i++)
{
for (j = 1; j <= a[i]; j++)
f = f*j;
cout << f;//displays each factorial
}
return 0;
}
You should reinitialize the value of f back to 1 at the start of each test case.
#include <iostream>
using namespace std;
int main()
{
int i, j, f = 1;
int t, a[100];
cin >> t;//enter test cases
for (i = 0; i < t; i++)
cin >> a[i];//enter all the cases one by one
for (i = 0; i < t; i++)
{
f = 1 // add this line
for (j = 1; j <= a[i]; j++)
f = f*j;
cout << f;//displays each factorial
}
return 0;
}
You should reinitialize the value for your factorial variable f each time you are computing the factorial, also you need not use array to store all the asked factorial cases unnecessarily waste of memory.
Here is the optimized solution:
#include <iostream>
using namespace std;
int main()
{
int i, j, f,num;
int t;
cin >> t; //enter test cases
for (i = 0; i < t; i++)
{
cin >> num;
f = 1;
for (j = 1; j <= num; j++) {
f = f * j;
}
//displays each factorial
cout << f << endl;
}
return 0;
}
include
#include <fstream>
#include <algorithm>
using namespace std;
long int lenghtOfLongestAP(long int set[],long int n)
{
if (n <= 2) return n;
long int L[n][n];
long int llap = 2;
for (long int i = 0; i < n; i++)
L[i][n-1] = 2;
for (long int j=n-2; j>=1; j--)
{
int i = j-1, k = j+1;
while (i >= 0 && k <= n-1)
{
if (set[i] + set[k] < 2*set[j])
k++;
else if (set[i] + set[k] > 2*set[j])
{ L[i][j] = 2, i--; }
else
{
L[i][j] = L[j][k] + 1;
llap = max(llap, L[i][j]);
i--; k++;
}
}
while (i >= 0)
{
L[i][j] = 2;
i--;
}
}
return llap;
}
int main()
{
ofstream cout("Output.txt");
ifstream cin("cablecar-sub4-attempt3.txt");
int ab;
cin >> ab;
for (long int z = 0; z < ab; z++)
{
long int bs;
cin >> bs;
long int array[bs];
for(long int h = 0; h<bs; h++)
cin >> array[h];
sort(array, array + bs);
cout << "Case #" << z+1 << ": " << lenghtOfLongestAP(array, bs) << endl;
}
return 0;
}
This is my code. It is a LAP (Largest arithmetic progression) algorithm, so it finds the largest progression in an sorted array. I have the following set of data:
pastebin.com/77meKfKW
Strangely, the program crashes after case 30, which it shouldn't. What kind of problem might it be and how can I fix it?
This is probably a stack overflow. You are allocating your array on the stack with 267*267 entries which uses a lot of memory.
Try allocating the memory on the heap instead, or simply changing the array from being local to being global (with a fixed maximum value of n).
e.g.
change
long int lenghtOfLongestAP(long int set[],long int n)
{
if (n <= 2) return n;
long int L[n][n];
to
long int L[1000][1000]; // or whatever your maximum n might be
long int lenghtOfLongestAP(long int set[],long int n)
{
if (n <= 2) return n;