How to find the product of the elements of the array, which are located between the maximum and minimum elements?

Closed due to the fact that the question is too common for participants Vlad from Moscow , Denis , aleksandr barakin , Harry , Kromster 30 Nov '16 at 3:58 .

Please correct the question so that it describes the specific problem with sufficient detail to determine the appropriate answer. Do not ask a few questions at once. See β€œHow to ask a good question?” For clarification. If the question can be reformulated according to the rules set out in the certificate , edit it .

  • And if all elements are equal? And if there are a lot of minimal elements? - VladD
  • @VladD, this is the second stage, do not rush, we will see these questions with you)))) - isnullxbh
  • @VladD can immediately link to questions about the long multiplication of numbers? And it does not fit in __ int128 - pavel
  • one
    @pavel: Hm. We need the product from the minimum to the current element, the current product from the current minimum to the current maximum, and the current minimum and current maximum. It seems enough. If so, you can write quite readable. - VladD
  • 6
    @Dasha Novikova Maximum and minimum elements can be many. Moreover, the range between the maximum and minimum elements may differ from the range between the minimum and maximum elements. Therefore, specify the conditions of the problem. For example, a task might sound like this: to find the product of elements in the range from the first minimum to the last maximum element, or in some other way. - Vlad from Moscow

4 answers 4

The solution is not the best, not the best, but try:

int data[] = { 2, 4, 5, 2, -3, 5, 2, 33, 5, 3, -45, 2 }; std::pair<int *, int *> pr = std::minmax_element( std::begin( data ), std::end( data ) ); if( std::distance( std::begin( data ), pr.second ) < std::distance( std::begin( data ), pr.first ) ) std::swap( pr.first, pr.second ); if( pr.first != pr.second && ++pr.first != pr.second ) { int res = std::accumulate( pr.first, pr.second, 1, std::multiplies<int>() ); } 
  • if (pr.second < pr.first) , not? - Qwertiy ♦
  • @Qwertiy, there is a prefix operator ++, it’s not just there) - isnullxbh
  • I actually wanted to replace this: if( std::distance( std::begin( data ), pr.second ) < std::distance( std::begin( data ), pr.first ) ) , not second condition. - Qwertiy ♦
  • However, in the second you can also if(++pr.first < pr.second) ? - Qwertiy ♦
  • @Qwertiy, I apologize, did not understand you correctly. Perhaps it will be acceptable here, I just don’t even know, is it ok to compare the addresses? - isnullxbh

My decision raises other questions, but nonetheless.

 #include "stdafx.h" #include <iostream> #include <conio.h> int main() { int n = 0; std::cout << "n = "; std::cin >> n; int* mass = new int[n]; for (int i(0); i < n; i++) std::cin >> mass[i]; int max = 0, min = 0; for (int i(0); i < n; i++) { if (i == 0 && mass[i] == mass[max]) max = i; else if (mass[i]> mass[max]) max = i;// ΠΈΡ‰Π΅ΠΌ Π½ΠΎΠΌΠ΅Ρ€ максимального элСмСнта if (i == 0 && mass[i] == mass[min]) min = i; else if (mass[i] < mass[min]) min = i;// ΠΈΡ‰Π΅ΠΌ Π½ΠΎΠΌΠ΅Ρ€ минимального элСмСнта } std::cout << "min = " << min << std::endl; std::cout << "max = " << max << std::endl; int proizvedenie = 1; if (max < min) { for (int i = max + 1; i < min; i++) proizvedenie *= mass[i];// ΠΏΡ€ΠΎΠΈΠ·Π²ΠΎΠ΄ΠΈΠΌ ΡƒΠΌΠ½ΠΎΠΆΠ΅Π½ΠΈΠ΅ Π² Π·Π°Π΄Π°Π½Π½ΠΎΠΌ Π΄ΠΈΠ°ΠΏΠ°Π·ΠΎΠ½Π΅ } else if (min < max) { for (int i = min + 1; i < max; i++) proizvedenie *= mass[i]; } std::cout << proizvedenie; _getch(); return 0; } 

Works

  • if you first write the maximum element, it does not count - Dasha Novikova
  • @DashaNovikova, try to play with the values, for example, set max = n-1; instead of zero. Experiment. - Semyon Shelukhin
  • @DashaNovikova, with minor changes. - Semyon Shelukhin

Calculations for "all occasions" :)

The code searches for one and more than one result, if any. After the search - there are examples of sorting the results according to various criteria.

Online version here - http://ideone.com/4CGmoo

 #include <algorithm> #include <iostream> #include <vector> ////////////////////////////////////////////////////////////////// // Π˜Ρ‰Π΅ΠΌ ΠΏΡ€ΠΎΠΈΠ·Π²Π΅Π΄Π΅Π½ΠΈΠ΅ элСмСнтов массива, ΠΊΠΎΡ‚ΠΎΡ€Ρ‹Π΅ располоТСны ΠΌΠ΅ΠΆΠ΄Ρƒ // ΠΌΠ°ΠΊΡΠΈΠΌΠ°Π»ΡŒΠ½Ρ‹ΠΌ ΠΈ ΠΌΠΈΠ½ΠΈΠΌΠ°Π»ΡŒΠ½Ρ‹ΠΌ элСмСнтами // // Π”ΠΎΠΏΠΎΠ»Π½ΠΈΡ‚Π΅Π»ΡŒΠ½ΠΎ (Ρ‡Π΅Π³ΠΎ Π½Π΅ ΡƒΡ‚ΠΎΡ‡Π½Π΅Π½ΠΎ Π² Π·Π°Π΄Π°Π½ΠΈΠΈ): // // 1. НС ΠΈΡ‰Π΅ΠΌ Π² ΠΎΠ±Ρ€Π°Ρ‚Π½ΠΎΠΌ порядкС (ΠΎΡ‚ минимального ΠΊ ΠΌΠ°ΠΊΡΠΈΠΌΠ°Π»ΡŒΠ½ΠΎΠΌΡƒ) // 2. Π’ΠΎΠ·ΠΌΠΎΠΆΠ½Ρ‹Π΅ Π²Π°Ρ€ΠΈΠ°Π½Ρ‚Ρ‹ заносим Π² Π²Π΅ΠΊΡ‚ΠΎΡ€ Ρ€Π΅Π·ΡƒΠ»ΡŒΡ‚Π°Ρ‚ΠΎΠ² ΠΈ ΠΈΡ… // разновсячСски сортируСм Π² порядкС Π½ΡƒΠΆΠ½ΠΎΠ³ΠΎ ////////////////////////////////////////////////////////////////// typedef std::pair<std::size_t,std::size_t> PairType; typedef std::vector<PairType> ResType; void FuncMul(std::vector<int> Vec, ResType &Res) { // Π² Π²Π΅ΠΊΡ‚ΠΎΡ€Π΅ Π΄Π»ΠΈΠ½ΠΎΠΉ мСньшС 3-Ρ… ΠΈΡΠΊΠ°Ρ‚ΡŒ Π½Π΅Ρ‡Π΅Π³ΠΎ if (Vec.size()<3) throw std::runtime_error("Π½ΠΈΡ‡Π΅Π³ΠΎ Π½Π΅ Π½Π°ΠΉΠ΄Π΅Π½ΠΎ"); // опрСдСляСмся с ΠΌΠΈΠ½ΠΈΠΌΡƒΠΌΠ°ΠΌΠΈ ΠΈ максимумами auto Min = *std::min_element(Vec.begin(),Vec.end()); auto Max = *std::max_element(Vec.begin(),Vec.end()); // ΠΈΡ‰Π΅ΠΌ всС ΠΏΠ°Ρ€Ρ‹ ΠΌΠ°Ρ…-min std::size_t I = 0, S = Vec.size(), X; int M = 0; while(I<S) { if (M == 0) { if (Vec[I]==Max) { M=1; X=I; } } else { if (Vec[I]==Min) { if (I>X+1) Res.push_back({X,I}); M=0; } else if (Vec[I]==Max) X=I; } I++; } if(Res.size()<1) throw std::runtime_error("Π½ΠΈΡ‡Π΅Π³ΠΎ Π½Π΅ Π½Π°ΠΉΠ΄Π΅Π½ΠΎ"); } int main() { std::vector<int> Vec = {3,-4,1,3,4,9,1,-4,9,9,2,-3,-4,9,1}; ResType Res; try { FuncMul(Vec,Res); // лямбда Π²Ρ‹Π²ΠΎΠ΄Π° Π½Π° ΠΏΠ΅Ρ‡Π°Ρ‚ΡŒ auto PrnPair = [&](const PairType &P) { std::cout << P.first << ":" << P.second << " = " << std::accumulate(Vec.begin()+P.first+1, Vec.begin()+P.second, 1, std::multiplies<int>()) << std::endl; }; // лямбда сортировки ΠΏΠΎ Π²Π΅Π»ΠΈΡ‡ΠΈΠ½Π΅ произвСдСния auto SortMul = [&] (const PairType &P1, const PairType &P2) { return std::accumulate(Vec.begin()+P1.first+1, Vec.begin()+P1.second, 1, std::multiplies<int>()) < std::accumulate(Vec.begin()+P2.first+1, Vec.begin()+P2.second, 1, std::multiplies<int>()); }; // лямбда сортировки ΠΏΠΎ количСству ΠΌΠ½ΠΎΠΆΠΈΠΌΡ‹Ρ… auto SortDis = [&] (const PairType &P1, const PairType &P2) { return P1.second - P1.first < P2.second-P2.first; }; // 1) ΠΏΠ΅Ρ‡Π°Ρ‚ΡŒ "сырого" нСсортированного Ρ€Π΅Π·ΡƒΠ»ΡŒΡ‚Π°Ρ‚Π° -------------------------------------------- for(const auto &i:Res) PrnPair(i); std::cout << "=========================" << std::endl; // 2) ΠΏΠ΅Ρ‡Π°Ρ‚ΡŒ отсортированного Ρ€Π΅Π·ΡƒΠ»ΡŒΡ‚Π°Ρ‚Π° ΠΏΠΎ Π²ΠΎΠ·Ρ€Π°ΡΡ‚Π°Π½ΠΈΡŽ Π²Π΅Π»ΠΈΡ‡ΠΈΠ½Ρ‹ произвСдСния ---------------- std::sort(Res.begin(),Res.end(),SortMul); for(const auto &i:Res) PrnPair(i); std::cout << "=========================" << std::endl; // 3) ΠΏΠ΅Ρ‡Π°Ρ‚ΡŒ отсортированного Ρ€Π΅Π·ΡƒΠ»ΡŒΡ‚Π°Ρ‚Π° ΠΏΠΎ ΡƒΠ±Ρ‹Π²Π°Π½ΠΈΡŽ количСства элСмСнтов Π² ΠΏΡ€ΠΎΠΈΠ·Π²Π΅Π΄Π΅Π½ΠΈΠΈ ---- std::sort(Res.begin(),Res.end(),SortDis); for(const auto &i:Res) PrnPair(i); std::cout << "=========================" << std::endl; } catch(std::runtime_error &E) { std::cout << "Ошибка: " << E.what() << std::endl; } catch(...) { std::cout << "Ошибка: Ρ‡Ρ‚ΠΎ-Ρ‚ΠΎ пошло Π½Π΅ Ρ‚Π°ΠΊ!" << std::endl; } return 0; } 
     #include <algorithm> #include <iostream> #include <vector> int FuncMul(std::vector<int> Vec) { if (Vec.size()>2) { std::sort(Vec.begin(), Vec.end(), std::less<int>()); return std::accumulate(Vec.begin()+1, Vec.end()-1, 1, std::multiplies<int>()); } return 0; } int main() { std::vector<int> Vec = {9,1,2,3,4,1,1,-4}; std::cout << "FuncMul: " << FuncMul(Vec); return 0; } 
    • "which are located between" - sorting does not match the condition. - Qwertiy ♦
    • nothing has been said about sorting, and if it is impossible, then it is possible - Majestio
    • @Majestio, are you serious? It is necessary to find the value for the current order, and not for the one that will turn out after sorting. Therefore, no sorting is out of the question. - isnullxbh
    • Then indicate this in the question that order is important. - Majestio
    • @Majestio, understand, the emphasis is on finding the same minimal and maximal element in the input set - however, different variants of their location are possible, which complicates the task. And so, it becomes much more primitive. - isnullxbh