Skip to content

Instantly share code, notes, and snippets.

@sunny1304
Created February 3, 2014 06:22
Show Gist options
  • Save sunny1304/8779590 to your computer and use it in GitHub Desktop.
Save sunny1304/8779590 to your computer and use it in GitHub Desktop.
finding_deviation C++11
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int find_deviation(vector<int> &, int);
int main(int argc, char const *argv[])
{
vector<int> given_array{1,2,3,4,5,6,7,8};
cout << find_deviation(given_array,3) << endl;
return 0;
}
int find_deviation(vector<int>&v, int d)
{
vector<int> medians;
int start = 0;
int loop_time = v.size()/d ;
for (int i=0; i<loop_time; i++)
{
vector<int> temp;
int end= start+3;
for (int i = start; i< end ; i++)
{
temp.push_back(v[i]);
vector<int>::iterator max = max_element(temp.begin(), temp.end());
vector<int>::iterator min = min_element(temp.begin(), temp.end());
medians.push_back(max - min);
}
}
vector<int>::iterator deviation = max_element(medians.begin(), medians.end());
return distance(medians.begin(),deviation);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment