Thursday, October 21, 2021

Leet Code Java Solution - First Bad Version

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
My Code:

/* The isBadVersion API is defined in the parent class VersionControl.
      boolean isBadVersion(int version); */

public class Solution extends VersionControl {
    public int firstBadVersion(int n) {
        int i=0, j =n, mid=0;
        while(i<=j){
            mid = i + (j-i) / 2;
            if(!isBadVersion(mid)) i = mid+1;
            else j = mid-1;
        }
        if(!isBadVersion(mid)) return mid+1;
        return mid;
    }
}

Copyright © saras ojha

Leet Code Solution JAVA - Guess Number Higher or Lower

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 3 possible results:

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

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

Example 4:

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

 

Constraints:

  • 1 <= n <= 231 - 1
  • 1 <= pick <= n

My Code:

/** 
 * Forward declaration of guess API.
 * @param  num   your guess
 * @return      -1 if num is lower than the guess number
 *       1 if num is higher than the guess number
 *               otherwise return 0
 * int guess(int num);
 */

public class Solution extends GuessGame {
    public int guessNumber(int num) {
        int i=1, j=num, mid=1;
        
        while(i<=j){
            mid = i + (j-i) / 2;
            int result = guess(mid);
            if(result==1) i = mid+1;
            else if (result==-1) j = mid-1;
            else return mid;
        }
        
   
        return mid;
    }
}
Copyright © saras ojha

Leet Code JAVA Solution - Angle Between Hands of a Clock

Given two numbers, hour and minutes. Return the smaller angle (in degrees) formed between the hour and the minute hand.

 

Example 1:

Input: hour = 12, minutes = 30
Output: 165

Example 2:

Input: hour = 3, minutes = 30
Output: 75

Example 3:

Input: hour = 3, minutes = 15
Output: 7.5

Example 4:

Input: hour = 4, minutes = 50
Output: 155

Example 5:

Input: hour = 12, minutes = 0
Output: 0

 

Constraints:

  • 1 <= hour <= 12
  • 0 <= minutes <= 59
  • Answers within 10^-5 of the actual value will be accepted as correct.

My Code:

class Solution {
    public double angleClock(int hour, int minutes) {
        double angle = 5.5*(minutes + (hour)*60);
          if (angle < 360 && angle > 180){
           return (360 -angle);
        }
        else if (angle>360){
            double n = angle%360;
            if(n>180){
                return (360 - n);
            }
            else{
                return n;
            }
        }
        else if(angle<0){
            return (-angle);
        }
       return 0; 
    }
    
}


Copyright © saras ojha

Monday, October 18, 2021

Leet Code Solution Java - Two Sum

Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.

You may assume that each input would have exactly one solution, and you may not use the same element twice.

You can return the answer in any order.

Example 1:

Input: nums = [2,7,11,15], target = 9
Output: [0,1]
Output: Because nums[0] + nums[1] == 9, we return [0, 1].

Example 2:

Input: nums = [3,2,4], target = 6
Output: [1,2]
Example 3:

Input: nums = [3,3], target = 6 
Output: [0,1]

Code:

class Solution {
    public int[] twoSum(int[] nums, int target) {
        int [] arr = new int [2];
        
        for (int i=0; i<nums.length;i++){
            for(int j=i+1;j<nums.length; j++){
                int add = nums[i]+nums[j];
                if(add==target){
                   arr[0] = i;
                   arr[1] = j;
                }
                }
                }
        return arr;
        
    }
Copyright © saras ojha