A non-empty array A consisting of N numbers is given. The array is sorted in non-decreasing order. The absolute distinct count of this array is the number of distinct absolute values among the elements of the array.
For example, consider array A such that:
A[0] = -5 A[1] = -3 A[2] = -1 A[3] = 0 A[4] = 3 A[5] = 6The absolute distinct count of this array is 5, because there are 5 distinct absolute values among the elements of this array, namely 0, 1, 3, 5 and 6.
Write a function:
int solution(vector<int> &A);
that, given a non-empty array A consisting of N numbers, returns absolute distinct count of array A.
For example, given array A such that:
A[0] = -5 A[1] = -3 A[2] = -1 A[3] = 0 A[4] = 3 A[5] = 6the function should return 5, as explained above.
Write an efficient algorithm for the following assumptions:
- N is an integer within the range [1..100,000];
- each element of array A is an integer within the range [−2,147,483,648..2,147,483,647];
- array A is sorted in non-decreasing order.
// you can use includes, for example:
#include <bits/stdc++.h>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &A) {
// write your code in C++14 (g++ 6.2.0)
int len = A.size();
for(int i=0; i<len; i++){
}
}
// you can use includes, for example:
#include <bits/stdc++.h>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &A) {
// write your code in C++14 (g++ 6.2.0)
int len = A.size();
for(int i=0; i<len; i++){
A[i];
}
}
// you can use includes, for example:
#include <bits/stdc++.h>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &A) {
// write your code in C++14 (g++ 6.2.0)
int len = A.size();
for(int i=0; i<len; i++){
abA[i];
}
}
// you can use includes, for example:
#include <bits/stdc++.h>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
v
int solution(vector<int> &A) {
// write your code in C++14 (g++ 6.2.0)
int len = A.size();
for(int i=0; i<len; i++){
absA[i];
}
}
// you can use includes, for example:
#include <bits/stdc++.h>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
vector<int> check;
int solution(vector<int> &A) {
// write your code in C++14 (g++ 6.2.0)
int len = A.size();
for(int i=0; i<len; i++){
absA[i];
}
}
// you can use includes, for example:
#include <bits/stdc++.h>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
vector<int> check();
int solution(vector<int> &A) {
// write your code in C++14 (g++ 6.2.0)
int len = A.size();
for(int i=0; i<len; i++){
absA[i];
}
}
// you can use includes, for example:
#include <bits/stdc++.h>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
vector<int> le();
int solution(vector<int> &A) {
// write your code in C++14 (g++ 6.2.0)
int len = A.size();
for(int i=0; i<len; i++){
absA[i];
}
}
// you can use includes, for example:
#include <bits/stdc++.h>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
vector<int> absdistinct();
int solution(vector<int> &A) {
// write your code in C++14 (g++ 6.2.0)
int len = A.size();
for(int i=0; i<len; i++){
absA[i];
}
}
// you can use includes, for example:
#include <bits/stdc++.h>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
vector<int> absDistinct();
int solution(vector<int> &A) {
// write your code in C++14 (g++ 6.2.0)
int len = A.size();
for(int i=0; i<len; i++){
absA[i];
}
}
// you can use includes, for example:
#include <bits/stdc++.h>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
vector<int> absDistinct;
int solution(vector<int> &A) {
// write your code in C++14 (g++ 6.2.0)
int len = A.size();
for(int i=0; i<len; i++){
if()
absDistinct.push_back()
}
}
// you can use includes, for example:
#include <bits/stdc++.h>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
vector<int> absDistinct;
int solution(vector<int> &A) {
// write your code in C++14 (g++ 6.2.0)
int len = A.size();
for(int i=0; i<len; i++){
abs(A[i])
if()
absDistinct.push_back()
}
}
// you can use includes, for example:
#include <bits/stdc++.h>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
vector<int> absDistinct;
int solution(vector<int> &A) {
// write your code in C++14 (g++ 6.2.0)
int len = A.size();
for(int i=0; i<len; i++){
int abs_val= abs(A[i]);
if()
absDistinct.push_back()
}
}
// you can use includes, for example:
#include <bits/stdc++.h>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
vector<int> absDistinct;
int solution(vector<int> &A) {
// write your code in C++14 (g++ 6.2.0)
int len = A.size();
for(int i=0; i<len; i++){
int abs_val= abs(A[i]);
if(absDistinct.find())
absDistinct.push_back()
}
}
// you can use includes, for example:
#include <bits/stdc++.h>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
vector<int> absDistinct;
int solution(vector<int> &A) {
// write your code in C++14 (g++ 6.2.0)
int len = A.size();
for(int i=0; i<len; i++){
int abs_val= abs(A[i]);
if(absDistinct.find(abs_val == absDistinct.end())){
absDistinct.push_back(abs_val);
}
}
}
// you can use includes, for example:
#include <bits/stdc++.h>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
vector<int> absDistinct;
int solution(vector<int> &A) {
// write your code in C++14 (g++ 6.2.0)
int len = A.size();
for(int i=0; i<len; i++){
int abs_val= abs(A[i]);
if(absDistinct.find(abs_val == absDistinct.end())){
absDistinct.push_back(abs_val);
}
}
int res = absDistinct.size();
return res;
}
func.cpp: In function 'int solution(std::vector<int>&)': func.cpp:12:24: error: 'class std::vector<int>' has no member named 'find'; did you mean 'end'? if(absDistinct.find(abs_val == absDistinct.end())){ ^~~~ func.cpp:12:37: error: no match for 'operator==' (operand types are 'int' and 'std::vector<int>::iterator {aka __gnu_cxx::__normal_iterator<int*, std::vector<int> >}') if(absDistinct.find(abs_val == absDistinct.end())){ ~~~~~~~~^~~~~~~~~~~~~~~~~~~~ In file included from /opt/lang/gcc/include/c++/6.2.0/regex:62:0, from /opt/lang/gcc/include/c++/6.2.0/x86_64-pc-linux-gnu/bits/stdc++.h:110, from func.cpp:2: /opt/lang/gcc/include/c++/6.2.0/bits/regex.h:937:5: note: candidate: template<class _BiIter> bool std::__cxx11::operator==(const std::__cxx11::sub_match<_BiIter>&, const std::__cxx11::sub_match<_BiIter>&) operator==(const sub_match<_BiIter>& __lhs, const sub_match<_BiIter>& __rhs) ^~~~~~~~ /opt/lang/gcc/include/c++/6.2.0/bits/regex.h:937:5: note: template argument deduction/substitution failed: func.cpp:12:56: note: mismatched types 'const std::__cxx11::sub_match<_BiIter>' and 'int' if(absDistinct.find(abs_val == absDistinct.end())){ ^ In file included from /opt/lang/gcc/include/c++/6.2.0/regex:62:0, from /opt/lang/gcc/include/c++/6.2.0/x86_64-pc-linux-gnu/bits/stdc++.h:110, from func.cpp:2: /opt/lang/gcc/include/c++/6.2.0/bits/regex.h:1010:5: note: candidate: template<class _Bi_iter, class _Ch_traits, class _Ch_alloc> bool std::__cxx11::operator==(std::__cxx11::__sub_match_string<_Bi_iter, _Ch_traits, _Ch_alloc>&, const std::__cxx11::sub_match<_BiIter>&) operator==(const __sub_match_string<_Bi_iter, _Ch_traits, _Ch_alloc>& __lhs, ^~~~~~~~ /opt/lang/gcc/include/c++/6.2.0/bits/regex.h:1010:5: note: template argument deduction/substitution failed: func.cpp:12:56: note: mismatched t
// you can use includes, for example:
#include <bits/stdc++.h>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
vector<int> absDistinct;
int solution(vector<int> &A) {
// write your code in C++14 (g++ 6.2.0)
int len = A.size();
for(int i=0; i<len; i++){
int abs_val= abs(A[i]);
if(absDistinct.find(abs_val) == absDistinct.end())){
absDistinct.push_back(abs_val);
}
}
int res = absDistinct.size();
return res;
}
// you can use includes, for example:
#include <bits/stdc++.h>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
vector<int> absDistinct;
int solution(vector<int> &A) {
// write your code in C++14 (g++ 6.2.0)
int len = A.size();
for(int i=0; i<len; i++){
int abs_val= abs(A[i]);
if(absDistinct.find(abs_val) == absDistinct.end()){
absDistinct.push_back(abs_val);
}
}
int res = absDistinct.size();
return res;
}
func.cpp: In function 'int solution(std::vector<int>&)': func.cpp:12:24: error: 'class std::vector<int>' has no member named 'find'; did you mean 'end'? if(absDistinct.find(abs_val) == absDistinct.end()){ ^~~~
// you can use includes, for example:
#include <bits/stdc++.h>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
vector<int> absDistinct;
int solution(vector<int> &A) {
// write your code in C++14 (g++ 6.2.0)
int len = A.size();
for(int i=0; i<len; i++){
int abs_val= abs(A[i]);
if(find(v.begin(), v.end(), 20); == absDistinct.end()){
absDistinct.push_back(abs_val);
}
}
int res = absDistinct.size();
return res;
}
// you can use includes, for example:
#include <bits/stdc++.h>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
vector<int> absDistinct;
int solution(vector<int> &A) {
// write your code in C++14 (g++ 6.2.0)
int len = A.size();
for(int i=0; i<len; i++){
int abs_val= abs(A[i]);
if(find(absDistinct.begin(), absDistinct.end(), abs_val); == absDistinct.end()){
absDistinct.push_back(abs_val);
}
}
int res = absDistinct.size();
return res;
}
// you can use includes, for example:
#include <bits/stdc++.h>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
vector<int> absDistinct;
int solution(vector<int> &A) {
// write your code in C++14 (g++ 6.2.0)
int len = A.size();
for(int i=0; i<len; i++){
int abs_val= abs(A[i]);
if(find(absDistinct.begin(), absDistinct.end(), abs_val) == absDistinct.end()){
absDistinct.push_back(abs_val);
}
}
int res = absDistinct.size();
return res;
}
// you can use includes, for example:
#include <bits/stdc++.h>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
vector<int> absDistinct;
int solution(vector<int> &A) {
// write your code in C++14 (g++ 6.2.0)
int len = A.size();
for(int i=0; i<len; i++){
int abs_val= abs(A[i]);
if(find(absDistinct.begin(), absDistinct.end(), abs_val) == absDistinct.end()){
absDistinct.push_back(abs_val);
}
}
int res = absDistinct.size();
return res;
}
// you can use includes, for example:
#include <bits/stdc++.h>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
vector<int> absDistinct;
int solution(vector<int> &A) {
// write your code in C++14 (g++ 6.2.0)
int len = A.size();
for(int i=0; i<len; i++){
int abs_val= abs(A[i]);
if(find(absDistinct.begin(), absDistinct.end(), abs_val) == absDistinct.end()){
absDistinct.push_back(abs_val);
}
}
int res = absDistinct.size();
return res;
}
The following issues have been detected: timeout errors.