yashsaha555's blog

By yashsaha555, history, 23 months ago, In English

A teacher takes an array of numbers and asks the students to perform the following operation: pick two adjacent positive numbers, ai and ai+1, and replace the two numbers with either (ai%(ai+1)) or (ai+1)%ai ,where x%y denotes x modulo y. Thus, after each operation, the array's length decreases by 1.

The task is to find the minimum possible length of the array, which can be achieved by performing the above operation any number of times

Example

Consider the array's length to be n=4 and the array of numbers to be arr=[1,1,2,3]. The following sequence of operations can be performed (considering 1-based indexing):

  1. arr=[1, 1, 2, 3]: Choose i=3, thus arr[i]= 2 and arr[i+1]=3. We get the new value to be given by 2%3

=2 or 3 % 2=1. The resulting array can thus be [1, 1, 2] or [1, 1. 1]. We consider the former one to minimize the array length.

  1. arr=[1,1,2]: Choose i= 2, thus arr[i]=1 and arr[i+1]=2. We can get the new array as [1, 1].

3 arr=[1, 1]: Choose i=1 to get the array [0].

Thus the minimum possible length for the above array would be 1.

Could someone please help me in solving this?

  • Vote: I like it
  • +5
  • Vote: I do not like it

| Write comment?
»
23 months ago, # |
Rev. 2   Vote: I like it 0 Vote: I do not like it

UPD: Solution invalid, see replies

  • »
    »
    23 months ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    what if the array elements are [2,2,2,2]. After the operations, we end up getting [0,0]. So in this case the answer is 2. One thing to be noted is that we can perform the operations only with positive number pairs (as mentioned in the question). Considering this, would be please explain the solution again?

    • »
      »
      »
      23 months ago, # ^ |
      Rev. 2   Vote: I like it 0 Vote: I do not like it

      I believe I have a solution which works in O(n) time.

      Let m be the smallest number in the array. First, consider the case when there is only one m. In this case, it is very easy to see how to remove all other numbers: you simply always choose a pair that contains m and some other number b and replace it with m % b = m (since b > m).

      Second, what do we do when there are multiple minimums? We try to somehow get an integer that is less than m. If we manage to do that, we confidently print '1', because of the reasons stated above. Turns out, we can do that if and only if there is a number somewhere in the array that is not a multiple of m. I believe it is best to show the idea on an example.

      Consider a = [6, 3, 3, 3, 6, 9, 11, 12]. In this case m = 3, and every other number is a multiple of m, except for a[7] = 11. Once we found an a[i] such that a[i] % m != 0, we can remove 6 and 9 using the same trick, and we will have a = [6, 3, 3, 3, 11, 12]. Notice that a % b < b. So we replace [3, 11] with 11 % 3 = 2 < 3 and have a = [6, 3, 3, 2, 12]. Obviously, after some operations we will be able to reduce the length to 1 (a = [2])

      If all of the numbers are multiples of m, then we can't perform any sequence of operations that creates a number less than m (not counting zeros). So we just erase all numbers greater than m and turn as many pairs as we can to zeros. For example, if a = [6, 3, 3, 3, 6, 9, 12, 12], then we can make a = [3, 3, 3], and then a = [0, 3]. The answer is 2.

      To recap:

      1. Find m = min(a).
      2. How many m's are there in a? Just one — the answer is '1'. Else:
      3. Is there a number a[i] such that a[i] % m != 0? Yes — the answer is '1'. Else:
      4. Let M be the count of m's in a. The answer is ceil(M / 2).
      • »
        »
        »
        »
        23 months ago, # ^ |
          Vote: I like it 0 Vote: I do not like it

        Thanks a lot for the solution. Also the first line of the problem says that pick two adjacent 'positive numbers'. So we exclude zeros in operations.

      • »
        »
        »
        »
        23 months ago, # ^ |
          Vote: I like it 0 Vote: I do not like it

        The answer is 2.

        In case of $$$a = [6, 3, 3, 3, 6, 9, 11, 12]$$$ we can choose firstly last pair $$$[11, 12]$$$ and then replace it with $$$1 = 12 \% 11$$$. Now, when we have $$$1$$$ in the array, we can eliminate all other elements because $$$1 \% a_i = 1$$$ if $$$a_i \gt 1$$$. So the answer should be $$$1$$$.

        • »
          »
          »
          »
          »
          23 months ago, # ^ |
            Vote: I like it 0 Vote: I do not like it

          The case in which a = [6, 3, 3, 3, 6, 9, 11, 12] was handled one paragraph earlier. The answer would be 2 if a = [6, 3, 3, 3, 6, 9, 12, 12].

  • »
    »
    9 months ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    IMO Solution is Correct. Can you tell on which test case it fails ?

    • »
      »
      »
      9 months ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      not fail I think he miss read the first line of the problem

»
23 months ago, # |
  Vote: I like it +5 Vote: I do not like it

i think to do modulo you use %