Number of unique elements using c++ in o(n):
/*author => shivamzaz
level=> ad-hoc
complexity=> o(n) */
#include "bits/stdc++.h"
using namespace std;
int main(){
int a[]={2,2,3,2},cnt=0;
bool b[4]={0};
for(int i=0;i<4;i++){
b[a[i]]=1;
}
for(int i=0;i<4;i++){
if(b[i]==1){
cnt++;
}
}
printf("%d\n",cnt);
return 0;
}
/*author => shivamzaz
level=> ad-hoc
complexity=> o(n) */
#include "bits/stdc++.h"
using namespace std;
int main(){
int a[]={2,2,3,2},cnt=0;
bool b[4]={0};
for(int i=0;i<4;i++){
b[a[i]]=1;
}
for(int i=0;i<4;i++){
if(b[i]==1){
cnt++;
}
}
printf("%d\n",cnt);
return 0;
}
No comments:
Post a Comment