Skip to content
21 changes: 21 additions & 0 deletions Java/167. Two Sum II - Input Array Is Sorted/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
167. Two Sum II - Input Array Is Sorted (question)

Given a 1-indexed array of integers numbers that is already sorted in non-decreasing order, find two numbers such that they add up to a specific target number. Let these two numbers be numbers[index1] and numbers[index2] where 1 <= index1 < index2 < numbers.length.

Return the indices of the two numbers, index1 and index2, added by one as an integer array [index1, index2] of length 2.

The tests are generated such that there is exactly one solution. You may not use the same element twice.

Your solution must use only constant extra space.

Example 1:

Input: numbers = [2,7,11,15], target = 9 Output: [1,2] Explanation: The sum of 2 and 7 is 9. Therefore, index1 = 1, index2 = 2. We return [1, 2]. Example 2:

Input: numbers = [2,3,4], target = 6 Output: [1,3] Explanation: The sum of 2 and 4 is 6. Therefore index1 = 1, index2 = 3. We return [1, 3]. Example 3:

Input: numbers = [-1,0], target = -1 Output: [1,2] Explanation: The sum of -1 and 0 is -1. Therefore index1 = 1, index2 = 2. We return [1, 2].

Constraints:

2 <= numbers.length <= 3 * 104 -1000 <= numbers[i] <= 1000 numbers is sorted in non-decreasing order. -1000 <= target <= 1000 The tests are generated such that there is exactly one solution.
19 changes: 19 additions & 0 deletions Java/167. Two Sum II - Input Array Is Sorted/sol.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class Solution {
public int[] twoSum(int[] numbers, int target) {
//start=left, end=right
int start=0;
int end=numbers.length-1;

while(start<=end){
int sum=numbers[start]+numbers[end];
if(sum==target){
return new int[]{start+1,end+1};
}else if(sum<target){
start++;
}else{
end--;
}
}
return new int[]{-1,-1};
}
}
19 changes: 19 additions & 0 deletions Java/278. First Bad Version/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
278. First Bad Version (question)

You are a product manager and currently leading a team to develop a new product. Unfortunately, the latest version of your product fails the quality check. Since each version is developed based on the previous version, all the versions after a bad version are also bad.

Suppose you have n versions [1, 2, ..., n] and you want to find out the first bad one, which causes all the following ones to be bad.

You are given an API bool isBadVersion(version) which returns whether version is bad. Implement a function to find the first bad version. You should minimize the number of calls to the API.

Example 1:

Input: n = 5, bad = 4 Output: 4 Explanation: call isBadVersion(3) -> false call isBadVersion(5) -> true call isBadVersion(4) -> true Then 4 is the first bad version.

Example 2:

Input: n = 1, bad = 1 Output: 1

Constraints:

1 <= bad <= n <= 231 - 1
17 changes: 17 additions & 0 deletions Java/278. First Bad Version/sol.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
public class Solution extends VersionControl {
public int firstBadVersion(int n) {
int start=1;
int end=n;
int ans=0;
while(start<=end){
int mid=start+ (end-start)/2;
if(isBadVersion(mid)==false){
start=mid+1;
}else{
ans=mid;
end=mid-1;
}
}
return ans;
}
}
17 changes: 17 additions & 0 deletions Java/367. Valid Perfect Square/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
367. Valid Perfect Square (question)

Given a positive integer num, return true if num is a perfect square or false otherwise.

A perfect square is an integer that is the square of an integer. In other words, it is the product of some integer with itself.

You must not use any built-in library function, such as sqrt.

Example 1:

Input: num = 16 Output: true Explanation: We return true because 4 * 4 = 16 and 4 is an integer. Example 2:

Input: num = 14 Output: false Explanation: We return false because 3.742 * 3.742 = 14 and 3.742 is not an integer.

Constraints:

1 <= num <= 231 - 1
23 changes: 23 additions & 0 deletions Java/367. Valid Perfect Square/sol.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
class Solution {
public boolean isPerfectSquare(int num) {
if(num==1){
return true;
}
long start=1;
long end=num/2;

while(start<=end){
long mid=start+(end-start)/2;
long square=mid*mid;

if(square==num){
return true;
}else if(square<num){
start=mid+1;
}else{
end=mid-1;
}
}
return false;
}
}
23 changes: 23 additions & 0 deletions Java/374. Guess Number Higher or Lower/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
374. Guess Number Higher or Lower (question)

We are playing the Guess Game. The game is as follows:

I pick a number from 1 to n. You have to guess which number I picked.

Every time you guess wrong, I will tell you whether the number I picked is higher or lower than your guess.

You call a pre-defined API int guess(int num), which returns three possible results:

-1: Your guess is higher than the number I picked (i.e. num > pick). 1: Your guess is lower than the number I picked (i.e. num < pick). 0: your guess is equal to the number I picked (i.e. num == pick). Return the number that I picked.

Example 1:

Input: n = 10, pick = 6 Output: 6 Example 2:

Input: n = 1, pick = 1 Output: 1 Example 3:

Input: n = 2, pick = 1 Output: 1

Constraints:

1 <= n <= 231 - 1 1 <= pick <= n
18 changes: 18 additions & 0 deletions Java/374. Guess Number Higher or Lower/sol.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
public class Solution extends GuessGame {
public int guessNumber(int n) {
//start=s, end=e
int s=1;
int e=n-1;
while(s<=e){
int m=s + (e-s)/2;
if(guess(m)==0){
return m;
}else if(guess(m)== -1){
e=m-1;
}else{
s=m+1;
}
}
return n;
}
}
16 changes: 16 additions & 0 deletions Java/69. Sqrt(x)/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
69. Sqrt(x) (question)

Given a non-negative integer x, return the square root of x rounded down to the nearest integer. The returned integer should be non-negative as well.
You must not use any built-in exponent function or operator.

For example, do not use pow(x, 0.5) in c++ or x ** 0.5 in python.

Example 1:

Input: x = 4 Output: 2 Explanation: The square root of 4 is 2, so we return 2. Example 2:

Input: x = 8 Output: 2 Explanation: The square root of 8 is 2.82842..., and since we round it down to the nearest integer, 2 is returned.

Constraints:

0 <= x <= 231 - 1
24 changes: 24 additions & 0 deletions Java/69. Sqrt(x)/sol.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
class Solution {
public int mySqrt(int x) {
if(x==0){
return 0;
}
int start=1;
int end=x;
int result=0;
while(start<=end){
int mid=start+(end-start)/2;
long square=(long)mid*mid;

if(square == x){
return mid;
}else if(square < x){
start=mid+1;
result = mid;
}else{
end=mid -1;
}
}
return result;
}
}