diff --git a/21_MeanMode.cpp b/21_MeanMode.cpp index 9a00ab1..5aa7286 100644 --- a/21_MeanMode.cpp +++ b/21_MeanMode.cpp @@ -4,6 +4,57 @@ #include using namespace std; +//we can solve it using another method +/* + +bool sortByVal(const pair &a, + const pair &b) +{ + return (a.second < b.second); +} + +int MeanMode(int A[], int n){ + + + //finding mean of given array + int sum = 0; + for(int i = 0; i < n; i++) + sum += A[i]; + + int mean = sum/n; + + + //creating a map to finding frequencies of given elements + //or to find mode of given array. + map m; + for(int i = 0; i < n; i++) + m[A[i]]++; + + + map::iterator itr; + + + //push it into the vector for sorting elements + vector> vec; + for(itr = m.begin(); itr != m.end(); itr++) + vec.push_back(make_pair(itr->first, itr->second)); + + + //sorting vector using second value of pair of vector. + sort(vec.begin(), vec.end(), sortByVal); + int mode = vec.back().first; + + + //checking results if mean == mode ? return 1 : return 0; + if(mean == mode) + return 1; + else + return 0; + +} + +*/ + int MeanMode(int arr[], int size) { @@ -67,4 +118,4 @@ int main() { cout << MeanMode(C, sizeof(C) / sizeof(C[0])) << endl; return 0; -} \ No newline at end of file +} diff --git a/27_AdditivePeristence.cpp b/27_AdditivePeristence.cpp index 706dfa2..760992c 100644 --- a/27_AdditivePeristence.cpp +++ b/27_AdditivePeristence.cpp @@ -4,8 +4,34 @@ #include #include #include +#include using namespace std; +/* + +//Recursive method to solve this problem +int AdditivePersistence(int n){ + static int count = 0; + //Checking if number n > 9 i.e, so that recursivly + //we can find sum of it's digits + if(n > 9){ + int sum = 0; + while(n > 0){ + sum += n%10; + n = n/10; + } + //this will combine result + count++; + return AdditivePersistence(sum); + } + else + //similar to base case + return count; + +} + +*/ + int AdditivePersistence(int num) { int count = 0; @@ -51,4 +77,4 @@ int main() { cout << AdditivePersistence(199) << endl; // 3 return 0; -} \ No newline at end of file +}