Maximum in an array that can make another array sorted Last Updated : 17 Aug, 2022 Comments Improve Suggest changes Like Article Like Report Given two arrays among which one is almost sorted with one element being in the wrong position making the array unsorted, the task is to swap that element with the maximum element from the second array which can be used to make the first array sorted. Examples: Input: arr1 = {1, 3, 7, 4, 10}, arr2 = {2, 1, 5, 8, 9} Output: 1 3 7 9 10 Swap 4 with 9. Input: arr1 = {20, 1, 23}, arr2 = {50, 26, 7} Output: Not Possible Approach: Get the index of the element which is making the array unsorted. Get the maximum element from the second array satisfying the neighboring conditions of the element with wrong index i.e max element >= arr[wrong index-1] max element <= arr[wrong index+1] if wrong index+1 exists Implementation: C++ // C++ program to make array sorted #include <bits/stdc++.h> using namespace std; // Function to check whether there is any // swappable element present to make the first // array sorted bool swapElement(int arr1[], int arr2[], int n) { // wrongIdx is the index of the element // which is making the first array unsorted int wrongIdx = 0; for (int i = 1; i < n; i++) { if (arr1[i] < arr1[i - 1]) wrongIdx = i; } int maximum = INT_MIN; int maxIdx = -1; bool res = false; // Find the maximum element which satisfies the // the above mentioned neighboring conditions for (int i = 0; i < n; i++) { if (arr2[i] > maximum && arr2[i] >= arr1[wrongIdx - 1]) { if (wrongIdx + 1 <= n - 1 && arr2[i] <= arr1[wrongIdx + 1]) { maximum = arr2[i]; maxIdx = i; res = true; } } } // if res is true then swap the element // and make the first array sorted if (res) swap(arr1[wrongIdx], arr2[maxIdx]); return res; } // Function to print the sorted array if elements // are swapped. void getSortedArray(int arr1[], int arr2[], int n) { if (swapElement(arr1, arr2, n)) for (int i = 0; i < n; i++) cout << arr1[i] << " "; else cout << "Not Possible" << endl; } // Drivers code int main() { int arr1[] = { 1, 3, 7, 4, 10 }; int arr2[] = { 2, 1, 6, 8, 9 }; int n = sizeof(arr1) / sizeof(arr1[0]); getSortedArray(arr1, arr2, n); } Java // Java program to make array sorted class GFG { // Function to check whether there // is any swappable element present // to make the first array sorted static boolean swapElement(int[] arr1, int[] arr2, int n) { // wrongIdx is the index of the // element which is making the // first array unsorted int wrongIdx = 0; for (int i = 1; i < n; i++) { if (arr1[i] < arr1[i - 1]) { wrongIdx = i; } } int maximum = Integer.MIN_VALUE; int maxIdx = -1; boolean res = false; // Find the maximum element which // satisfies the above mentioned // neighboring conditions for (int i = 0; i < n; i++) { if (arr2[i] > maximum && arr2[i] >= arr1[wrongIdx - 1]) { if (wrongIdx + 1 <= n - 1 && arr2[i] <= arr1[wrongIdx + 1]) { maximum = arr2[i]; maxIdx = i; res = true; } } } // if res is true then swap // the element and make the // first array sorted if (res) { swap(arr1, wrongIdx, arr2, maxIdx); } return res; } static void swap(int[] a, int wrongIdx, int[] b, int maxIdx) { int c = a[wrongIdx]; a[wrongIdx] = b[maxIdx]; b[maxIdx] = c; } // Function to print the sorted // array if elements are swapped. static void getSortedArray(int arr1[], int arr2[], int n) { if (swapElement(arr1, arr2, n)) { for (int i = 0; i < n; i++) { System.out.print(arr1[i] + " "); } } else { System.out.println("Not Possible"); } } // Driver code public static void main(String[] args) { int arr1[] = {1, 3, 7, 4, 10}; int arr2[] = {2, 1, 6, 8, 9}; int n = arr1.length; getSortedArray(arr1, arr2, n); } } // This code contributed by 29AjayKumar Python3 # Python3 program to make array sorted import sys # Function to check whether there is any # swappable element present to make the # first array sorted def swapElement(arr1, arr2, n) : # wrongIdx is the index of the element # which is making the first array unsorted wrongIdx = 0; for i in range(1, n) : if (arr1[i] < arr1[i - 1]) : wrongIdx = i maximum = -(sys.maxsize - 1) maxIdx = -1 res = False # Find the maximum element which satisfies # the above mentioned neighboring conditions for i in range(n) : if (arr2[i] > maximum and arr2[i] >= arr1[wrongIdx - 1]) : if (wrongIdx + 1 <= n - 1 and arr2[i] <= arr1[wrongIdx + 1]) : maximum = arr2[i] maxIdx = i res = True # if res is true then swap the element # and make the first array sorted if (res) : (arr1[wrongIdx], arr2[maxIdx]) = (arr2[maxIdx], arr1[wrongIdx]) return res # Function to print the sorted array # if elements are swapped. def getSortedArray(arr1, arr2, n) : if (swapElement(arr1, arr2, n)) : for i in range(n) : print(arr1[i], end = " ") else : print("Not Possible") # Driver code if __name__ == "__main__" : arr1 = [ 1, 3, 7, 4, 10 ] arr2 = [ 2, 1, 6, 8, 9 ] n = len(arr1) getSortedArray(arr1, arr2, n) # This code is contributed by Ryuga C# // C# program to make array sorted using System; class GFG { // Function to check whether there // is any swappable element present // to make the first array sorted static bool swapElement(int[] arr1, int[] arr2, int n) { // wrongIdx is the index of the // element which is making the // first array unsorted int wrongIdx = 0; for (int i = 1; i < n; i++) { if (arr1[i] < arr1[i - 1]) { wrongIdx = i; } } int maximum = int.MinValue; int maxIdx = -1; bool res = false; // Find the maximum element which // satisfies the above mentioned // neighboring conditions for (int i = 0; i < n; i++) { if (arr2[i] > maximum && arr2[i] >= arr1[wrongIdx - 1]) { if (wrongIdx + 1 <= n - 1 && arr2[i] <= arr1[wrongIdx + 1]) { maximum = arr2[i]; maxIdx = i; res = true; } } } // if res is true then swap the element // and make the first array sorted if (res) { swap(arr1, wrongIdx, arr2, maxIdx); } return res; } static void swap(int[] a, int wrongIdx, int[] b, int maxIdx) { int c = a[wrongIdx]; a[wrongIdx] = b[maxIdx]; b[maxIdx] = c; } // Function to print the sorted array // if elements are swapped. static void getSortedArray(int []arr1, int []arr2, int n) { if (swapElement(arr1, arr2, n)) { for (int i = 0; i < n; i++) { Console.Write(arr1[i] + " "); } } else { Console.Write("Not Possible"); } } // Driver Code public static void Main() { int []arr1 = {1, 3, 7, 4, 10}; int []arr2 = {2, 1, 6, 8, 9}; int n = arr1.Length; getSortedArray(arr1, arr2, n); } } // This code is contributed // by PrinciRaj1992 JavaScript <script> // Javascript program to make array sorted // Function to check whether there // is any swappable element present // to make the first array sorted function swapElement(arr1, arr2, n) { // wrongIdx is the index of the // element which is making the // first array unsorted let wrongIdx = 0; for (let i = 1; i < n; i++) { if (arr1[i] < arr1[i - 1]) { wrongIdx = i; } } let maximum = Number.MIN_VALUE; let maxIdx = -1; let res = false; // Find the maximum element which // satisfies the above mentioned // neighboring conditions for (let i = 0; i < n; i++) { if (arr2[i] > maximum && arr2[i] >= arr1[wrongIdx - 1]) { if (wrongIdx + 1 <= n - 1 && arr2[i] <= arr1[wrongIdx + 1]) { maximum = arr2[i]; maxIdx = i; res = true; } } } // if res is true then swap the element // and make the first array sorted if (res) { swap(arr1, wrongIdx, arr2, maxIdx); } return res; } function swap(a, wrongIdx, b, maxIdx) { let c = a[wrongIdx]; a[wrongIdx] = b[maxIdx]; b[maxIdx] = c; } // Function to print the sorted array // if elements are swapped. function getSortedArray(arr1, arr2, n) { if (swapElement(arr1, arr2, n)) { for (let i = 0; i < n; i++) { document.write(arr1[i] + " "); } } else { document.write("Not Possible"); } } let arr1 = [1, 3, 7, 4, 10]; let arr2 = [2, 1, 6, 8, 9]; let n = arr1.length; getSortedArray(arr1, arr2, n); // This code is contributed by decode2207. </script> Output1 3 7 9 10 Complexity Analysis: Time Complexity: O(n)Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article Maximum in an array that can make another array sorted sahilkhoslaa Follow Improve Article Tags : Misc Searching Sorting DSA Arrays +1 More Practice Tags : ArraysMiscSearchingSorting Similar Reads Maximum j - i in an array such that arr[i] <= arr[j] Given an array arr[] of n positive integers, the task is to find the maximum of j - i subjected to the constraint of arr[i] <= arr[j] and i <= j.Examples : Input: arr[] = [34, 8, 10, 3, 2, 80, 30, 33, 1]Output: 6 Explanation: for i = 1 and j = 7.Input: arr[] = [1, 2, 3, 4, 5, 6]Output: 5 Expla 15+ min read Maximum adjacent difference in an array in its sorted form Given an array arr[] of size N, find the maximum difference between its two consecutive elements in its sorted form. Examples: Input: N = 3, arr[] = {1, 10, 5}Output: 5Explanation: Sorted array would be {1, 5, 10} and maximum adjacent difference would be 10 - 5 = 5 Input: N = 4, arr[] = {2, 4, 8, 11 8 min read Maximize Array sum by swapping at most K elements with another array Given two arrays A and B of size N and an integer K, the task is to find the maximum possible sum of array A by swapping at most K elements with array B. Examples: Input: A[] = {2, 3, 4}, B[] = {6, 8, 5}, K = 1 Output: 15 Explanation: Swap A[0] and B[1]. Hence sum = 8 + 3 + 4 = 15. Input: A[] = {9, 6 min read Maximum array from two given arrays keeping order same Given two same-sized arrays a[] and b[] (each containing distinct elements individually, but they may have some common elements between them). The task is to create a third array res[] of the same size n that includes maximum n elements combined from both arrays.Start by including elements from a[] 8 min read Maximum element in a sorted and rotated array Given a sorted array arr[] (may contain duplicates) of size n that is rotated at some unknown point, the task is to find the maximum element in it. Examples: Input: arr[] = {5, 6, 1, 2, 3, 4}Output: 6Explanation: 6 is the maximum element present in the array.Input: arr[] = {3, 2, 2, 2}Output: 3Expla 7 min read Maximum value in an array after m range increment operations Consider an array of size n with all initial values as 0. We need to perform the following m range increment operations.increment(a, b, k) : Increment values from 'a' to 'b' by 'k'. After m operations, we need to calculate the maximum of the values in the array.Examples:Input : n = 5 m = 3 a = 0, b 10 min read Maximum AND value of a pair in an array Given an array of N positive elements, the task is to find the maximum AND value generated by any pair of elements from the array. Examples: Input: arr1[] = {16, 12, 8, 4}Output: 8Explanation: 8 AND12 will give us the maximum value 8 Input: arr1[] = {4, 8, 16, 2}Output: 0 Recommended PracticeMaximum 12 min read Maximize X such that Array can be sorted by swapping elements X distance apart Given an input arr[] of length N, Which contains integers from 1 to N in unsorted order. You can apply only one type of operation on arr[] elements': Choose an index i and an integer X and swap Ai with an element that is X distance apart and both the indices are within the boundary of the array. The 10 min read Maximum subarray sum in an array created after repeated concatenation | Set-2 Given an array arr[] consisting of N integers and a positive integer K, the task is to find the largest sum of any contiguous subarray in the modified array formed by repeating the given array K times. Examples: Input: arr[] = {-1, 10, 20}, K = 2Output: 59Explanation:After concatenating the array tw 15+ min read Print all maximal increasing contiguous sub-array in an array Given an array arr[], the task is to find all the maximal contiguous increasing subarray in a given array. Examples: Input: arr[] = { 80, 50, 60, 70, 40, 50, 80, 70 } Output: 80 50 60 70 40 50 80 70 Input: arr[] = { 10, 20, 23, 12, 5, 4, 61, 67, 87, 9 } Output: 10 20 23 12 5 4 61 67 87 9 Approach: I 4 min read Like