Skip to content
Related Articles

Related Articles

Minimize array length by repeatedly replacing pairs of unequal adjacent array elements by their sum
  • Difficulty Level : Basic
  • Last Updated : 07 May, 2021

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++




// 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;
}

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

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

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

Javascript




<script>
 
// Javascript program for
// the above approach   
 
// Function that returns the minimum
    // length of the array
    // after merging unequal
    // adjacent elements
     
    function minLength(A , N)
    {
        // Stores the first element
        // and its frequency
        var elem = A[0], count = 1;
 
        // Traverse the array
        for (var 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
     
        // Given array
        var arr = [ 2, 1, 3, 1 ];
 
        // Length of the array
        var N = arr.length;
 
        // Function Call
        document.write(minLength(arr, N) + "\n");
 
// This code contributed by aashish1995
 
</script>
Output: 
1

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

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :