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

  • Home
  • SEARCH
  • 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 6018455
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T03:16:41+00:00 2026-05-23T03:16:41+00:00

Given a vector of n elements of type integer, what is the more efficient

  • 0

Given a vector of n elements of type integer, what is the more efficient algorithm that produce the minimum number of transformation step resulting in a vector that have all its elements equals, knowing that :

  • in a single step, you could transfer at most one point from element to its neighbours ([0, 3, 0] -> [1, 2, 0] is ok but not [0, 3, 0] -> [1, 1, 1]).
  • in a single step, an element could receive 2 points : one from its left neighbour and one from the right ([3, 0 , 3] -> [2, 2, 2]).
  • first element and last element have only one neighbour, respectively, the 2nd element and the n-1 element.
  • an element cannot be negative at any step.

Examples :

Given :
 0, 3, 0
Then 2 steps are required :
 1, 2, 0
 1, 1, 1

Given :
 3, 0, 3
Then 1 step is required :
 2, 2, 2

Given :
 4, 0, 0, 0, 4, 0, 0, 0
Then 3 steps are required :
 3, 1, 0, 0, 3, 1, 0, 0
 2, 1, 1, 0, 2, 1, 1, 0
 1, 1, 1; 1, 1, 1, 1, 1

My current algorithm is based on the sums of the integers at each side of an element. But I’m not sure if it produce the minimum steps.

FYI the problem is part of a code contest (created by Criteo http://codeofduty.criteo.com) that is over.

  • 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-05-23T03:16:42+00:00Added an answer on May 23, 2026 at 3:16 am

    Here is a way. You know the sum of the array, so you know the target number in each cell.
    Thus you also know the target sum for each subarray.
    Then iterate through the array and on each step you make a desicion:

    1. Move 1 to the left: if the sum up to the previous element is less then desired.
    2. Move 1 to the right: if the sum up to the current element is more than desired
    3. Don’t do anything: if both of the above are false

    Repeat this until no more changes are made (i.e. you only applied 3 for each of the elements).

        public static int F(int[] ar)
        {
            int iter = -1;
            bool finished = false;
            int total = ar.Sum();
    
            if (ar.Length == 0 || total % ar.Length != 0) return 0; //can't do it
            int target = total / ar.Length;
    
            int sum = 0;
    
            while (!finished)
            {
                iter++;
                finished = true;
                bool canMoveNext = true;
    
                //first element
                if (ar[0] > target)
                {
                    finished = false;
                    ar[0]--;
                    ar[1]++;
    
                    canMoveNext = ar[1] != 1;
                }
    
                sum = ar[0];
                for (int i = 1; i < ar.Length; i++)
                {
                    if (!canMoveNext)
                    {
                        canMoveNext = true;
                        sum += ar[i];
                        continue;
                    }
    
                    if (sum < i * target && ar[i] > 0)
                    {
                        finished = false;
                        ar[i]--;
                        ar[i - 1]++;
                        sum++;
                    }
                    else if (sum + ar[i] > (i + 1) * target && ar[i] > 0) //this can't happen for the last element so we are safe
                    {
                        finished = false;
                        ar[i]--;
                        ar[i + 1]++;
    
                        canMoveNext = ar[i + 1] != 1;
                    }
    
                    sum += ar[i];
                }
            }
    
            return iter;
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a function that processes a given vector, but may also create such
Given a sorted vector with a number of values, as in the following example:
In C++, given vector<T> src, dst , both already sorted, is there a more
I'm trying to write a program in R that when, given a vector, will
Given a large collection (let's call it 'a') of elements of type T (say,
Given a vector with 100 elements, I want to move elements 75 to 100
Given std::vector<CMyClass> objects; CMyClass list[MAX_OBJECT_COUNT]; Is it wise to do this? for(unsigned int i
In R, given a vector casp6 <- c(0.9478638, 0.7477657, 0.9742675, 0.9008372, 0.4873001, 0.5097587, 0.6476510,
Given an STL vector, output only the duplicates in sorted order, e.g., INPUT :
Given a point such as (0, 0, 0) and a vector like (x, y,

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.