Divide the array into two parts, placing in the first elements, more than the arithmetic average of their sum, and the second smaller. Help me please.

Closed due to the fact that user207618, αλεχολυτ , dirkgntly , aleksandr barakin , Vladimir Gamalyan 28 aug '16 at 9:25 am off topic .

  • Most likely, this question does not correspond to the subject of Stack Overflow in Russian, according to the rules described in the certificate .
If the question can be reformulated according to the rules set out in the certificate , edit it .

  • How can I help you? - Vladimir Gamalyan
  • @VladimirGamalian To help write an algorithm, there are simply no options at all how to do it ( - t16bz
  • @ t16bz "arithmetic mean of the sum" - this is how it is, eh? The "sum of elements" is one number. The arithmetic average of a tuple of one number is the number itself. In short, the compiler of the assignment in mathematics understands no better than their students. - gbg
  • You can first calculate the arithmetic average, then in the new array, first copy those elements that are larger than the resulting number, then the remaining ones. - Vladimir Gamalyan
  • 6
    I vote for closing this question as not relevant, because SO is not a freelance exchange. - user207618

2 answers 2

If the task is to get a new array, in which the elements first go more than the arithmetic average, then the rest, then it is enough to sort the array in descending order:

#include <algorithm> int main() { int a[8] = { 32, 71, 12, 45, 26, 80, 53, 33 }; std::sort(a, a + 8, std::greater<int>()); } 

In the result, in the array a first there will be elements greater than the arithmetic mean.

Working example

  • Sorry, I forgot to write that the task says that the parts are not sorted. I think your example just suits it ( - t16bz
  • @ t16bz then something like this repl.it/Cx36/5 - Vladimir Gamalyan
  • complexity O (n log (n)) where necessary O (n)? = \ - yrHeTaTeJlb
  • @yrHeTaTeJlb yes, you are right, excessive complexity. - Vladimir Gamalyan

This is exactly what the std::partition , or std::stable_partition . The second algorithm preserves the relative order of the elements. Usage looks like this:

 std::partition(&arr[0], &arr[size], std::bind1st(std::greater<int>(), mean)); 

or you can, if your compiler supports C ++ 11:

 std::partition(&arr[0], &arr[size], std::bind(std::greater<int>(), mean, _1)); 

All code looks like this:

 #include <iostream> #include <algorithm> #include <numeric> int main() { int arr[] = {32, 21, 34, 44, 17, 26, 7, 16}; int size = sizeof(arr) / sizeof(int); int summ = std::accumulate(&arr[0], &arr[size], 0); int mean = summ / size; std::stable_partition(&arr[0], &arr[size], std::bind1st(std::less<int>(), mean)); std::cout << mean << std::endl; for(int i = 0; i < size; ++i){ std::cout << arr[i] << " "; } } 

http://cpp.sh/5tyr

  • Beautiful decision. Only it seems that elements less than average will appear at the beginning. - Vladimir Gamalyan
  • @VladimirGamalian, and really, did not carefully read the task. Replaced std::greater with std::less - yrHeTaTeJlb
  • It seems there in the comments the author asked that the parts remain in the same order. - Vladimir Gamalyan
  • @VladimirGamalian, no problem, replaced std::partition with std::stable_partition - yrHeTaTeJlb
  • For negative numbers may give an error. - Vladimir Gamalyan