Question #27751

Integer V lies strictly between integers U and W if U < V < W or if U > V > W.
A non-empty zero-indexed array A consisting of N integers is given. A pair of indices (P, Q) where 0 ≤ P < Q < N is said to have adjacent values if no value in the array lies strictly between values A[P] and A[Q].
For example, in array A such that
A[0] = 0 A[1] = 3 A[2] = 3
A[3] = 7 A[4] = 5 A[5] = 3
A[6] = 11 A[7] = 1
the following pairs of indices have adjacent values:
(0, 7), (1, 2), (1, 4),
(1, 5), (1, 7), (2, 4),
(2, 5), (2, 7), (3, 4),
(3, 6), (4, 5), (5, 7).

Expert's answer

#include <stdio.h>

#include <cstdlib>

#include <iostream>

using namespace std;

int main ()

{

int n;

scanf("%d", &n);

int *a = new int[n];

for (int i = 0; i < n; i++)

& scanf("%d", &a[i]);

for (int i = 0; i < n; i++) {

& int low_bound = i;

& for (int j = 0; j < n; j++) {

if (a[j] > a[i] && (low_bound == i || a[low_bound] > a[j])) {

low_bound = j;

}

& }

& if (low_bound != i) {

for (int j = 0; j < n; j++) {

if (a[j] == a[low_bound] || (a[j] == a[i] && i < j)) {

& printf("(%d, %d)\n", min(i, j), max(i, j));

}

}

& }

}

return 0;

}

#include <cstdlib>

#include <iostream>

using namespace std;

int main ()

{

int n;

scanf("%d", &n);

int *a = new int[n];

for (int i = 0; i < n; i++)

& scanf("%d", &a[i]);

for (int i = 0; i < n; i++) {

& int low_bound = i;

& for (int j = 0; j < n; j++) {

if (a[j] > a[i] && (low_bound == i || a[low_bound] > a[j])) {

low_bound = j;

}

& }

& if (low_bound != i) {

for (int j = 0; j < n; j++) {

if (a[j] == a[low_bound] || (a[j] == a[i] && i < j)) {

& printf("(%d, %d)\n", min(i, j), max(i, j));

}

}

& }

}

return 0;

}

## Comments

Assignment Expert06.11.18, 14:55Dear POORNACHANDRAN S

Questions in this section are answered for free. We can't fulfill them all and there

is no guarantee of answering certain question but we are doing our best. And if answer

is published it means it was attentively checked by experts. You can try it yourself by

publishing your question. Although if you have serious assignment that requires large amount

of work and hence cannot be done for free you can submit it as assignment and our

experts will surely assist you.

POORNACHANDRAN S05.11.18, 17:41CAN U GIVE ME THE C CODE FOR THE SAME QUESTION

Assignment Expert17.06.13, 15:57Thanks for your appreciation! We are glad to be helpful.

If you really liked our service please press like-button beside answer field. Thank you!

Rishi17.06.13, 02:36Excellent answer

## Leave a comment