Related Articles

# Smallest array that can be obtained by replacing adjacent pairs with their products

• Last Updated : 05 May, 2021

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:
Explanation:
Pick arr and arr and replace with arr*arr, arr[] = {7, 7, 1, 7, 1}
Pick arr and arr and replace with arr*arr, arr[] = {49, 1, 7, 1}
Pick arr and arr and replace with arr*arr, arr[] = {49, 7, 1}
Pick arr and arr and replace with arr*arr, arr[] = {343, 1}
Pick arr and arr and replace with arr*arr, arr[] = {343}

Input: arr[] = {2, 2, 2, 2}
Output:

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:

1. Iterate over the given array arr[].
2. If all elements of the array are same, then print N as the required answer.
3. 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 ``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 != 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);` `    ``// 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 != 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

 ``
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.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

My Personal Notes arrow_drop_up