Array Transformation in C++



Array transformation is a popular coding question asked in technical assessments and interviews. In this article, we will discuss the problem of array transformation with example testcases, the algorithm to solve it, and provide a C/C++ implementation.

Array Transformation Based on Local Maxima and Minima

In this problem, you are given an array of integers arr[] of size N. Every day the array undergoes a transformation which can be defined as follows:

The first and last elements of the array are not transformed. For every other element arr[i] in the array, where 0 < i < N-1,:

  • If arr[i] is greater than the elements to it's left and right (local maxima), then arr[i] is decremented by 1.
  • If arr[i] is less than the elements to it's left and right (local minima), then arr[i] is incremented by 1.

This process is repeated until no further changes can be made to the array. Your task is to find the final state of the array after all transformations are complete.

Consider the following example scenarios to under the concept better:

Scenario 1

Input:
 [6, 2, 3, 4, 5]

Output:
 [6, 3, 3, 4, 5]

Explanation: The first and last elements of the array are not transformed. On the first day, 2 is incremented to 3 since it's surrounded by 6 and 3, which are larger. 3 and 4 remain unchanged because they are not surrounded by smaller or larger elements. The array after first day will be [6, 3, 3, 4, 5].

On day 2, no further changes can be made because there is no any local minima or local maxima in the array. So we will stop at this stage and hence the final state of the array is [6, 3, 3, 4, 5].

Example Scenario 2

Input:
 [1, 6, 3, 4, 3, 5]

Output:
 [1, 3, 4, 4, 4, 5]

On the first day, the transformations are as follows:

  • 6 is decremented to 5 (since it is surrounded by 1 and 3, which are smaller).
  • 3 is incremented to 4 (since it is surrounded by 6 and 4, which are larger).
  • 4 is decremented to 3 (since it is surrounded by 3 and 3, which are smaller).
  • 3 is incremented to 4 (since it is surrounded by 4 and 5, which are larger).

After the first day, the array becomes [1, 5, 4, 3, 4, 5]. On the second day following transformations occur:

  • 5 is decremented to 4 (since it is surrounded by 1 and 4, which are smaller).
  • 3 is incremented to 4 (since it is surrounded by 4 and 4, which are larger).
  • Rest of the elements remain unchanged.

After the second day, the array becomes [1, 4, 4, 4, 4, 5]. On the third day, no further changes can be made because there is no local minima or maxima in the array. So we will stop at this stage and hence the final state of the array is [1, 4, 4, 4, 4, 5].

Steps to Solve the Array Transformation Problem

To solve the array transformation problem, you follow these steps:

  • Declare and initialize the input array arr[] of size N.
  • Define a a boolean variable changed to track if any changes are made to array during a transformation. ( Set initial value to true)
  • Start a while loop that continues until the changed variable is false.
  • Inside the loop, set changed to false and copy arr[] to a temporary array temp[].
  • Now, iterate through the temporary array temp[] from index 1 to N-2 and update the elements based on the following conditions:
  • Condition 1: If temp[i-1] < temp[i] and temp[i+1] < temp[i], then temp[i] is decremented by 1.
  • Condition 2: If temp[i-1] > temp[i] and temp[i+1] > temp[i], then temp[i] is incremented by 1.
  • If any element is updated, set changed to true. (i.e., if no elements are updated, the changed variable will remain false and the while loop will be exited)
  • After the loop, print the final state of the array.

C++ Implementation of Array Transformation

Here is a C++ implementation of the array transformation problem:

#include <iostream>
#include <vector>
using namespace std;

vector<int> transformArray(vector<int>& arr) {
    int n = arr.size();
    bool changed = true;

    while (changed) {
        changed = false;
        vector<int> newArr = arr;

        for (int i = 1; i < n - 1; ++i) {
            if (arr[i] < arr[i - 1] && arr[i] < arr[i + 1]) {
                newArr[i]++;
                changed = true;
            } else if (arr[i] > arr[i - 1] && arr[i] > arr[i + 1]) {
                newArr[i]--;
                changed = true;
            }
        }

        arr = newArr;
    }

    return arr;
}

int main() {
    vector<int> arr = {1, 6, 3, 4, 3, 5};
    vector<int> result = transformArray(arr);

    cout << "Final stable array: ";
    for (int num : result) {
        cout << num << " ";
    }
    cout << endl;

    return 0;
}

The output of above code will be:

Final stable array: 1 4 4 4 4 5
Farhan Muhamed
Farhan Muhamed

No Code Developer, Vibe Coder

Updated on: 2025-07-25T16:59:14+05:30

618 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements