64 542
Assignments Done
99,3%
Successfully Done
In September 2018

Answer to Question #27751 in C++ for bharat

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;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

Assignment Expert
17.06.13, 14:57

Thanks for your appreciation! We are glad to be helpful.
If you really liked our service please press like-button beside answer field. Thank you!

Rishi
17.06.13, 01:36

Excellent answer

Leave a comment

Ask Your question

Submit
Privacy policy Terms and Conditions