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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T17:25:54+00:00 2026-06-09T17:25:54+00:00

I am taking up algorithm course on coursera,and I am stuck on this particular

  • 0

I am taking up algorithm course on coursera,and I am stuck on this particular problem. I am supposed to find the time complexity of this code.

int sum = 0

 for (int i = 1; i <= N*N; i = i*2)

  {
     for (int j = 0; j < i; j++)
           sum++; 
  }

I checked it in eclipse itself, for any value of N the number of times sum statement is executed is less than N

final value of sum:
for N=8 sum=3 
for N=16 sum=7 
for N=100000 sum=511

so the time complexity should be less than N
but the answer that is given is N raised to the power 2, How is it possible?

What I have done so far:

the first loop will run log(N^ 2) times, and consequently second loop will be execute 1,2,3.. 2 logN

  • 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-09T17:25:55+00:00Added an answer on June 9, 2026 at 5:25 pm

    The first inner loop will be 1 + 2 + 4 + 8 .. 2^M where 2^M is <= N * N.

    The sum of powers of 2 up to N * N is approximately 2 * N * N or O(N ^ 2)

    Note: When N=100000 , N*N will overflow so its result is misleading. If you consider overflow to be part of the problem, the sum is pretty random for large numbers so you could argue its O(1), i.e. if N=2^15, N^2 = 2^30 and the sum will be Integer.MAX_VALUE. No higher value of N will give a higher sum.

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

Sidebar

Related Questions

Taking the jQuery framework for example, if you run code like this: $(document).ready(function init()
This is a school-related question, although not exactly homework. I'm taking an algorithms course,
I'm taking a data structures course, and we're using Data Structures and Algorithm Analysis
I have some issues resolving an algorithm used to find all posible combinations taking
I'm taking an algorithm course at the university, and for one of my projects
http://www.cplusplus.com/reference/algorithm/for_each/ Unary function taking an element in the range as argument. This can either
for an algorithm time complexity analysis i need to know what is the result
I'm using the Levenshtein algorithm to find the similarity between two strings. This is
Taking a rails tutorial, and I've run into the following problem that I'm having
Taking this file as an example, I'm trying to read the data in a

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.