
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Maximum in an array that can make another array sorted in C++
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
// 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]<<" "; } }
Output
Maximum in arr2[] to make arr1[] sorted:9 Arr1[]:1 3 7 9 10 Arr2[]:2 1 6 8 9
- Related Articles
- Maximum number of partitions that can be sorted individually to make sorted in C++
- Make an array of another array's duplicate values in JavaScript
- 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++
- Check if reversing a sub array make the array sorted in Python
- Maximum element in a sorted and rotated array in C++
- Can i refer an element of one array from another array in java?
- C++ Permutation of an Array that has Smaller Values from Another Array
- Sort an array according to another array in JavaScript
- JavaScript in filter an associative array with another array
- Program to find max chunks to make array sorted in Python
- Elements of an array that are not divisible by any element of another array in C++
- C++ code to count operations to make array sorted
- Find the element that appears once in sorted array - JavaScript
- Maximum array sum that can be obtained after exactly k changes in C++
