Reduce array to a single element by repeatedly replacing adjacent unequal pairs with their maximum

Given an array arr[] consisting of N integers, the task is to reduce the given array to a single element by repeatedly replacing any pair of consecutive unequal elements, say arr[i] and arr[i+1] with max(arr[i], arr[i + 1]) + 1. If possible, print the index of the element from where the operation can be started. Otherwise, print -1.

Examples:

Input: arr[] = {5, 3, 4, 4, 5} 
Output: 1 
Explanation:
Step 1: Replace arr[1] and arr[2] with max(arr[1], arr[2])+1 = max(5, 3) + 1 = 6. Therefore, arr[] = {6, 4, 4, 5}.
Step 2: Replace arr[1] and arr[2] with max(arr[1], arr[2]) + 1 = max(6, 4) + 1 = 7. Therefore, arr[] = {7, 4, 5}.
Step 3: Replace arr[1] and arr[2] with max(arr[1], arr[2])+1 = max(7, 4) + 1 = 8. Therefore, arr[] = {8, 5}.
Step 4: Replace arr[1] and arr[2] with max(arr[1], arr[2]) + 1 = max(8, 5)+1 = 9. Therefore, arr[] = {9}.

Input: arr[] ={1, 1} 
Output: -1

Naive Approach: The simplest approach is to traverse the given array and for each element arr[i], start performing the given operation continuously according to the given constraints. If for any element, the array becomes a single element, print the index i otherwise, print -1.
Time Complexity: O(N2)
Auxiliary Space: O(N)

Efficient Approach: The idea is to use a Sorting Algorithm. Notice that the answer will always be -1 if all the elements are the same. Otherwise, the index having the maximum element can be chosen to starting performing the operations. Follow the below steps to solve the problem:



  • Create another array B[] same as the given array and create a variable save initialize with -1 to store the answer.
  • Sort the array B[].
  • Traverse the array over the range [N – 1 to 0] using the variable i and if two consecutive unequal elements are found i.e., B[i] is not equals to B[i – 1], update save as save = i.
  • After traversing the array:
    • If save is -1, print -1 and return.
    • Else if save is equal to arr[0] and save is not equals arr[1], then update save as 1.
    • Else if save is equal to arr[N – 1] and save is not equal to arr[N – 2], then update save as N.
    • Otherwise, iterate a loop over the range [1, N – 1] and check if save is equal to arr[i] such that arr[i] is not equals to arr[i – 1] and arr[i+1], then update save as save = i+1.
  • After the above steps, print the index that is stored in the variable save.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program for the above approach
  
#include <bits/stdc++.h>
using namespace std;
  
// Function to print the index from
// where the operation can be started
void printIndex(int arr[], int N)
{
    // Initialize B[]
    int B[N];
  
    // Initialize save
    int save = -1;
  
    // Make B[] equals to arr[]
    for (int i = 0; i < N; i++) {
        B[i] = arr[i];
    }
  
    // Sort the array B[]
    sort(B, B + N);
  
    // Traverse from N-1 to 1
    for (int i = N - 1; i >= 1; i--) {
  
        // If B[i] & B[i-1] are unequal
        if (B[i] != B[i - 1]) {
            save = B[i];
            break;
        }
    }
  
    // If all elements are same
    if (save == -1) {
        cout << -1 << endl;
        return;
    }
  
    // If arr[1] is maximum element
    if (save == arr[0]
        && save != arr[1]) {
        cout << 1;
    }
  
    // If arr[N-1] is maximum element
    else if (save == arr[N - 1]
             && save != arr[N - 2]) {
        cout << N;
    }
  
    // Find the maximum element
    for (int i = 1; i < N - 1; i++) {
  
        if (save == arr[i]
            && (save != arr[i - 1]
                || save != arr[i + 1])) {
            cout << i + 1;
            break;
        }
    }
}
  
// Driver Code
int main()
{
    // Given array arr[]
    int arr[] = { 5, 3, 4, 4, 5 };
  
    // Length of array
    int N = sizeof(arr) / sizeof(arr[0]);
  
    // Function Call
    printIndex(arr, N);
  
    return 0;
}

chevron_right


Output:

1

Time Complexity: O(N)
Auxiliary Space: O(N)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Recommended Posts:



If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.