Open In App

Minimize Array size by replacing adjacent integers by their Modulo

Last Updated : 07 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Given an array arr[] of positive integers of size N, the task is to find the minimum possible size of the array that can be obtained by replacing any two adjacent positive elements with their modulo i.e., (arr[i]%arr[i+1] or arr[i+1]%arr[i]).

Examples:

Input: arr[] = {3, 4, 5, 2, 1}
Output: 1
Explanation:  The following series of operations leads to a minimum size of the array under given conditions.
Select i = 0 and i+1. Replace them with 3%4. Updated arr[] = {3, 5, 2, 1}
Select i = 0 and i+1. Replace them with 3%5. Updated arr[] = {3, 2, 1}
Select i = 1 and i+1. Replace them with 1%2. Updated arr[] = {3, 1}
Select i = 1 and i+1. Replace them with 1%3. Updated arr[] = {1}

Input: arr[] = {2, 2, 2, 2}
Output: 2
Explanation: The following series of operations leads to a minimum size of the array under given conditions.
Select i = 0 and i+1. Replace them 2%2. Updated arr[] = {0, 2, 2}
Select i = 1 and i+1. Replace them 2%2. Updated arr[] = {0, 0}
Since there are no more adjacent positive integers left in the array we cannot perform the operation. So the final array will have no less than 2 elements.

Approach: The problem can be solved based on the following idea:

If two adjacent elements in the array are unequal, then we can always make their modulo to be equal to the smaller number (a%b = a where a is smaller than b). We can find the smallest number in the array, say min_ele.

Now, we can have two cases:

  • Case 1: All the elements in arr[] are multiple of min_ele.
    • In this case, we can remove all the elements of the array that are multiples of min_ele and are greater than min_ele.
    • For the remaining elements which are equal to min_ele, we can reduce two elements and replace them with a zero.
    • So, the final answer will be (frequency of min_ele + 1) / 2.
  • Case 2: There is at least one element in arr[] that is not a multiple of min_ele, say X.
    • So, we can reduce all the multiples of min_ele with min_ele.
    • Then, we can modulo X by min_ele to get a remainder rem, such that 0 < rem < X.
    • Now, we can reduce all the remaining elements by applying modulo with rem and we will be left with only one element, that is rem.
    • So, the final answer will be 1.

Follow the below steps to implement the idea:

  • Find the minimum element in the array arr[], say min_ele.
  • Iterate over the array and check if arr[i] is not a multiple of min_ele.
  • If we find a number arr[i] which is not a multiple of min_ele, then print 1.
  • Otherwise, find the frequency of the min_ele, say cnt and return (cnt + 1) / 2.

Below is the implementation of the above approach.

C++




// C++ code to implement the approach
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to find minimum array size
int min_size(int arr[], int n)
{
    int min_ele = *min_element(arr, arr + n);
    for (int i = 0; i < n; i++)
        if (arr[i] % min_ele > 0)
            return 1;
    int cnt = count(arr, arr + n, min_ele);
    return (cnt + 1) / 2;
}
 
// Driver code
int main()
{
    // Test case 1
    int arr1[] = { 5, 5, 5, 5, 4 };
    int N = sizeof(arr1) / sizeof(arr1[0]);
 
    // Function call
    cout << min_size(arr1, N) << endl;
 
    // Test case 2
    int arr2[] = { 5, 2, 2, 2, 2, 2, 2 };
    N = sizeof(arr2) / sizeof(arr2[0]);
 
    // Function call
    cout << min_size(arr2, N) << endl;
 
    return 0;
}


Java




import java.util.Arrays;
 
public class Main {
    // Function to find minimum array size
    static int minSize(int[] arr) {
        int minEle = Arrays.stream(arr).min().getAsInt();
        for (int i = 0; i < arr.length; i++)
            if (arr[i] % minEle > 0)
                return 1;
        long cnt = Arrays.stream(arr).filter(x -> x == minEle).count();
        return (int)((cnt + 1) / 2);
    }
 
    // Driver code
    public static void main(String[] args) {
        // Test case 1
        int[] arr1 = { 5, 5, 5, 5, 4 };
 
        // Function call
        System.out.println(minSize(arr1));
 
        // Test case 2
        int[] arr2 = { 5, 2, 2, 2, 2, 2, 2 };
 
        // Function call
        System.out.println(minSize(arr2));
    }
}


C#




using System;
using System.Linq;
 
class Program
{
    // Function to find minimum array size
    static int MinSize(int[] arr)
    {
        int minEle = arr.Min();
        foreach (int num in arr)
        {
            if (num % minEle > 0)
                return 1;
        }
        long cnt = arr.Count(x => x == minEle);
        return (int)((cnt + 1) / 2);
    }
 
    // Driver code
    static void Main(string[] args)
    {
        // Test case 1
        int[] arr1 = { 5, 5, 5, 5, 4 };
 
        // Function call
        Console.WriteLine(MinSize(arr1));
 
        // Test case 2
        int[] arr2 = { 5, 2, 2, 2, 2, 2, 2 };
 
        // Function call
        Console.WriteLine(MinSize(arr2));
    }
}


Javascript




// Function to find minimum array size
function minSize(arr) {
    let minEle = Math.min(...arr);
    for (let num of arr) {
        if (num % minEle > 0)
            return 1;
    }
    let cnt = arr.filter(x => x === minEle).length;
    return Math.floor((cnt + 1) / 2);
}
 
// Test case 1
let arr1 = [5, 5, 5, 5, 4];
console.log(minSize(arr1));
 
// Test case 2
let arr2 = [5, 2, 2, 2, 2, 2, 2];
console.log(minSize(arr2));


PHP




<?php
// Function to find minimum array size
function minSize($arr) {
    $minEle = min($arr);
    foreach ($arr as $num) {
        if ($num % $minEle > 0)
            return 1;
    }
    $cnt = array_count_values($arr)[$minEle] ?? 0;
    return intval(($cnt + 1) / 2);
}
 
// Test case 1
$arr1 = [5, 5, 5, 5, 4];
echo minSize($arr1) . "\n";
 
// Test case 2
$arr2 = [5, 2, 2, 2, 2, 2, 2];
echo minSize($arr2) . "\n";
?>


Python3




# Function to find minimum array size
def min_size(arr):
    min_ele = min(arr)
    for num in arr:
        if num % min_ele > 0:
            return 1
    cnt = arr.count(min_ele)
    return (cnt + 1) // 2
 
# Test case 1
arr1 = [5, 5, 5, 5, 4]
print(min_size(arr1))
 
# Test case 2
arr2 = [5, 2, 2, 2, 2, 2, 2]
print(min_size(arr2))


Output

1
1

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

Related Articles:



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads