- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

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.

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

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.

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

No such element.

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

Arr1[] cannot be sorted.

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[].

// 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[0]); int len2 = sizeof(arr2) / sizeof(arr2[0]); 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]<<" "; } }

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

- Related Questions & Answers
- Maximum number of partitions that can be sorted individually to make sorted in C++
- Maximum adjacent difference in an array in its sorted form in C++
- Maximum possible XOR of every element in an array with another array in C++
- C++ Permutation of an Array that has Smaller Values from Another Array
- Make an array of another array's duplicate values in JavaScript
- Maximum element in a sorted and rotated array in C++
- C++ code to count operations to make array sorted
- Elements of an array that are not divisible by any element of another array in C++
- Check if reversing a sub array make the array sorted in Python
- Print all triplets in sorted array that form AP in C++
- Maximum array sum that can be obtained after exactly k changes in C++
- Can i refer an element of one array from another array in java?
- Maximum equlibrium sum in an array in C++
- Sort an array according to another array in JavaScript
- JavaScript in filter an associative array with another array

Advertisements