Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I have a problem with this simple code. Propably the problem is with using the vector class:
int solution(vector<int> &A) {
// write your code in C++14 (g++ 6.2.0)
vector<int> left, right;
int cnt=0,k,j;
if (A.size()==1) return 0;
if (A.size()==2 && A[0]==A[1]) return 1;
if (A.size()==2 && A[0]!=A[1]) return 0;
if (A.size()==3 && A[0]==A[1] && A[1]==A[2]) return 2;
if (A.size()==3 && A[0]==A[1] && A[1]!=A[2]) return 0;
for (size_t i=0; i<(A.size()-1); ++i)
{
left.assign( A.begin(), A.begin() + i );
right.assign( A.begin() + i + 1, A.end());
sort(left.begin(), right.end());
sort(right.begin(), right.end());
j=left.size()/2;
k=right.size()/2;
if (left[j]==right[k])
{
cnt++;
}
}
return cnt;
}
after execution it return :
stderr:
Segmentation Fault
left[j] is out-of-bounds if i==0 because left.size() is i, so 0.
Similar for right[k].
Also the case A.size() == 0 is not considered and will wrap around A.size()-1.
There is also a typo in sort(left.begin(), right.end()): right.end() should be left.end().
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 10 days ago.
This post was edited and submitted for review 10 days ago.
Improve this question
What is wrong with this code?
class Solution {
public:
bool uniqueOccurrences(vector<int> &arr) {
map<int, int> mp;
for (auto x : arr) {
mp[x]++;
}
vector<int> v;
for (auto x : mp) {
v.push_back(x.second);
}
sort(v.begin(), v.end());
for (int i = 0; i < v.size() - 1; i++) {
if (v[i] != v[i + 1])
return true;
}
return false;
}
};
Given an array of integers arr, return true if the number of occurrences of each value in the array is unique or false otherwise.
This was the question statement and was working fine with various test cases but fails in the follow test case: [3,5,-2,-3,-6,-6] should return false but returns true
Your problem is here:
for (int i = 0; i < v.size() - 1; i++) {
if (v[i] != v[i + 1])
return true;
}
return false;
You should swap the return true and return false.
for (int i = 0; i < v.size() - 1; i++) {
if (v[i] != v[i + 1])
return false;
}
return true;
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 months ago.
Improve this question
I'm writing a simple program to search for an element in a 2D matrix. It's not giving me an active/named exception, but it just throws me a Runtime error.
Any help with as to why this is happening?
class Solution {
public:
bool searchMatrix(vector<vector<int>>& matrix, int target) {
for (int i=0 ;i< matrix.size(); ++i){
int m = matrix[i].size() -1 ;
int n = matrix.size()-1;
if (matrix[n][m] < target){
return false;
}
else if (matrix[i][m] < target){
i++;
} else {
for (int j=0;j <= m; j++ ){
if (matrix[i][j] == target){
return true;
} else if (matrix[i][j] != target) {
continue;
}
}
if (matrix[i][m] != target ){
return false;
}
}
}throw;
}
};
}throw;
A throw without following expression can be used inside an exception handler to re-throw the currently handled exception.
However, at this point your program does not have an active exception. In this case, throw calls std::terminate().
This seems to be an edit artifact.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
This is code for the intersecting arrays. It seems to work for most of the cases until
nums1 = [1]
nums2 = [1,1]
class Solution {
public:
vector<int> intersect(vector<int>& nums1, vector<int>& nums2) {
vector<int> temp;
int i = 0;
int j = 0;
for (i = 0; i < nums1.size(); i++) {
for (j = 0; j < nums2.size(); j++) {
if ( (nums1.size() == nums2.size()) && (nums1[i] < nums1.size() - 1) ) {
if (nums1[i] == nums2[j])
temp.push_back(nums1[i]);
}
else if ( (nums1[i] == nums2[j]) && (i < nums1.size()) ) {
temp.push_back(nums1[i]);
i++;
}
}
}
return temp;
}
};
when i = 1, I get the error, but with the condition that (nums1[i] == nums2[j]) && (i < nums1.size()), why does it still go there and get stuck?
LeetCode 349
The order of your checks are wrong. You need to check if i is a valid index before indexing, like this:
else if ( (i < nums1.size()) && (nums1[i] == nums2[j]) ) {
otherwise you invoke undefined behavior (which could cause a run-time error).
Similarly, in all the cases where you are indexing into a vector, you need to be sure that the index is valid.
The operands of && are evaluated left to right, post-confditions will be checked if pre-conditions are all valid.
BTW, modifying the index in a for-loop is not recommended. In this case, you are likely to do repeated work. if you really want to modify the index to make your algorithm works, a while-loop is better.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
What is wrong with my code?
It has to find the maximum between n numbers.
#include<iostream>
using namespace std;
int main()
{
int n,i = 0;
cin >> n;
int a[n];
while(i < n)
{
cin >> a[i];
i++;
}
i=1;
while(i <= (n + 1))
{
if (a[i] > a[0])
{
a[0] = a[i];
}
i++;
}
cout << a[0];
return 0;
}
This is meant to be a learning thing for you so I don't want to do your code for you. That being said I would look at while(i <= (n + 1)). You are stepping outside of the bounds of your array.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
#include <iostream>
using namespace std;
int compute_binare_code(int n, int a[][4]){
int i,j;
int bC = 0, p = 1;
for(i = 1;i <= n;i++){
for(j = i+1;j <= n;j++){ //just counting bC
bC += a[i][j]*p;
p =p*2;
}
}
return bC;
}
int main(){
cout << "mata3";
int a[4][4],b[5][5],i,j;
for(i=1;i<=4;i++)
for(j=1;j<=4;j++)
if( (i+j)%2 == 0)
a[i][j]=0;
else
a[i][j]=1;
cout<<"mata1";
cout<<compute_binare_code(4, a);
cout<<"mata2";
return 0;
}
When i run this program, it doesn't give any error but it runs in background forever. It does not print anything, not even "mata3". Can someone explain me why?
Arrays in C++ are indexed from 0.
Your for loop increments i/j from 1 to 4, but it should be 0 to 3.
See also Accessing an array out of bounds gives no error, why?