# Check if Array can be made strictly increasing by merging three consecutive elements M times

• Difficulty Level : Hard
• Last Updated : 05 Aug, 2022

Given an array arr[] of length N and an integer M, the task is to determine if a strictly increasing sequence can be formed by merging three consecutive elements exactly M times.

Note: Merging three elements means removing all three of them and inserting a single element having value same as the sum of the three at the same position.

Examples:

Input: arr = {10, 24, 26, 2, 32, 36}, M = 2
Output: True
Explanation: 1st Operation – Merge arr, arr and arr to get arr = 70,
Delete arr and arr, arr = {10, 24, 26, 70}
2nd Operation – Merge arr, arr and arr to get arr = 60
Delete arr and arr, arr = {60, 70}, which is a strictle increasing array.

Input: arr = {1, 2, 3}, M = 2
Output: True
Explanation: 1st Operation – Merge arr, arr and arr to get arr = 6,
Delete arr and arr, arr = {6}
2nd Operation is not possible as there are not enough elements left.

Approach:

This problem can be solved using greedy approach. Each time decreasing sequence is found, merge the following three elements in order to make the array increasing, remove two elements and decrease the array size. Keep on repeating this process to find whether array can be transformed into strictly increasing after all the M operations.

Follow the below steps to solve the given problem:

• Traverse until i>0 and M>0 and the array is not empty.
• For each position there are three choices:
• If the arr[i]<arr[i-1] and i+2<arr.size() then perform arr[i]=arr[i]+arr[i+1]+arr[i+2] and delete arr[i+1] and arr[i+2] and call the function recursively with new array and M-1.
• If the arr[i]<arr[i-1] and i>=2 then perform arr[i-2]=arr[i-2]+arr[i]+arr[i-1] and delete arr[i-1] and arr[i] and call the function recursively with new array and M-1.
• If the arr[i]<arr[i-1] and i<2 then perform arr[i-1]=arr[i-1]+arr[i]+arr[i+1] and delete arr[i] and arr[i+1] and call the function recursively with new array and M-1.
• If the array is strictly increasing and 2*M<size of array arr then return True else return False.

Below is the implementation of this approach:

## C++14

 `// C++ code to implement the greedy approach``#include ``using` `namespace` `std;`` ` `// function to check whether array is strictly increasing or not``bool` `isIncreasing(vector<``int``>& arr)``{``    ``for` `(``int` `i = 1; i < arr.size(); i++) {``        ``if` `(arr[i - 1] > arr[i])``            ``return` `false``;``    ``}``    ``return` `true``;``}`` ` `// function to check whether we can make``// given an array an increasing array``// in exact M operations``bool` `make_inc_seq(vector<``int``> arr, ``int` `M)``{``    ``vector<``int``> temp(arr);``    ``if` `(isIncreasing(arr) && 2 * M < arr.size())``        ``return` `1;``    ``else` `if` `(!M || arr.size() < 3)``        ``return` `0;``    ``bool` `ans1 = 0, ans2 = 0, ans3 = 0;`` ` `    ``for` `(``int` `i = arr.size() - 1; i > 0; i--) {``        ``if` `(arr[i] < arr[i - 1]) {``            ``if` `(arr.size() > i + 2 && i >= 0) {``                ``arr.clear();``                ``arr = temp;``                ``arr[i] = arr[i] + arr[i + 1] + arr[i + 2];``                ``arr.erase(arr.begin() + i + 1);``                ``arr.erase(arr.begin() + i + 1);``                ``ans1 = make_inc_seq(arr, M - 1);``            ``}``            ``if` `(i >= 2 && i < arr.size()) {``                ``arr.clear();``                ``arr = temp;``                ``arr[i - 2] = arr[i - 2] + arr[i - 1] + arr[i];``                ``arr.erase(arr.begin() + i);``                ``arr.erase(arr.begin() + i - 1);``                ``ans2 = make_inc_seq(arr, M - 1);``            ``}``            ``if` `(i >= 1 && i + 1 < arr.size()) {``                ``arr.clear();``                ``arr = temp;``                ``arr[i - 1] = arr[i - 1] + arr[i] + arr[i + 1];``                ``arr.erase(arr.begin() + i);``                ``arr.erase(arr.begin() + i);``                ``ans3 = make_inc_seq(arr, M - 1);``            ``}``        ``}``    ``}`` ` `    ``return` `ans1 || ans2 || ans3;``}`` ` `// Driver's code``int` `main()``{``    ``vector<``int``> arr = { 10, 24, 26, 2, 32, 36 };``    ``int` `M = 2;``    ``if` `(make_inc_seq(arr, M))``        ``cout << ``"True"``;``    ``else``        ``cout << ``"False"``;``    ``return` `0;``}``// this code is contributed by prophet1999`

Output

`True`

Time Complexity: O(3min(M, N)*N)
Auxiliary Space: O(N)

My Personal Notes arrow_drop_up