Smallest array that can be obtained by replacing adjacent pairs with their products
Given an array arr[] of size N, the task is to print the least possible size the given array can be reduced to by performing the following operations:
- Remove any two adjacent elements, say arr[i] and arr[i+1] and insert a single element arr[i] * arr[i+1] at that position in the array.
- If all array elements become equal, then print the size of the array.
Examples:
Input: arr[] = {1, 7, 7, 1, 7, 1}
Output: 1
Explanation:
Pick arr[0] and arr[1] and replace with arr[0]*arr[1], arr[] = {7, 7, 1, 7, 1}
Pick arr[0] and arr[1] and replace with arr[0]*arr[1], arr[] = {49, 1, 7, 1}
Pick arr[0] and arr[1] and replace with arr[0]*arr[1], arr[] = {49, 7, 1}
Pick arr[0] and arr[1] and replace with arr[0]*arr[1], arr[] = {343, 1}
Pick arr[0] and arr[1] and replace with arr[0]*arr[1], arr[] = {343}Input: arr[] = {2, 2, 2, 2}
Output: 4
Approach: The approach is based on the idea that if all array elements are the same, then the given operations can’t be performed on the given array. Otherwise, in every case, the array size can be reduced to 1. Below are the steps:
- Iterate over the given array arr[].
- If all elements of the array are same, then print N as the required answer.
- Otherwise, always pick adjacent elements which give the maximum product to reduce the size of arr[] to 1. Therefore, the minimum possible size will be 1.
Below is the implementation of the above approach:
C++
// C++ implementation of the above approach #include <bits/stdc++.h> using namespace std; // Function to minimize the size of // array by performing given operations int minLength( int arr[], int N) { for ( int i = 1; i < N; i++) { // If all array elements // are not same if (arr[0] != arr[i]) { return 1; } } // If all array elements // are same return N; } // Driver Code int main() { int arr[] = { 2, 1, 3, 1 }; int N = sizeof (arr) / sizeof (arr[0]); // Function Call cout << minLength(arr, N); return 0; } |
Java
// Java implementation of the above approach import java.io.*; class GFG{ // Function to minimize the size of // array by performing given operations static int minLength( int arr[], int N) { for ( int i = 1 ; i < N; i++) { // If all array elements // are not same if (arr[ 0 ] != arr[i]) { return 1 ; } } // If all array elements // are same return N; } // Driver Code public static void main(String[] args) { int [] arr = { 2 , 1 , 3 , 1 }; int N = arr.length; // Function call System.out.print(minLength(arr, N)); } } // This code is contributed by akhilsaini |
Python3
# Python3 implementation of the above approach # Function to minimize the size of # array by performing given operations def minLength(arr, N): for i in range ( 1 , N): # If all array elements # are not same if (arr[ 0 ] ! = arr[i]): return 1 # If all array elements # are same return N # Driver Code if __name__ = = "__main__" : arr = [ 2 , 1 , 3 , 1 ] N = len (arr) # Function call print (minLength(arr, N)) # This code is contributed by akhilsaini |
C#
// C# implementation of the above approach using System; class GFG{ // Function to minimize the size of // array by performing given operations static int minLength( int [] arr, int N) { for ( int i = 1; i < N; i++) { // If all array elements // are not same if (arr[0] != arr[i]) { return 1; } } // If all array elements // are same return N; } // Driver Code public static void Main() { int [] arr = { 2, 1, 3, 1 }; int N = arr.Length; // Function call Console.Write(minLength(arr, N)); } } // This code is contributed by akhilsaini |
Javascript
<script> // Javascript program to implement // the above approach // Function to minimize the size of // array by performing given operations function minLength(arr, N) { for (let i = 1; i < N; i++) { // If all array elements // are not same if (arr[0] != arr[i]) { return 1; } } // If all array elements // are same return N; } // Driver Code let arr = [ 2, 1, 3, 1 ]; let N = arr.length; // Function call document.write(minLength(arr, N)); </script> |
1
Time Complexity: O(N)
Auxiliary Space: O(1)