When I run this code in VScode I don't get any error but the program doesn't take input and ends.
#include <iostream>
using namespace std;
int main()
{
int t, N, a, b, i, j, k, n, l = 1, m = 1;
int number[N];
cin >> t;
for (k = 1; k <= t; k++)
{
cin >> N >> a >> b;
for (i = 1; i <= N; i++)
{
cin >> number[i];
}
for (j = l; j <= N; j++)
{
if (number[n] % a != 0)
{
cout << "ALICE" << endl;
break;
}
if (number[j] % a == 0)
{
l = j;
break;
}
}
for (n = m; n <= N; n++)
{
if (number[n] % b != 0)
{
cout << "BOB" << endl;
break;
}
if (number[n] % b == 0)
{
m = n;
break;
}
}
}
return 0;
}
Please explain why I am getting this error.
The reason you are getting the error because of the line int number[N]; Here N is not defined and N can have any garbage value. You have to either statically define the value of N which will result in memory leakage because you initially doesn't know how much length array is required to avoid this leakage you can use vectors for dynamic allocation.
For vector implementation you can refer this.
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";
}
This is my first kickstart problem attempt (Round C 2020 problem 4 - Candies) and although my attempted solution works on vscode and passes the sample cases on the kickstart platform, it is giving me "runtime error" for test set 1. Any ideas why this is happening? I've tried changing the value of N (initially with a larger value it was giving me TLE) and "ints" to "long longs" but nothing seems to be working. I wrote my attempt based on the official analysis so I would have thought it would pass, although this is the first time I try using segment trees so maybe I'm missing something obvious with the implementation. Any help would be much appreciated.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 101;
int n;
vector<ll> t(2*N);
vector<ll> t1(2*N);
void build() {
for (int i = n - 1; i > 0; --i) t[i] = t[2*i] + t[2*i+1];
}
void build2() {
for (int i = n - 1; i > 0; --i) t1[i] = t1[2*i] + t1[2*i+1];
}
int sum(int a, int b) {
a += n; b += n;
int s = 0;
while (a <= b) {
if (a%2 == 1) s += t[a++]; //array[i++] is equivalent to array[i] THEN ++i; or i++; N.B. array[++i]; would be equivalent to ++i; or i++ THEN array[i];
if (b%2 == 0) s += t[b--];
a /= 2; b /= 2;
}
return s;
}
int sum2(int a, int b) {
a += n; b += n;
int s = 0;
while (a <= b) {
if (a%2 == 1) s += t1[a++]; //array[i++] is equivalent to array[i] THEN ++i; or i++; N.B. array[++i]; would be equivalent to ++i; or i++ THEN array[i];
if (b%2 == 0) s += t1[b--];
a /= 2; b /= 2;
}
return s;
}
void replace(int k, int x) {
k += n;
t[k] = x;
for (k /= 2; k >= 1; k /= 2) {
t[k] = t[2*k]+t[2*k+1];
}
}
void replace2(int k, int x) {
k += n;
t1[k] = x;
for (k /= 2; k >= 1; k /= 2) {
t1[k] = t1[2*k]+t1[2*k+1];
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
ll T, q;
cin >> T;
for(int i = 0; i < T; i++){
cin >> n >> q;
for(int i = 0; i<n; i++){
int temp, temp2;
cin >> temp;
temp2 = temp;
temp = pow((-1),(i))*temp;
temp2 = pow((-1),(i))*temp2*(i+1);
t[i+n] = temp;
t1[i+n] = temp2;
}
build();
build2();
ll ans = 0;
char c;
ll l,r;
for(int i = 0; i<q; i++){
cin >> c >> l >> r;
l--;
r--;
if(c=='Q'){
if(l%2==1){
ans-=sum2(l,r)-((l)*sum(l,r));
}
else{
ans+=sum2(l,r)-((l)*sum(l,r));
}
}
else{
if(l%2==1){
replace(l,-(r+1));
replace2(l,-((r+1)*(l+1)));
}
else{
replace(l,(r+1));
replace2(l,((r+1)*(l+1)));
}
}
}
cout << "Case #" << i+1 << ": " << ans << endl;
}
}
The following piece of code takes the input but does not execute or return any output.It takes a matrix and its size and an integer to be searched as input. I am failing to understand the issue here.
#include<iostream>
using namespace std;
int main()
{
int n, m;
cin >> n >> m;
int a[n][m];
int t;
cin >> t;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
cin >> a[i][j];
}
}
int r = 0, c = m - 1;
bool found = false;
while (r < n && c >= 0)
{
if (a[r][c] == t)
{
found = true;
}
else if (a[r][c] < t)
{
r++;
}
else
{
c--;
}
}
if(found)
{
cout << "found";
}
else
{
cout << "not found";
}
return 0;
}
Writing break after the number is found gives what you want. You are not exiting the loop after a number has been found.
#include<iostream>
using namespace std;
int main()
{
int n, m;
cin >> n >> m;
int a[n][m];
int t;
cin >> t;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
cin >> a[i][j];
}
}
int r = 0, c = m - 1;
bool found = false;
while (r < n && c >= 0)
{
if (a[r][c] == t)
{
found = true;
break;
// ^^^^^^^
}
else if (a[r][c] < t)
{
r++;
}
else
{
c--;
}
}
if(found)
{
cout << "found";
}
else
{
cout << "not found";
}
return 0;
}
UPDATE 1.0: Updated code considering comments of variable length array. Used vector in C++ to achieve the same purpose.
#include<iostream>
#include<vector>
using namespace std;
int main()
{
int n, m;
cin >> n >> m;
// Using vector of vectors instead of 2-D array
vector< vector <int> > a(n);
for(int i = 0; i < n; i++){
a[i] = vector<int> (m);
}
int t;
cin >> t;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
cin >> a[i][j];
}
}
int r = 0, c = m - 1;
bool found = false;
while (r < n && c >= 0)
{
if (a[r][c] == t)
{
found = true;
break;
// ^^^^^^^
}
else if (a[r][c] < t)
{
r++;
}
else
{
c--;
}
}
if(found)
{
cout << "found";
}
else
{
cout << "not found";
}
return 0;
}
I wrote this solution for the absolute permutation problem on HackerRank. It works fine on dev-C++ but doesn't work on Hackerrank. I've found that the code produces output when I remove the abs_perm(). What's the problem here?
#include <iostream>
using namespace std;
int arr[100000];
int check(int n, int k)
{
if ( (2*k == n) || (k == 0) || (n - 4*k == 0) )
return 1;
else if (k < n/2)
return check(n - 4*k, k);
else
return 0;
}
void swap(int &a, int &b)
{
int c = b;
b = a;
a = c;
}
void ini(int n)
{
for (int i = 0; i < n; i++)
{
arr[i] = i+1;
}
}
void abs_perm(int n, int k)
{
for (int i = 0; i < k; i++)
{
swap(arr[i], arr[k+i]);
}
if (2*k == n)
return;
for (int i = n - 1; i > n - k - 1; i--)
{
swap(arr[i], arr[i-k]);
}
if (n - 4*k == 0)
return;
abs_perm(n - 4*k, k);
}
int main()
{
int T;
cin >> T;
int N[T], K[T];
for (int i = 0; i < T; i++)
{
cin >> N[i] >> K[i];
}
for (int i = 0; i < T; i++)
{
cout << N[i] << " " << K[i] << "\n";
}
for (int i = 0; i < T; i++)
{
if ( !check(N[i], K[i]) )
cout << "-1\n";
else
{
ini(N[i]);
abs_perm(N[i], K[i]);
for (int j = 0; j < N[i]; j++)
{
cout << arr[j] << " ";
}
cout << "\n";
}
}
return 0;
}
Array is a structure to use when you know at compile time the dimension of your structure. What you wrote at the begin in abs_perm() is not correct for standard compilers (in fact you don't know the dimension of your array). You can use a std::vector or a std::list which allocate memory dynamically or (bad solution) you can allocate an array with dimension that certainly contains all elements you will put inside.
Here's my C++ code for the 3n+1 problem from UVA online judge which runs fine here, but each submission is judged as the wrong solution. I believe it has something to do with input, or output formatting. I just don't know exactly what the issue is. Could anyone help me investigate this issue?
#include <iostream>
using namespace std;
int main(){
int i, j, ori, orj, complexity = 0;
while(!cin.eof()){
cin >> i >> j;
ori = i;
orj = j;
if (i > j){
int temp = i;
i = j;
j = temp;
}
for (int k = i; k <= j; k++){
int c = 1;
int n = k;
do{
c++;
if (n % 2 == 0)
n /= 2;
else
n = 3 * n + 1;
} while (n != 1);
if (c > complexity)
complexity = c;
}
cout << ori << " " << orj << " " << complexity << endl;
}
return 0;
}
Each submission has come under the time limit, and when I debug i get correct outputs.
!cin.eof() is not a good way to determine when to exit from the loop.
Try changing
while(!cin.eof()){
cin >> i >> j;
to
while(cin >> i >> j){
You have to clear value of complexity variable for every new input numbers. Your code also doesn't handles correctly values of i and j equal to 1. You have to add special condition and not to run the loop in this case. So this it the whole code change:
int main() {
int i, j, ori, orj, complexity;
while (!cin.eof()) {
complexity = 0;
cin >> i >> j;
ori = i;
orj = j;
if (i > j) {
int temp = i;
i = j;
j = temp;
}
if (j > 1) {
for (int k = i; k <= j; k++) {
int c = 1;
int n = k;
do {
c++;
if (n % 2 == 0)
n /= 2;
else
n = 3 * n + 1;
} while (n != 1);
if (c > complexity)
complexity = c;
}
} else {
complexity = 1;
}
cout << ori << " " << orj << " " << complexity << endl;
}
return 0;
}