Finding the smallest value in two arrays related

Hello everyone,
I have a small question. I have two vectors as follows:
A=[3 0 0 7]
B=[5 3 8 9]

I need to find the index of the smallest values of A which are 0,1 which correspond to the value 0.
Then, I need to choose the index amoung these indexes which corresponds to the smallest value in B ie, I need to choose index 0 between 0 and 1, which corresponds to smallest value ,3, in vector B. The problem I face, I do not want to use push_back to save the indexes of A since it is costly. How can I achieve this task in an easiest and least costly way ?
Thank you so much

A=[3 2 7 2]

The smallest value in A is apparently 2. The value 2 appears at indices 1 and 3.
B=[5 4 8 9]

Indices 1 and 3 in B contain values 4 and 9. The smallest of them is 4.

The push_back(), specifically vector::push_back() may be costly if vector has to reallocate. It is possible to preallocate, which ensures that push_back will not reallocate. Hence that cost is not so real.

However,
auto aMin = *std::min_element( begin(A), end(A) );
FOR each index i
  IF Ai == aMin
  THEN consider Bi for bMin
Let's clarify some things: You're asking for the most efficient way to find the smallest corresponding value between two vectors. Is it the value that you need or do you specifically need the index of that smallest value?

I'm assuming both the vectors may be any length in this problem.

How do you feel about sorting the vectors? Or does the index of the values have to remain unchanged?

Edit: nevermind, thanks kbw.
Last edited on
If you sort the vectors, you'll change the index of the item that has zero. You have to search the existing container as it is.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
#include <vector>
using namespace std;

int minmin( const vector<int> &A, const vector<int> &B )
{
   int minA = A[0], minB = B[0];
   for ( int i = 1; i < A.size(); i++ )
   {
      if ( A[i] == minA )
      {
         if ( B[i] < minB ) minB = B[i];
      }
      else if ( A[i] < minA )
      {
         minA = A[i];
         minB = B[i];
      }
   }
   return minB;
}

int main()
{
   vector<int> A = { 3, 0, 0, 7 };
   vector<int> B = { 5, 3, 8, 9 };
   cout << minmin( A, B ) << '\n';
}




A=[3 0 0 7]
B=[5 3 8 9]

I need to find the index of the smallest values of A which are 0,1

Nonsense: those indices would be 1 and 2.

@learner999, please make sure that your question is correct.
Last edited on
If all you need is the final min value and not the intermediate array of indices, then you don't need to store these. So possibly:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <iterator>

int main()
{
	constexpr int A[] {3, 0, 0, 7};
	constexpr int B[] {5, 3, 8, 9};

	static_assert(std::size(B) >= std::size(A));

	auto amin {A[0]}, bmin {B[0]};

	for (size_t i = 1; i < std::size(A); ++i) {
		if (A[i] < amin) {
			amin = A[i];
			bmin = B[i];
		} else
			if (A[i] == amin)
				if (B[i] < bmin)
					bmin = B[i];
	}

	std::cout << bmin << '\n';
}

Thank you so much everyone . Thank you so much @lastchance @seeplus for their detailed answer. I understood it well and applied . Thank you soo much
Topic archived. No new replies allowed.