Getting segmentaition fault with subset dp problem - subset

Given a set of numbers, check whether it can be partitioned into two subsets such that the sum of elements in both subsets is same or not
I am getting segmentation fault in C++(g++ 5.4) with a this problem.
This is where i submitted my solution in C++
https://practice.geeksforgeeks.org/problems/subset-sum-problem/0
I am checking if the array can be divided into two parts with equal sum. So I am just checking if there exists a subset with sum equal to half the sum of the array
I have implemented the below logic with dynamic programming
Let dp[i][j] denote yes or no whether a subset with sum j is possible to form with elements in the range [0, i](both inclusive) where i is 0-based index. I have done nothing new with this traditional problem. But I am getting segmentation fault. The program is giving correct output for small test cases. What mistake have I made
I haven't used any comments because I have done nothing new. Hope it is understandable.
#include <iostream>
#include <bits/stdc++.h>
#include<cstdio>
#define ll long long int
using namespace std;
bool isVowel(char c){
return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u';
}
bool isLower(char c){
return 97 <= c && c <= 122;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cout << setprecision(10);
ll t, n;
cin >> t;
while (t--) {
cin >> n;
ll a[n];
ll sum = 0;
for (ll i = 0; i < n; i++) {
cin >> a[i];
sum += a[i];
}
if (sum % 2) {
cout << "NO" << '\n';
continue;
}
sum /= 2;
ll dp[n][sum + 1];
for (ll i = 0; i < n; i++) {
for(ll j = 0; j < sum + 1; j++) {
dp[i][j] = 0;
}
}
for (ll i = 0; i < n; i++) {
dp[i][a[i]] = 1;
dp[i][0] = 1;
}
for (ll i = 1; i < n; i++) {
for (ll j = 1; j < sum + 1; j++){
if (j - a[i] > 0) {
dp[i][j] = dp[i - 1][j - a[i]];
}
dp[i][j] |= dp[i - 1][j];
}
}
cout << (dp[n - 1][sum] ? "YES" : "NO") << '\n';
}
}

The segmentation fault is due to
ll dp[n][sum + 1];
Even though the constraints say 1 <= N<= 100, 0 <= arr[i]<= 1000, the test cases used are probably much larger, so ll dp[n][sum + 1] will end up taking some serious stack memory, use
bool dp[n][sum + 1];
It should work fine.
On a side note, avoid using ll randomly, use them according to the constraints.

Related

Remove the first group of consecutive negative array elements from the array

Delete the first group of consecutive negative
elements in the array.
void delNegative(int* arr, int& size) {
if (!check_prop(arr, size, neg)) return;
int ind_l, ind_f;//
if (arr[0] < 0) ind_f = 0;
if (arr[size - 1] < 0) ind_l = size - 1;
for (int i = 1; i < size; i++){
if (arr[i - 1] > 0 && arr[i] < 0) ind_f = i;
if (arr[i - 1] < 0 && arr[i] > 0) ind_l = i - 1;
}
if (ind_l < ind_f) ind_l = size - 1;
int ii = 0;
for (int i = 0; i < size; i++)
if (i > ind_l || i < ind_f) {
arr[ii] = arr[i];
ii++;
}
size -= ind_l - ind_f + 1;
}
There is this function, but it removes the last group of negative elements.
It needs to be redone for dynamic arrays

what is the subproblem here and how it is being calculated

N exams numbered from 1 to N are going be held at Geekland state university. Geek is a renowned professor at the university and therefore is given the task to design the datesheet for exams. Datesheet must be made in such a way that total number of holidays in the datesheet is exactly K i.e. sum of holidays given for each exam is exactly K. Moreover, no exam should have more than M holidays. Two datesheets are considered different if number of holidays for a particular exam in both the datesheets is different. Find number of valid datesheets Geek can choose from. As answer can be very large, find it modulo 10^9 + 7.
solution:
int datesheets(int N, int K, int M){
int mod = 1000000007;
vector<vector<int>> dp(N+1, vector<int>(K+1, 0));
for(int i = 0; i <= N; i++)
dp[i][0] = 1;
for(int i = 1; i <= N; i++)
{
for(int j = 1; j <= K; j++)
{
dp[i][j] += dp[i-1][j];
dp[i][j] %= mod;
dp[i][j] += dp[i][j-1];
dp[i][j] %= mod;
if(j-M>0)
{
dp[i][j] = dp[i][j] - dp[i-1][j-M-1] + mod;
dp[i][j] %= mod;
}
}
}
return dp[N][K];
}

Run-Time Check Failure #2 - Stack around the variable 'TP' was corrupted

I'm trying to do a pascal triangle but for some reason i have the error "Run-Time Check Failure #2 - Stack around the variable 'TP' was corrupted." Can someone help me, please?
#include <iostream>
using namespace std;
void main()
{
int TP[100][100] = { 0 }, n;
do
{
cout << "Digite a ordem do triangulo de pascal: ";
cin >> n;
} while (n < 0 || n > 100);
for (int j = 0; j < n; j++)
{
TP[j][0] = 1;
TP[j][j] = 1;
}
for (int i = 2; i < n + 1; i++)
for (int j = 1; TP[i][j] != 1; j++)
TP[i][j] = TP[i - 1][j - 1] + TP[i - 1][j];
for (int i = 0; i < n; i++)
{
cout << endl;
for (int j = 0; j != i + 1; j++)
cout << TP[i][j] << " ";
}
cout << endl << endl;
system("pause");
}
You go beyond the array boundaries in for (int i = 2; i < n + 1; i++) for i == n when n=100.

time command shows user time greater than real time

I have more or less the same question as
linux time command resulting real is less than user
and
user time larger than real time
but can't post a comment on those questions.
When I run the non-multi-threaded program given below, I occasionally get user time greater than real time with both /usr/bin/time and bash's builtin time. I don't see anything that might use a different core. Is rand() somehow the culprit? How? Thanks!
#include <stdio.h>
#include <stdlib.h>
#define N 100
#define MM_MAX 50000
int
main(int ac, char **av)
{
unsigned int i, j, k, n;
int A[N][N], B[N][N], C[N][N];
if (ac != 2) {
fprintf(stderr, "Usage: matmul <seed>");
exit(1);
}
srand((unsigned int) atoi(av[1]));
for (n = 0; n < atoi(av[1]); n++) {
for (i = 0; i < N; i++) {
for (j = 0; j < N; j++) {
A[i][j] = rand() % MM_MAX;
B[i][j] = rand() % MM_MAX;
}
}
for (i = 0; i < N; i++) {
for (j = 0; j < N; j++) {
C[i][j] = 0;
for (k = 0; k < N; k++) {
C[i][j] += A[i][k] * B[k][j];
}
printf("%7d ", C[i][j]);
}
putchar('\n');
}
}
return 0;
}

C/C++: 'Attempted to read or write protected memory" exception

My code compiles but throws the following exception:
An unhandled exception of type 'System, Access Violation Exception' occured Additional Information: Attempted to read or write protected memory. . .
the error is related to s=s+a[z][r]*b[f][h]
Here a copy of the code:
#include"stdafx.h"
#include"iostream"
using namespace std;
int main()
{
int **a, **b;
int z, r, f, h, a_r, a_c, b_r, b_c, s = 0;
cout << "Enter the size of the matrix(nxm) :" << endl;
cin >> a_r >> a_c;
cout << "enter the size of the mask :" << endl;
cin >> b_r >> b_c;
a = (int **) malloc(10 * a_r);
for (int i = 0; i < a_c; i++)
{
a[i] = (int *) malloc(10 * a_c);
}
b = (int **) malloc(10 * b_r);
for (int i = 0; i < b_c; i++)
{
b[i] = (int *) malloc(10 * b_c);
}
for (int i = 0; i < a_r; i++)
{
for (int j = 0; j < a_c; j++)
{
a[i][j] = i + j;
}
}
for (int i = 0; i < b_r; i++)
{
for (int j = 0; j < b_c; j++)
{
b[i][j] = i + j;
}
}
int k = 1, d = 1;
for (int i = 0; i < a_r; i++)
{
for (int j = 0; j < a_c; j++)
{
for (int n = -1; n <= 1; n++)
{
for (int e = -1; e <= 1; e++)
{
z = i + n;
r = j + e;
f = k + n;
h = d + e;
if (z < 0 || z > a_r || r < 0 || r > a_c)
{
s = s + 0;
} else {
s = s + a[z][r] * b[f][h]; // runtime error occurs here
}
}
}
a[i][j] = s;
s = 0;
}
}
return 0;
}
Here is one problem:
if (z < 0 || z > a_r || r < 0 || r > a_c)
This should read:
if (z < 0 || z >= a_r || r < 0 || r >= a_c)
Otherwise you're potentially accessing out-of-bounds elements.

Resources