diff --git a/challenge_2/cpp/rbrt/README.md b/challenge_2/cpp/rbrt/README.md new file mode 100644 index 000000000..17739d01b --- /dev/null +++ b/challenge_2/cpp/rbrt/README.md @@ -0,0 +1,2 @@ +The code accept an array of any length and find the values that appear only once in the array. The complexity of the code is linear with the length of the array. +In order to obtain this result we generate an array where we stored how many time the values in the input array appear. In this way it is necessary to scan the list only once and update the values in the counters array. Once the reading of the input list is over we look for the element that appear only once, i.e. the one that has the counter == 1. \ No newline at end of file diff --git a/challenge_2/cpp/rbrt/src/[C++]Challenge2.cpp b/challenge_2/cpp/rbrt/src/[C++]Challenge2.cpp new file mode 100644 index 000000000..9eb216e12 --- /dev/null +++ b/challenge_2/cpp/rbrt/src/[C++]Challenge2.cpp @@ -0,0 +1,68 @@ +#include + + +using namespace std; + +int find_max(int x[],int n); //This function read an array and give as output the maximum element in the array + +int main() +{ + int n; + + //we accept as input a value representing the length of the array + //and we create an array of the given size + + + cout<< "Please insert the array length >> "; + cin>>n; + int *x=new int[n+1]; + + cout<< "Please insert an array >> "; + + for(int i=0;i>x[i]; + } + + + //we generate an array from 1 to the maximum value present in the input array. + //Every time that a value occurs in the input array we increment it in ctrl + //To know which element occur once is sufficient to find the value in ctrl==1 + + int *ctrl=new int[find_max(x,n)+1]; + + for(int i=0;i