Skip to content

Conversation

@Subhosjx
Copy link
Contributor

@Subhosjx Subhosjx commented Oct 28, 2025

PR Title Format: Problem no.Problem name.cpp

Approach

  • A number n is a power of two if:

    • n > 0
    • In binary, it has exactly one set bit.
  • Trick: For powers of two, n & (n - 1) == 0.

Example:

  • n = 8 (1000), n-1 = 7 (0111), n & (n-1) = 0000
  • n = 6 (0110), n-1 = 5 (0101), n & (n-1) = 0100

Intuition

Binary representation of powers of two looks like: 1 (0001), 2 (0010), 4 (0100), 8 (1000), 16 (10000) ...

Each has exactly one set bit.
Therefore, checking (n > 0) && (n & (n-1)) == 0 efficiently verifies this.

Code Solution (C++)

    class Solution {
public:
    bool isPowerOfTwo(int n) {
        if (n <= 0) return false;
        return (n & (n - 1)) == 0;
    }
};
    

Related Issues #109

By submitting this PR, I confirm that:

  • This is my original work not totally AI generated
  • I have tested the solution thoroughly on leetcode
  • I have maintained proper PR description format
  • This is a meaningful contribution, not spam

Summary by Sourcery

New Features:

  • Add isPowerOfTwo function in Solution class utilizing n > 0 and n & (n - 1) == 0 to verify powers of two

@sourcery-ai
Copy link

sourcery-ai bot commented Oct 28, 2025

Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Adds a new Solution method that efficiently determines if an integer is a power of two using a bitwise trick.

Class diagram for the new Solution class with isPowerOfTwo method

classDiagram
class Solution {
  +bool isPowerOfTwo(int n)
}
Loading

Flow diagram for isPowerOfTwo logic

flowchart TD
    A["Input: n"] --> B["n <= 0?"]
    B -- Yes --> C["Return false"]
    B -- No --> D["Compute n & (n-1)"]
    D --> E["Result == 0?"]
    E -- Yes --> F["Return true"]
    E -- No --> G["Return false"]
Loading

File-Level Changes

Change Details Files
Implement isPowerOfTwo method with bitwise check
  • Created Solution class with public isPowerOfTwo(int) signature
  • Added guard clause returning false for non-positive inputs
  • Added (n & (n - 1)) == 0 check to verify single set bit
231. Power of Two.cpp

Possibly linked issues

  • 112.PathSum.cpp #231: The PR implements the exact solution for the 'Power of Two' problem described in the issue.

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey there - I've reviewed your changes and they look great!


Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@SjxSubham SjxSubham linked an issue Oct 28, 2025 that may be closed by this pull request
@SjxSubham SjxSubham changed the title Implement isPowerOfTwo function in Solution class 231. Power of Two Oct 28, 2025
@SjxSubham SjxSubham added the hacktoberest-accepted hacktoberfest-accepted label Oct 28, 2025
@SjxSubham SjxSubham merged commit 553e9ff into SjxSubham:main Oct 28, 2025
2 checks passed
@github-actions
Copy link

🎉 Congrats on getting your PR merged in, @Subhosjx! 🙌🏼

Thanks for your contribution every effort helps improve the project.

Looking forward to seeing more from you! 🥳✨

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

hacktoberest-accepted hacktoberfest-accepted

Projects

None yet

Development

Successfully merging this pull request may close these issues.

231. Power of Two

2 participants