Given an integer array **arr[], **the task is to minimize the length of the given array by repeatedly replacing **two unequal adjacent array elements** by their sum. Once the array is reduced to its minimum possible length, i.e. no adjacent unequal pairs are remaining in the array, print the count of operations required.

**Examples:**

Input:arr[] = {2, 1, 3, 1}Output:1Explanation:

Operation 1: {2, 1,3, 1} -> {3, 3, 1}

Operation 2: {3,3, 1} -> {3, 4}

Operation 3: {3, 4} -> {7}

Therefore, the minimum length the array can be reduced to is 1.

Input:arr[] = {1, 1, 1, 1}Output:4Explanation:

No merge operation is possible as no unequal adjacent pair can be obtained.

Hence, the minimum length of the array is 4.

**Naive Approach: **The simplest approach to solve the problem is to traverse the given array and for every adjacent unequal pair, replace the pair by its sum. Finally, if no unequal pair exists in the array, print the length of the array. **Time Complexity:** O(N^{2})**Auxiliary Space**: O(N)

**Efficient Approach: **The above approach can be optimized based on the following observations:

- If all the elements of the array are equal, then no operation can be performed. Therefore, print
, i.e., the initial length of the array, as the minimum reducible length of the array**N** - Otherwise, the minimum length of the array will always be
**1**.

Therefore, to solve the problem, simply traverse the array and check if all array elements are equal or not. If found to be true, print **N** as the required answer. Otherwise, print **1**.

Below is the implementation of the above approach:

## C++

`// C++ program for ` `// the above approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function that returns the minimum ` `// length of the array after merging ` `// unequal adjacent elements ` `int` `minLength(` `int` `A[], ` `int` `N) ` `{ ` ` ` ` ` `// Stores the first element ` ` ` `// and its frequency ` ` ` `int` `elem = A[0], count = 1; ` ` ` ` ` `// Traverse the array ` ` ` `for` `(` `int` `i = 1; i < N; i++) { ` ` ` `if` `(A[i] == elem) { ` ` ` `count++; ` ` ` `} ` ` ` `else` `{ ` ` ` `break` `; ` ` ` `} ` ` ` `} ` ` ` ` ` `// If all elements are equal ` ` ` `if` `(count == N) ` ` ` ` ` `// No merge-pair operations ` ` ` `// can be performed ` ` ` `return` `N; ` ` ` ` ` `// Otherwise ` ` ` `else` ` ` `return` `1; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `// Given array ` ` ` `int` `arr[] = { 2, 1, 3, 1 }; ` ` ` ` ` `// Length of the array ` ` ` `int` `N = ` `sizeof` `(arr) / ` `sizeof` `(arr[0]); ` ` ` ` ` `// Function Call ` ` ` `cout << minLength(arr, N) << endl; ` ` ` ` ` `return` `0; ` `}` |

*chevron_right*

*filter_none*

## Java

`// Java program for ` `// the above approach ` `class` `GFG{ ` ` ` `// Function that returns the minimum ` `// length of the array ` `// after merging unequal ` `// adjacent elements ` `static` `int` `minLength(` `int` `A[], ` ` ` `int` `N) ` `{ ` ` ` `// Stores the first element ` ` ` `// and its frequency ` ` ` `int` `elem = A[` `0` `], count = ` `1` `; ` ` ` ` ` `// Traverse the array ` ` ` `for` `(` `int` `i = ` `1` `; i < N; i++) ` ` ` `{ ` ` ` `if` `(A[i] == elem) ` ` ` `{ ` ` ` `count++; ` ` ` `} ` ` ` `else` ` ` `{ ` ` ` `break` `; ` ` ` `} ` ` ` `} ` ` ` ` ` `// If all elements are equal ` ` ` `if` `(count == N) ` ` ` ` ` `// No merge-pair operations ` ` ` `// can be performed ` ` ` `return` `N; ` ` ` ` ` `// Otherwise ` ` ` `else` ` ` `return` `1` `; ` `} ` ` ` `// Driver Code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `// Given array ` ` ` `int` `arr[] = {` `2` `, ` `1` `, ` `3` `, ` `1` `}; ` ` ` ` ` `// Length of the array ` ` ` `int` `N = arr.length; ` ` ` ` ` `// Function Call ` ` ` `System.out.print(minLength(arr, N) + ` `"\n"` `); ` `} ` `} ` ` ` `// This code is contributed by Rajput-Ji` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program for the above approach ` ` ` `# Function that returns the minimum ` `# length of the array after merging ` `# unequal adjacent elements ` `def` `minLength(A, N): ` ` ` ` ` `# Stores the first element ` ` ` `# and its frequency ` ` ` `elem ` `=` `A[` `0` `] ` ` ` `count ` `=` `1` ` ` ` ` `# Traverse the array ` ` ` `for` `i ` `in` `range` `(` `1` `, N): ` ` ` `if` `(A[i] ` `=` `=` `elem): ` ` ` `count ` `+` `=` `1` ` ` `else` `: ` ` ` `break` ` ` ` ` `# If all elements are equal ` ` ` `if` `(count ` `=` `=` `N): ` ` ` ` ` `# No merge-pair operations ` ` ` `# can be performed ` ` ` `return` `N ` ` ` ` ` `# Otherwise ` ` ` `else` `: ` ` ` `return` `1` ` ` `# Driver Code ` ` ` `# Given array ` `arr ` `=` `[ ` `2` `, ` `1` `, ` `3` `, ` `1` `] ` ` ` `# Length of the array ` `N ` `=` `len` `(arr) ` ` ` `# Function call ` `print` `(minLength(arr, N)) ` ` ` `# This code is contributed by code_hunt ` |

*chevron_right*

*filter_none*

## C#

`// C# program for ` `// the above approach ` `using` `System; ` `class` `GFG{ ` ` ` `// Function that returns the minimum ` `// length of the array ` `// after merging unequal ` `// adjacent elements ` `static` `int` `minLength(` `int` `[]A, ` ` ` `int` `N) ` `{ ` ` ` `// Stores the first element ` ` ` `// and its frequency ` ` ` `int` `elem = A[0], count = 1; ` ` ` ` ` `// Traverse the array ` ` ` `for` `(` `int` `i = 1; i < N; i++) ` ` ` `{ ` ` ` `if` `(A[i] == elem) ` ` ` `{ ` ` ` `count++; ` ` ` `} ` ` ` `else` ` ` `{ ` ` ` `break` `; ` ` ` `} ` ` ` `} ` ` ` ` ` `// If all elements are equal ` ` ` `if` `(count == N) ` ` ` ` ` `// No merge-pair operations ` ` ` `// can be performed ` ` ` `return` `N; ` ` ` ` ` `// Otherwise ` ` ` `else` ` ` `return` `1; ` `} ` ` ` `// Driver Code ` `public` `static` `void` `Main(String[] args) ` `{ ` ` ` `// Given array ` ` ` `int` `[]arr = {2, 1, 3, 1}; ` ` ` ` ` `// Length of the array ` ` ` `int` `N = arr.Length; ` ` ` ` ` `// Function Call ` ` ` `Console.Write(minLength(arr, N) + ` `"\n"` `); ` `} ` `} ` ` ` `// This code is contributed by Rajput-Ji` |

*chevron_right*

*filter_none*

**Output:**

1

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

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.

## Recommended Posts:

- Minimize Array length by repeatedly replacing co-prime pairs with 1
- Last element remaining by deleting two largest elements and replacing by their absolute difference if they are unequal
- Array value by repeatedly replacing max 2 elements with their absolute difference
- Minimize the sum calculated by repeatedly removing any two elements and inserting their sum to the Array
- Minimize length of string by replacing K pairs of distinct adjacent characters
- Maximize array sum by replacing equal adjacent pairs by their sum and X respectively
- Maximize modulus by replacing adjacent pairs with their modulus for any permutation of given Array
- Smallest array that can be obtained by replacing adjacent pairs with their products
- Maximize sum of squares of array elements possible by replacing pairs with their Bitwise AND and Bitwise OR
- Minimize count of unequal elements at corresponding indices between given arrays
- Maximize count of equal numbers in Array of numbers upto N by replacing pairs with their sum
- Maximum sum of Array formed by replacing each element with sum of adjacent elements
- Reduce number to a single digit by subtracting adjacent digits repeatedly
- Minimize swaps required to maximize the count of elements replacing a greater element in an Array
- Count of pairs in a given range with sum of their product and sum equal to their concatenated number
- Count unequal element pairs from the given Array
- Find last two remaining elements after removing median of any 3 consecutive elements repeatedly
- Count pairs from 1 to N such that their Sum is divisible by their XOR
- Maximum distance between two unequal elements
- Largest number in given Array formed by repeatedly combining two same elements

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.