# Minimize array length by repeatedly replacing pairs of unequal adjacent array elements by their sum

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: 1
Explanation:
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: 4
Explanation:
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(N2)
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 N, i.e., the initial length of the array, as the minimum reducible length of the array
• 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++ program for  ` `// the above approach ` `#include ` `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; ` `}`

 `// 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`

 `# 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 `

 `// 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`

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.

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.

Improved By : Rajput-Ji, code_hunt

Article Tags :