Блог пользователя C_o_d_e__

Автор C_o_d_e__, история, 5 месяцев назад, По-английски

Find maximum length of subsequence of an array where odd and even index of subsequence having same element in ### o(n*n) for eg. array=[1,2,8,1,2,8,2] maximum length of subsequence if 2,8,2,8,2

  • Проголосовать: нравится
  • +3
  • Проголосовать: не нравится

»
5 месяцев назад, # |
Rev. 5   Проголосовать: нравится 0 Проголосовать: не нравится

I think this can be solved using : create two buckets(vars) and xor the odd indices and even indices. starting from 0 and 1 , move to next set of elements and again xor the odd with the odd bucket and even with the ven bucket and if any of them doesnt turn to zero or the same number remove the previous element and put in new element. tc: o(n) my explanation is bit vague but ill try to dry run on your example

Dry Run
»
5 месяцев назад, # |
Rev. 4   Проголосовать: нравится 0 Проголосовать: не нравится
void solve(int t)
{  
    int n;
    cin >> n;
    ia(a,n);
    int ab = a[0];
    int bb = a[1];
    int count = 2;
    int mc =2;
    //assuming n>=2
    f(i,n){
        if(i==0||i==1) continue;
        else{
            if(i%2==0){
              

                if((ab^a[i])==0 )
                {   
                    count ++;
                    mc = max(mc ,count);
                }
                else{
                    mc = max(mc ,count );
                    count = 2;
                    ab=a[i];
                }
                
            }
            else{
                //xor with bucket b
                 if((bb^a[i])==0 )
                {
                    count ++;
                    mc = max(mc ,count );
                }
                else{
                    mc = max(mc ,count );
                    count = 2;
                    bb=a[i];
                }
            }
        }
        debug(mc)

    }
    cout << mc << endl;
   


}
»
5 месяцев назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

I will share my solution of complexity O(N^2) assuming each element is from 0 to n — 1 inclusive.

Considering the case when the odd and even positions have equal values, we can say that the answer is atleast equal to the frequency of the most frequent element.

Now the case, when the odd and even position elements have different values, we have to iterate over all possible values at the odd index of the sub-sequence in the array (that is from 0 to n — 1) and calculate the best possible length with such an element at the odd positions as follows:

We need to find optimal even position element for every possible odd position element.

Let this odd position element be called o and the optimal even position element for o be e.

Please try to visualise how to find e for a given o:

Colour all occurrences of o in the array to yellow. To maintain invariance we will have to add two elements in the array one at beginning and other at end that is at positions -1 and n (0-based) which will be marked yellow irrespective.

Now consider segments between these marked yellow cells. Colour all the first occurrences of each element in every segment as red.

The optimal even position element i.e. e is the value which is coloured red the most. And the length of the sub-sequence is the length of the alternating cells of o and e.

We find optimal length of the sub-sequence for each o(0 to n — 1) by the above algorithm.

Answer is the maximum length amoung all such possible lengths.

My implementation