Code giving many run time errors but runs fine on my PC

#include <iostream">
#include <vector">

using namespace std;

int main()
{
int a, tjs, cjs;
cin >> a;

while (a--)
{
	cin >> tjs >> cjs;

	int *jobs = (int*)malloc(tjs * sizeof(int));

	for (int i = 1; i <= tjs; i++)
	{
		jobs[i] = 0;
	}

	for (int i = 0; i < cjs; i++)
	{
		int hold;
		cin >> hold;
		jobs[hold] = 1;
	}

	std::vector<int> dot;

	for (int i = 1; i <= tjs; i++)
	{
		if (jobs[i] == 0)
		{
			dot.push_back(i);
		}
	}

	int k = 0;
	for (auto x : dot)
	{
		if (k % 2 == 0)
		{
			cout << x << " ";
		}
		k++;
	}
	putchar('\n');

	k = 0;
	for (auto x : dot)
	{
		if (!(k % 2 == 0))
		{
			cout << x << " ";
		}
		k++;
	}
	putchar('\n');
}

}

Okay so the above code runs perfectly on my pc no matter what but when I try it to submit it either gives me a run time error(SIGABRT) or error(prog: malloc.c:2403: sysmalloc: Assertion `(old_top == initial_top (av) && old_size == 0) || ((unsigned long) (old_size) >= MINSIZE && prev_inuse (old_top) && ((unsigned long) old_end & (pagesize - 1)) == 0)’ failed.)

I’m quite sure I’m allocating the right amount of memory for the jobs array but if not, how do I fix it?

You have alloted tjs size to jobs, and technically, jobs[tjs] is the (tjs+1)th element of it. It runs fine on ur PC just because it doesn’t give runtime, and simply allots the next memory for jobs[tjs], while in Online, it’s accurate for errors.

Sorry I’m still new to this. I get that my pc just allocates the next memory for it. However, what would I need to change in my code to not have that issue?

Simply allocate tjs+1 space instead of tjs. Was that ur doubt?

Yes, can you explain why I need to do +1?

To allocate space that isn’t automatically done

So without adding +1it won’t allocate automatically?

int t = 5;
int a = (int)malloc(t + 1 * sizeof(int)); //this will work but I still get SIGTSTP error due to it exceeding the time limit now…

I tried doing int a = (int)calloc(t + 1 , sizeof(int)) //Better since I don’t have to initialize all elements to 0 with a loop

Even using calloc gives me SIGCONT error…

EDIT Running the program with calloc gives SIGCONT when just testing it for run, but submitting it says it’s the correct solution? How does that work? (CodeChef: Practical coding for everyone)