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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T04:28:19+00:00 2026-06-09T04:28:19+00:00

In a program I’m writing in an unnamed language, I have a block of

  • 0

In a program I’m writing in an unnamed language, I have a block of text for which the width is unknown, and all I know is the maximum width that this block of text could be. Given this information, I need to find out the smallest possible width that this text could be (assume that I can’t use the metrics of the characters / glyphs or the character count). So far I just have a brute force solution which looks like follows:

for (int i = .1; i < maxTextWidth; i += .1)
{
       if (textFitsInGivenWidth(text, i))
       {
             textWidth = i;
             break;
       }
}

I’d like to try and optimize this as much as I can. My first thought was to use a binary search, but I’m having trouble implementing this in the proper way (and am not sure if it’s even possible). Does anyone have any suggestions on what I could do here to improve the run time using only what I’ve given in the above 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-09T04:28:21+00:00Added an answer on June 9, 2026 at 4:28 am

    Binary Search is the answer indeed.

    http://en.wikipedia.org/wiki/Binary_search_algorithm

    for integer binary search, it can be:

    minW=0, maxW=maxTextWidth
    while(minW<=maxW){
    
      mid=(minW+maxW)/2;
      if (textFitsInGivenWidth(text, mid)){
        maxW=mid-1;
      }else{
        minW=mid+1;
      }
    }
    textWidth=minW
    

    The idea is, if you have textFitsInGivenWidth(text, mid) == True,

    then you must have textFitsInGivenWidth(text, i) == True for all i>=mid,

    and if it’s False, then you have textFitsInGivenWidth(text, i) == False for all i<=mid

    so each time we check the middle of the interval to be checked, and reduce the interval into half . The time is O(logN), in which N=maxTextWidth

    update: for float support, see the example below :

    float minW=0, maxW=maxTextWidth
    while(1){
      if (maxW-minW<0.05)
        break;
      float mid=(minW+maxW)/2;
      if (textFitsInGivenWidth(text, mid)){
        maxW=mid;
      }else{
        minW=mid;
      }
    }
    
    textWidth=minW
    

    and to get a precision of .1, simply change the last line to :

    textWidth=int(minW*10)/10.0
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Program Details: I am writing a program for python that will need to look
Program that calculates and shows the value of (2 to the 10th power) This
program to output all 4 digit numbers where no prior digit is greater than
My program has 3 text fields, Title, Website, and PictureURL. When I click the
My program is a game that uses RMI to allow users to connect to
I program in Java and have been trying to understand exactly what operator overloading
The program that I am working on takes a file and parses it line
// Program to print simple text on a Printer import javax.swing.*; import java.awt.*; import
// Program to print simple text on a Printer import javax.swing.*; import java.awt.*; import
Program : List all C files in the current folder using execlp() system call:

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.