Close pair

A pair of x values in the string on a card is a y number that is part of the same string and has the following property:
- if x is in an even position, y is the minimum of numbers greater than x that are to the left of x in a vector
-if x is in odd position, y is the minimum of numbers greater than x that are to the right of x in a vector

The program will display on the screen a string of n numbers, representing the string of pairs corresponding to each x element in the string. If there is a number x that is not such a pair in the string, the value -1 will be displayed instead of its pair.
0 < n ≤ 100
0 ≤ x ≤ 1000

Input:
7
24 248 456 0 35 74 554
Output:
35 -1 554 24 74 248 -1


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
  #include <iostream>
using namespace std;

int main() {
    int n, i, v[102];
    cin >> n;
    for (i = 1; i <= n; i++) {
        cin >> v[i];
    }
 //   for (i = 1; i <=n; i++) {
        if (i % 2 == 0) {
           
        }
        if (i % 2 == 1) {
            
        }
    
    return 0;
}
Sadly, "even" and "odd" don't reflect array indexing from 0.

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
29
30
31
32
33
#include <iostream>
using namespace std;

int search( int A[], int value, int first, int last )
{
   bool found = false;
   int mn = -1;
   for ( int i = first; i <= last; i++ )
   {
      if ( A[i] > value )
      {
         if ( !found ) mn = A[i];
         else if ( A[i] < mn ) mn = A[i];
         found = true;
      }
   }
   return mn;
}


int main()
{
   const int SIZE = 1000;
   int v[SIZE], n;
   cin >> n;
   for ( int i = 0; i < n; i++ ) cin >> v[i];

   for ( int i = 0; i < n; i++ )
   {
      if ( i % 2 ) cout << search( v, v[i], 0    , i - 1 ) << ' ';   // Ahem, EVEN in OP's notation!!!
      else         cout << search( v, v[i], i + 1, n - 1 ) << ' ';
   }
}
Last edited on
Topic archived. No new replies allowed.