Consider an integer array, the number of elements in which is determined by the user. The
elements are also taken as input from the user. Write a program to find those pairs of elements
that have the maximum and minimum difference among all element pairs in java
Answers
Consider an integer array, the number of elements in which is determined by the user. The elements are also taken as input from the user. Write a program to find the pair of elements that has the maximum difference among all element pairs.
Answer:
#include<iostream>
using namespace std;
int main(){
int *arr;
int size;
cout<<"Enter the size of array "<<endl;
cin>>size;
arr=new int[size];
cout<<"Enter the elements"<<endl;
for(int i=0;i<size;i++){
cin>>arr[i];
}
int max=arr[0],min=arr[0];
for(int i=0;i<size;i++){
if(arr[i]>max){
max=arr[i];
}
if(arr[i]<min){
min=arr[i];
}
}
cout<<"The pair that has the maximum difference is "<<max<<" "<<min<<endl;
}