Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7926537
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T18:48:56+00:00 2026-06-03T18:48:56+00:00

Given an array A with n integers. In one turn one can apply the

  • 0

Given an array A with n
integers. In one turn one can apply the
following operation to any consecutive
subarray A[l..r] : assign to all A i (l <= i <= r)
median of subarray A[l..r] .
Let max be the maximum integer of A .
We want to know the minimum
number of operations needed to change A
to an array of n integers each with value
max.
For example, let A = [1, 2, 3] . We want to change it to [3, 3, 3] . We
can do this in two operations, first for
subarray A[2..3] (after that A equals to [1,
3, 3] ), then operation to A[1..3] .
Also,median is defined for some array A as follows. Let B be the same
array A , but sorted in non-decreasing
order. Median of A is B m (1-based
indexing), where m equals to (n div 2)+1 .
Here ‘div’ is an integer division operation.
So, for a sorted array with 5 elements,
median is the 3rd element and for a sorted
array with 6 elements, it is the 4th element.

Since the maximum value of N is 30.I thought of brute forcing the result
could there be a better solution.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T18:48:57+00:00Added an answer on June 3, 2026 at 6:48 pm

    This is the problem from codechef Long Contest.Since the contest is already over,so awkwardiom ,i am pasting the problem setter approach (Source : CC Contest Editorial Page).

    “Any state of the array can be represented as a binary mask with each bit 1 means that corresponding number is equal to the max and 0 otherwise. You can run DP with state R[mask] and O(n) transits. You can proof (or just believe) that the number of statest will be not big, of course if you run good DP. The state of our DP will be the mask of numbers that are equal to max. Of course, it makes sense to use operation only for such subarray [l; r] that number of 1-bits is at least as much as number of 0-bits in submask [l; r], because otherwise nothing will change. Also you should notice that if the left bound of your operation is l it is good to make operation only with the maximal possible r (this gives number of transits equal to O(n)). It was also useful for C++ coders to use map structure to represent all states.”

    The C/C++ Code is::

    #include <cstdio>
    #include <iostream>
    using namespace std;
    
    int bc[1<<15];
    const int M = (1<<15) - 1;
    
    void setMin(int& ret, int c)
    {
        if(c < ret) ret = c;
    }
    
    void doit(int n, int mask, int currentSteps, int& currentBest)
    {
        int numMax = bc[mask>>15] + bc[mask&M];
        if(numMax == n) {
            setMin(currentBest, currentSteps);
            return;
        }
        if(currentSteps + 1 >= currentBest)
            return;
        if(currentSteps + 2 >= currentBest)
        {
            if(numMax * 2 >= n) {
                setMin(currentBest, 1 + currentSteps);
            }
            return;    
        }  
    
        if(numMax < (1<<currentSteps)) return;
    
        for(int i=0;i<n;i++) 
        {
            int a = 0, b = 0;
            int c = mask;
            for(int j=i;j<n;j++)
            {
                c |= (1<<j);
                if(mask&(1<<j)) b++;
                else a++;
                if(b >= a) {
                    doit(n, c, currentSteps + 1, currentBest);
                }
            }
        }
    }
    
    int v[32];
    void solveCase() {
        int n;
        scanf(" %d", &n);
        int maxElement = 0;
        for(int i=0;i<n;i++) {
            scanf(" %d", v+i);
            if(v[i] > maxElement) maxElement = v[i];
        }
        int mask = 0;
        for(int i=0;i<n;i++) if(v[i] == maxElement) mask |= (1<<i);
        int ret = 0, p = 1;
        while(p < n) {
            ret++;
            p *= 2;
        }
        doit(n, mask, 0, ret);
        printf("%d\n",ret);
    }
    
    main() {
        for(int i=0;i<(1<<15);i++) {
            bc[i] = bc[i>>1] + (i&1);
        }
        int cases;
        scanf(" %d",&cases);
        while(cases--) solveCase();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given an array A of integers, find any 3 of them that sum to
Given an array, A, containing, in some order, all but one of the k-bit
Consider this problem: You are given an array containing positive integers. All the integers
Given an array of n integers, where one element appears more than n/2 times.
Is there any way one can apply a function with signature bool IsOdd(int number);
This problem is taken from interviewstreet.com Given array of integers Y=y1,...,yn, we have n
I'm simplifying a larger complex problem with the following... Given three arrays of integers,
Given an unsorted integer array, and without making any assumptions on the numbers in
// given following array: $data = array( 0=>array( data=>object1, col=>array( 0=>array( data=>object2, col=>array( 0=>array(
Given an array. How can we find sum of elements in index interval (i,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.