Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 52 additions & 1 deletion 21_MeanMode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,57 @@
#include <iostream>
using namespace std;

//we can solve it using another method
/*

bool sortByVal(const pair<int, int> &a,
const pair<int, int> &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<int, int> m;
for(int i = 0; i < n; i++)
m[A[i]]++;


map<int, int>::iterator itr;


//push it into the vector for sorting elements
vector<pair<int, int>> 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) {

Expand Down Expand Up @@ -67,4 +118,4 @@ int main() {
cout << MeanMode(C, sizeof(C) / sizeof(C[0])) << endl;
return 0;

}
}
28 changes: 27 additions & 1 deletion 27_AdditivePeristence.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,34 @@
#include <iostream>
#include <string>
#include <sstream>
#include <bits/stdc++.h>
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;
Expand Down Expand Up @@ -51,4 +77,4 @@ int main() {
cout << AdditivePersistence(199) << endl; // 3
return 0;

}
}