# Maximum in an array that can make another array sorted in C++

C++Server Side ProgrammingProgramming

#### C in Depth: The Complete C Programming Guide for Beginners

45 Lectures 4.5 hours

#### Practical C++: Learn C++ Basics Step by Step

Most Popular

50 Lectures 4.5 hours

#### Master C and Embedded C Programming- Learn as you go

66 Lectures 5.5 hours

We are given an array of numbers say Arr1[] and another array Arr2[] of same or different length. The Arr1[] has elements sorted in ascending order, such that a single element is unsorted. We must find the element from the second array Arr2[] such that it can replace the wrongly placed element of Arr1[] and make it sorted. Also, the element chosen from Arr2[] should be maximum if there are multiple options available.

## Input

Arr1[]= { 1,3,5,7,2,11 }, Arr2[]= { 4,8,7,10,9 }

## Output

Maximum element that can make Arr1 sorted: 10

Explanation − Numbers in Arr2[] that can make Arr1[] sorted − 8,9,10 as they all are >=7 and <=11. Out of these 10 is maximum.

New Arr1[] becomes { 1,3,5,7,10,11 } and is sorted.

## Input

Arr1[]= { 12,5,22,17 }, Arr2[]= { 4,8,7,10,9 }

## Output

No such element.

Explanation − No umbers in Arr2[] that are >=12 and <=22.

Arr1[] cannot be sorted.

## Approach used in the below program is as follows

• Arrays arr1[] and arr2[] are used to store numbers. Arr1[] is sorted in ascending order except one element.

• Function sortMax( int arr1[], int arr2[], int n1, int n2 ) takes both arrays and their length and updates arr1[] if element in arr2[] is found that is maximum and can also replace incorrect element in arr[]

• Store index of wrong placed element in arr1[] in variable wpos, initially -1.

• maxx is used to store maximum among those elements of arr2[] that can replace wrongly placed element of arr1[] and make it sorted.

• At first travers arr1[] and find incorrect element such. If arr[i]<arr[i-1]. Store index in wpos as i.

• Now travers arr2[] and find elements that can be placed between arr1[wpos-1] and arr[wpos+1]. Also if such an element exists then compare it with maxx such element.

• Update maximum.

• In the end replace arr1[wpos] with maxx.

• If such an element is found return maxx, else return -1.

• If no element is found display a message.

• Else print sorted arr1[].

## Example

Live Demo

// C++ program to make array sorted
#include <bits/stdc++.h>
using namespace std;
int sortMax(int arr1[], int arr2[], int n1, int n2) //making arr1 sorted{
int wpos=-1;
int maxx=-1;
int i,j;
for(i=0;i<n1;i++)
if(arr1[i]<arr1[i-1])
wpos=i;
for(j=0;j<n2;j++){
if(arr2[j]>=arr1[wpos-1] && arr2[j]<=arr1[wpos+1])
if(arr2[j]>=maxx)
maxx=arr2[j];
}
if(maxx!=-1)
arr1[wpos]=maxx;
return maxx;
}
int main(){
int arr1[] = { 1, 3, 7, 4, 10 };
int arr2[] = { 2, 1, 6, 8, 9 };
int len1 = sizeof(arr1) / sizeof(arr1);
int len2 = sizeof(arr2) / sizeof(arr2);
int res=sortMax(arr1, arr2, len1, len2);
if(res==-1)
cout<<"No swap possible! No such element!";
else{
cout<<"Maximum in arr2[] to make arr1[] sorted:"<<res;
cout<<endl<<"Arr1[]:";
for(int i=0;i<len1;i++)
cout<<arr1[i]<<" ";
cout<<endl<<"Arr2[]:";
for(int i=0;i<len2;i++)
cout<<arr2[i]<<" ";
}
}

## Output

Maximum in arr2[] to make arr1[] sorted:9
Arr1[]:1 3 7 9 10
Arr2[]:2 1 6 8 9