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), as we have to use nested loops for traversing N*N times.
Auxiliary Space: O(N), as we have to use extra space.
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++
#include <bits/stdc++.h>
using namespace std;
int minLength( int A[], int N)
{
int elem = A[0], count = 1;
for ( int i = 1; i < N; i++) {
if (A[i] == elem) {
count++;
}
else {
break ;
}
}
if (count == N)
return N;
else
return 1;
}
int main()
{
int arr[] = { 2, 1, 3, 1 };
int N = sizeof (arr) / sizeof (arr[0]);
cout << minLength(arr, N) << endl;
return 0;
}
|
Java
import java.io.*;
class GFG{
static int minLength( int A[],
int N)
{
int elem = A[ 0 ], count = 1 ;
for ( int i = 1 ; i < N; i++)
{
if (A[i] == elem)
{
count++;
}
else
{
break ;
}
}
if (count == N)
return N;
else
return 1 ;
}
public static void main(String[] args)
{
int arr[] = { 2 , 1 , 3 , 1 };
int N = arr.length;
System.out.print(minLength(arr, N) + "\n" );
}
}
|
Python3
def minLength(A, N):
elem = A[ 0 ]
count = 1
for i in range ( 1 , N):
if (A[i] = = elem):
count + = 1
else :
break
if (count = = N):
return N
else :
return 1
arr = [ 2 , 1 , 3 , 1 ]
N = len (arr)
print (minLength(arr, N))
|
C#
using System;
class GFG{
static int minLength( int []A,
int N)
{
int elem = A[0], count = 1;
for ( int i = 1; i < N; i++)
{
if (A[i] == elem)
{
count++;
}
else
{
break ;
}
}
if (count == N)
return N;
else
return 1;
}
public static void Main(String[] args)
{
int []arr = {2, 1, 3, 1};
int N = arr.Length;
Console.Write(minLength(arr, N) + "\n" );
}
}
|
Javascript
<script>
function minLength(A , N)
{
var elem = A[0], count = 1;
for ( var i = 1; i < N; i++) {
if (A[i] == elem) {
count++;
} else {
break ;
}
}
if (count == N)
return N;
else
return 1;
}
var arr = [ 2, 1, 3, 1 ];
var N = arr.length;
document.write(minLength(arr, N) + "\n" );
</script>
|
Time Complexity: O(N), as we are using a loop to traverse N times.
Auxiliary Space: O(1), as we are not using any extra space.