Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nobel Integer #6

Open
bhasindhruv117 opened this issue Jun 26, 2019 · 2 comments
Open

Nobel Integer #6

bhasindhruv117 opened this issue Jun 26, 2019 · 2 comments

Comments

@bhasindhruv117
Copy link

If all the elements are negative except one and that it 0 it returns -1 instead of 1

@lavishsaluja
Copy link

@bhasindhruv117
True, there should be an if condition.

int Solution::solve(vector<int> &v) { sort(v.begin(), v.end()); int n = v.size(); for(int i = 0; i < n; i++){ if(i != n-1) if(v[i] == v[i+1]) continue; if(v[i] == (n-i-1)) return 1; } return -1; }

@ghost
Copy link

ghost commented May 17, 2021

if the last element of sorted array is zero then 'continue' is executed . Due to this some times we get wrong answer for some cases .
An If loop can be applied only the to line containing continue statement.
int solve(vector &A) {
int n=A.size();
sort(A.begin(),A.end());
for(int i = 0;i<n;i++)
{
if(i<n-1)
{
if(A[i]==A[i+1]) continue;
}
if(A[i]==n-i-1) return 1;
}
return -1;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants