Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Power of two problem #144

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions javaProblems/problem231/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class Solution {
public boolean solve(int n) {
while (n>1 && n%2==0){
n = n/2;
}
return n==1;
}
}
26 changes: 26 additions & 0 deletions javaProblems/problem231/Test.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import java.util.logging.Level;
import java.util.logging.Logger;

public class Test {
public static void main(String[] args) throws Exception {
Logger logger = Logger.getLogger(Logger.GLOBAL_LOGGER_NAME);
Solution solveSolution = new Solution();

int arg = 16;
int actualAns = true;

try {
int ans = solveSolution.solve(arg);
} catch (Exception e) {
logger.log(Level.SEVERE, "Runtime error");
System.exit(1);
throw new Error("Runtime Error");
}
if (actualAns != ans) {
logger.log(Level.SEVERE, "Wrong solution!");
System.exit(1);
throw new Exception("Wrong solution!");
}
System.out.print("Testcase passed!");
}
}
10 changes: 10 additions & 0 deletions javaProblems/problem231/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# 231.Power of Two
The code determine if a given integer n is a power of two.

```
# Example Input
n = 16

# Example Output
true
```