Open In App

Minimize Array size by replacing adjacent integers by their Modulo

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:

Follow the below steps to implement the idea:

Below is the implementation of the above approach.




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




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




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




// 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
// 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";
?>




# 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:


Article Tags :