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 8898341
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T00:35:25+00:00 2026-06-15T00:35:25+00:00

I have a sorted array of integers of size n. These values are not

  • 0

I have a sorted array of integers of size n. These values are not unique. What I need to do is
: Given a B, I need to find an i<A[n] such that the sum of |A[j:1 to n]-i| is lesser than B and to that particular sum contribute the biggest number of A[j]s. I have some ideas but I can’t seem to find anything better from the naive n*B and n*n algorithm. Any ideas about O(nlogn) or O(n) ?
For example: Imagine

A[n] = 1 2 10 10 12 14 and B<7 then the best i is 12 cause I achieve having 4 A[j]s contribute to my sum. 10 and 11 are also equally good i’s cause if i=10 I got 10 – 10 + 10 – 10 +12-10 + 14-10 = 6<7

  • 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-15T00:35:26+00:00Added an answer on June 15, 2026 at 12:35 am

    I think you can do it in O(n) using these three tricks:

    CUMULATIVE SUM

    Precompute an array C[k] that stores sum(A[0:k]).
    This can be done recursively via C[k]=C[k-1]+A[k] in time O(n).
    The benefit of this array is that you can then compute sum(A[a:b]) via C[b]-C[a-1].

    BEST MIDPOINT

    Because your elements are sorted, then it is easy to compute the best i to minimise the sum of absolute values. In fact, the best i will always be given by the middle entry.
    If the length of the list is even, then all values of i between the two central elements will always give the minimum absolute value.

    e.g. for your list 10,10,12,14 the central elements are 10 and 12, so any value for i between 10 and 12 will minimise the sum.

    ITERATIVE SEARCH

    You can now scan over the elements a single time to find the best value.

    1. Init s=0,e=0
    2. if the score for A[s:e] is less than B increase e by 1
    3. else increase s by 1
    4. if e<n return to step 2
    

    Keep track of the largest value for e-s seen which has a score < B and this is your answer.

    This loop can go around at most 2n times so it is O(n).

    The score for A[s:e] is given by sum |A[s:e]-A[(s+e)/2]|.

    Let m=(s+e)/2.

    score = sum |A[s:e]-A[(s+e)/2]| 
    = sum |A[s:e]-A[m]|
    = sum (A[m]-A[s:m]) + sum (A[m+1:e]-A[m])
    = (m-s+1)*A[m]-sum(A[s:m]) + sum(A[m+1:e])-(e-m)*A[m]
    

    and we can compute the sums in this expression using the precomputed array C[k].

    EDIT

    If the endpoint must always be n, then you can use this alternative algorithm:

    1. Init s=0,e=n
    2. while the score for A[s:e] is greater than B, increase s by 1
    

    PYTHON CODE

    Here is a python implementation of the algorithm:

    def fast(A,B):
        C=[]
        t=0
        for a in A:
            t+=a
            C.append(t)
    
        def fastsum(s,e):
            if s==0:
                return C[e]
            else:
                return C[e]-C[s-1]
    
        def fastscore(s,e):
            m=(s+e)//2
            return (m-s+1)*A[m]-fastsum(s,m)+fastsum(m+1,e)-(e-m)*A[m]
    
        s=0
        e=0
        best=-1
        while e<len(A):
            if fastscore(s,e)<B:
                best=max(best,e-s+1)
                e+=1
            elif s==e:
                e+=1
            else:
                s+=1
        return best
    
    print fast([1,2,10,10,12,14],7)
    # this returns 4, as the 4 elements 10,10,12,14 can be chosen
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an array of values which is almost, but not quite sorted, with
I have a sorted array of doubles (latitudes actually) that relatively uniformally spread out
1-)For sorted array I have used Binary Search. We know that the worst case
You are given two sorted integer arrays, which have some common integers. Any common
Suppose I have a sorted array of integers int[] , and I want to
I have a sorted array of 5000 integers. How fast can I tell if
I have a simple one dimmensional array of integer values that represent a physical
If I have a sorted array, how do I find the sequential numbers? Btw,
Given an array (lets assume of non negative integers) we are required to find
Given a sorted array of n integers, like the following: ary = [3, 5,

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.