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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T02:48:45+00:00 2026-05-26T02:48:45+00:00

I have a NSMutableArray that has 5 values initially set to zero. It contains

  • 0

I have a NSMutableArray that has 5 values initially set to zero. It contains the x-coordinate of elements on the screen and is updated every 1/60th of a second. The position of each item is constantly changing. There is a maximum of 5 items on the screen and a minimum of 1 at any time.

Each item in the array will be in the range 0 – 480 (height of iphone screen). An example array at a given time would be:

{123,450,0,0,0}

Which might then change to:

{150,320,90,0,0}

I need a quick way to find a position that is not near any position in the array. This can be a position that is not within a set range around each value (for example the value is not within 50 of any items in the array) or the position with the most distance either side of it.

Obviously if finding a position within a certain range is not possible, the best solution should be picked.

It needs to be quick as a new item is being added to the screen so any delay in picking a new position will slow down the game ticker – so a while loop is not preferable.

Hopefully theres a simple mathematical method in objective-c that can sort this. I really am stuck as to how I should achieve this. Any help is MUCH appreciated.

  • 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-26T02:48:46+00:00Added an answer on May 26, 2026 at 2:48 am

    Let me rephrase this question a bit. You have points p1,p2,p3,p4,p5 such that

    0 <= p_i <= 480
    

    You want to find a point x such that

    0 <= x <= 480
    

    that maximizes the function

    min_{x} ( |p1-x| + |p2-x| + |p3-x| + |p4-x| + |p5-x| )
    

    If this is your aim (and it isn’t clear to me that it is or should be), then you can solve this problem by checking which of the following potential x values maximizes the distance:

    0 , p1/2 , p1 + (p2-p1)/2 , p2 + (p3-p2)/2 , ... , p5 + (480-p5)/2
    

    this assumes 0 <= p1 <= p2 <= p3 <= p4 <= p5 <= 480. Whichever of the difference terms is larger is the answer you should select.

    For example, for

    {123,450,0,0,0}
    

    the answer is 123 + (450-123)/2.

    For

    {150,320,90,0,0} 
    

    the answer is 150 + (320-150)/2

    To code this in Objective-C, you need to have a function that returns the index of the maximum entry of an array. Take the input to be the p1,p2,...,p5. Sort these into increasing order, append 0 on the left and 480 on the right. Then make a new array of length one less that gives successive differences, e.g. {p1-p0, p2-p1, ..., p6-p5} where p0 = 0 and p6 = 480. Finally, get the index of the maximum of this new array, call it i, and return the optimal position p_i + (p(i+1)-p_i)/2.

    Example:

    input: {150,320,90,0,0}

    rearrange to {0 , 0 , 0 , 90 , 150 , 320 , 480}

    difference array {0 , 0 , 90 , 60 , 170 , 160}

    maximum at index 4

    answer is 150 + (320-150)/2

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class that has a method: -(NSInteger) getCityCountForState:(NSString *)state CityArray:(NSMutableArray *)cityArray {
I have an Obj-C 2.0 class that has an NSMutableArray property. If I use
I have a NSMutableArray that has custom objects in it. Each object does have
I have an app that has a tab bar, each tab contains a separate
I have an object that has an NSMutableArray of custom UIViews, a UIImage, and
Good day. I have an NSMutableArray that contains references to objects. I need to
I have an NSMutableArray that is populated with objects of strings. For simplicity sake
I have a NSMutableArray that I need to search for a string and return
Newbie question. I have a NSMutableArray that holds multiple objects (objects that stores Bezier
I have a class that subclasses NSMutableArray. I init it using: MyClass class =

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.