Sunday, October 29, 2023

Worksheets

We have created some free worksheets for students of IB (MYP 4 and MYP 5), IB AA HL, Cambridge IGCSE, and AS and A Level students.

Worksheets are documented topic wise and can be accessed via left hand navigation.
You can post your answers in the comment section and I will respond to it.

Thank you. 

Thursday, October 21, 2021

Leet Code Java Solution - Container with Most Water


Given n non-negative integers a1, a2, ..., an , where each represents a point at coordinate (i, ai)n vertical lines are drawn such that the two endpoints of the line i is at (i, ai) and (i, 0). Find two lines, which, together with the x-axis forms a container, such that the container contains the most water.

Notice that you may not slant the container.

 

Example 1:

Input: height = [1,8,6,2,5,4,8,3,7]
Output: 49
Explanation: The above vertical lines are represented by array [1,8,6,2,5,4,8,3,7]. In this case, the max area of water (blue section) the container can contain is 49.

Example 2:

Input: height = [1,1]
Output: 1

Example 3:

Input: height = [4,3,2,1,4]
Output: 16

Example 4:

Input: height = [1,2,1]
Output: 2

 

Constraints:

  • n == height.length
  • 2 <= n <= 105
  • 0 <= height[i] <= 104
My Code:

class Solution {
    public int maxArea(int[] height) {
        
        int maxWater = 0;
        int left = 0;
        int right=height.length -1;
        while (left<right){
            maxWater = Math.max(maxWater, (right-left)*Math.min(height[left],height[right]));
            if (height[left]<height[right]) left ++;
            else right --;
        }
        return maxWater;
    }
}



Alternate Code:

class Solution {
    public int maxArea(int[] height) {
        
        int [] [] area = new int[height.length][height.length];
        int prod = 0;
        
        for (int i = 0; i<height.length;i++){
            for(int j=i+1;j< height.length;j++){
                int distance = Math.abs(j-i);
                if (height[i]<height[j]){
                    
                    area[i][j-1] = height[i]*distance;
                }
                else{
                    area[i][j-1] = height[j]*distance;
                }
            }
               
                
            }
        
         
        for (int i = 0; i<height.length;i++){
            for (int j =0; j<height.length;j++){
            if (area[i][j]>prod){
                prod = area[i][j];
                
            }
            }
        
    }
        return prod;
    }
}

Copyright © saras ojha

Reversing a String - Practice

Problem: The output should be reverse of a string input.
For example, Input:  greatlearning;
Output: gninraeltaerg;

Input: to;
Output: ot;

public class reversingString {

public static void main(String[] args) {
// TODO Auto-generated method stub
String original = "greatlearning";
reversingString obj = new reversingString();
char [] get_result = obj.mirrorstring(original);
System.out.println(get_result);
   
}
public char[] mirrorstring(String original){
char [] chars = original.toCharArray();
for (int i=0,j=0; i<chars.length && j<chars.length/2; i++,j++){
char ch = chars[chars.length-1-j];
chars[chars.length-1-j] = chars[i];
chars[i]= ch;
}
return chars;
}

}

Copyright © saras ojha

Leet Code Java Solution - Verifing an Alien Dictionary

In an alien language, surprisingly, they also use English lowercase letters, but possibly in a different order. The order of the alphabet is some permutation of lowercase letters.

Given a sequence of words written in the alien language, and the order of the alphabet, return true if and only if the given words are sorted lexicographically in this alien language.

 

Example 1:

Input: words = ["hello","leetcode"], order = "hlabcdefgijkmnopqrstuvwxyz"
Output: true
Explanation: As 'h' comes before 'l' in this language, then the sequence is sorted.

Example 2:

Input: words = ["word","world","row"], order = "worldabcefghijkmnpqstuvxyz"
Output: false
Explanation: As 'd' comes after 'l' in this language, then words[0] > words[1], hence the sequence is unsorted.

Example 3:

Input: words = ["apple","app"], order = "abcdefghijklmnopqrstuvwxyz"
Output: false
Explanation: The first three characters "app" match, and the second string is shorter (in size.) According to lexicographical rules "apple" > "app", because 'l' > '∅', where '∅' is defined as the blank character which is less than any other character (More info).

 

Constraints:

  • 1 <= words.length <= 100
  • 1 <= words[i].length <= 20
  • order.length == 26
  • All characters in words[i] and order are English lowercase letters.
My Code:

class Solution {
    public boolean isAlienSorted(String[] words, String order) {
        
        for(int i=0;i<words.length-1;i++)
        {
            boolean allEqual= false;
            for(int j=0;j<words[i].length() && j<words[i+1].length();j++){
                if(order.indexOf(words[i].charAt(j))<order.indexOf(words[i+1].charAt(j))){
                    
                    allEqual = true;
                    break;
                }
                 
                    
                
                else if(order.indexOf(words[i].charAt(j))>order.indexOf(words[i+1].charAt(j))){
                    return false; 
                    
                }
                
            }
           if (!allEqual && words[i+1].length()< words[i].length()){
                    return false;
                } 
        }
        return true;
        
    }
}

Copyright © saras ojha

Leet Code Java Solution - Valid Anagram

Given two strings s and t, return true if t is an anagram of s, and false otherwise.

 

Example 1:

Input: s = "anagram", t = "nagaram"
Output: true

Example 2:

Input: s = "rat", t = "car"
Output: false

 

Constraints:

  • 1 <= s.length, t.length <= 5 * 104
  • s and t consist of lowercase English letters.

My Code:

class Solution {
    public boolean isAnagram(String s, String t) {
        char c1[] = s.toCharArray();
char c2[] = t.toCharArray();
Arrays.sort(c1);
Arrays.sort(c2);
if(Arrays.equals(c1, c2)){
return true;
}
        
         else{
return false;
}
}
   
        
    }

Copyright © saras ojha

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