Question: We are given an array A consisting of N integers. In one move, we can choose any element in this array and replace it with

We are given an array A consisting of N integers. In one move, we can choose any element in this array and replace it with any value. Find the minimum number of distinct values in the array that we can achieve by performing at most three moves? Write a c++ function: class Solution{public int solution(int[] A); } that, given an array A of N integers, returns the minimum number of distinct values that can be obtained after replacing up to three elements of array A. Examples: 1. Given A =[-9,8], the function should return 1. We can replace -9 with 8 and then 8 will be the only distinct value. 2. Given A =[1,1,1,1,1], the function should return 1. There is nothing that we can do to reduce the number of distinct values. 3. Given A =[11,-2,-1,11,-2,-1], the function should return 2. We can for example replace both values 11 with -1 and -2, which leaves us with two different values. 4. Given A =[10,9,2,2,-3], the function should return 1. We can replace 10,9 and -3 with 2 such that all values are equal to 2. Assume that: N is an integer within the range [1..12]: each element of array A is an integer within the range [-50..50]

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!