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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T09:49:23+00:00 2026-06-05T09:49:23+00:00

I would like to know if the two variants of the code examples below

  • 0

I would like to know if the two variants of the code examples below technically have the same run-time complexity.

For example (and for the sake of making a point, lets say the string’s length is an even number):

//counting how many times char 'c' appear in the string s

String s = "ascdwcccdweccaaa"; //the "array", contain char 'c' 6 times

int counter = 0; //times char 'c' appear in the string

for(int i=1; i <= s.length()/2; i++)
    {
    if(s.charAt(i-1) == 'c')
        counter++;
    if(s.charAt(s.length()-i) == 'c')
        counter++;
    }

Compared to this…

for(int i=0; i < s.length(); i++)
    if(s.charAt(i) == 'c')
        counter++;

The first example uses the index to check from both end and start of the array until it reaches the middle of the array (supposedly O(n/2) )

while the second example is strictly checking all chars in the array from start to end (supposedly O(n))

On the first example i need to use two ifs, while on the second example i need to use one if.

Are these two codes technically identical in their time-complexity? (given the fact i use two ifs when i only pass half the array in the first example, do they “even out”?)

  • 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-05T09:49:24+00:00Added an answer on June 5, 2026 at 9:49 am

    Both of your programs have exactly the same O(n) complexity. In fact O(n/2) is equal to O(n) as it is the same order. But even taking that into account: you have two times less iterations performing two times more work. Total is the same.

    So the programs have the same complexity. However the first one has several disadvantages:

    • it is more complex to read and less clear

    • what about arrays with odd number of elements?

    • JVM might do some fancy optimizations wrt. boundary checking (the VM might not check the boundaries all the time when it discovers you are simply iterating over the whole array). Using this fanciness might confuse the optimizer.

    That being said you made your code harder to read, incorrect and presumably slower – while trying to optimize it.

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

Sidebar

Related Questions

I would like to know the difference between below two values in mysql. show
I would like to know if it is possible to have two CADisplayLink in
I would like to know two things about the following code: HttpWebRequest objRequest =
I would like to know how you can prevent to use of two same
I have two rows of data and I would like to know if it
I have two methods in my NSObject. I would like to know how to
I have two problems regarding Android Services. First, I would like to know which
I would like to know how do I combine the below two checkboxes to
I would like to know if there are any differences in between the two
I would like to know if there is anyway I can compare two columns

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.