Posts

The kth Factor of n

Given two positive integers   n   and   k . A factor of an integer  n  is defined as an integer  i  where  n % i == 0 . Consider a list of all factors of  n  sorted in  ascending order , return  the  kth  factor  in this list or return  -1  if  n  has less than  k  factors. Example 1: Input: n = 12, k = 3 Output: 3 Explanation: Factors list is [1, 2, 3, 4, 6, 12], the 3rd factor is 3. Example 2: Input: n = 7, k = 2 Output: 7 Explanation: Factors list is [1, 7], the 2nd factor is 7. Example 3: Input: n = 4, k = 4 Output: -1 Explanation: Factors list is [1, 2, 4], there is only 3 factors. We should return -1. Example 4: Input: n = 1, k = 1 Output: 1 Explanation: Factors list is [1], the 1st factor is 1. Example 5: Input: n = 1000, k = 3 Output: 4 Explanation: Factors list is [1, 2, 4, 5, 8, 10, 20, 25, 40, 50, 100, 125, 200, 25...

Spiral Matrix II

Image
Given a positive integer   n , generate an   n x n   matrix   filled with elements from   1   to   n 2   in spiral order. Example 1: Input: n = 3 Output: [[1,2,3],[8,9,4],[7,6,5]] Example 2: Input: n = 1 Output: [[1]]   Constraints: 1 <= n <= 20 Solution: class Solution {     public int[][] generateMatrix(int n) {         int[][] matrix = new int[n][n];         int top = 0, bottom = n - 1, left = 0, right = n - 1;         int val = 1;         while (val <= n * n) {             for (int i = left; i <= right; i++)                  matrix[top][i] = val++;              top++;           ...

Maximum Depth of Binary Tree

Image
Given the   root   of a binary tree, return   its maximum depth . A binary tree's  maximum depth  is the number of nodes along the longest path from the root node down to the farthest leaf node. Example 1: Input: root = [3,9,20,null,null,15,7] Output: 3 Example 2: Input: root = [1,null,2] Output: 2 Example 3: Input: root = [] Output: 0 Example 4: Input: root = [0] Output: 1 Constraints: The number of nodes in the tree is in the range  [0, 10 4 ] . -100 <= Node.val <= 100 Solution 1: /**  * Definition for a binary tree node.  * public class TreeNode {  *     int val;  *     TreeNode left;  *     TreeNode right;  *     TreeNode() {}  *     TreeNode(int val) { this.val = val; }  *     TreeNode(int val, TreeNode left, TreeN...

Longest Mountain in Array

  Let's call any (contiguous) subarray B (of A) a   mountain   if the following properties hold: B.length >= 3 There exists some  0 < i < B.length - 1  such that  B[0] < B[1] < ... B[i-1] < B[i] > B[i+1] > ... > B[B.length - 1] (Note that B could be any subarray of A, including the entire array A.) Given an array  A  of integers, return the length of the longest  mountain .  Return  0  if there is no mountain. Example 1: Input: [2,1,4,7,3,2,5] Output: 5 Explanation: The largest mountain is [1,4,7,3,2] which has length 5. Example 2: Input: [2,2,2] Output: 0 Explanation: There is no mountain. Note: 0 <= A.length <= 10000 0 <= A[i] <= 10000 Follow up: Can you solve it using only one pass? Can you solve it in  O(1)  space? Try it on Leetcode  ...

132 Pattern

  Given an array of   n   integers   nums , a   132 pattern   is a subsequence of three integers   nums[i] ,   nums[j]   and   nums[k]   such that   i < j < k   and   nums[i] < nums[k] < nums[j] . Return  true  if there is a  132 pattern  in  nums , otherwise, return  false . Follow up:  The  O(n^2)  is trivial, could you come up with the  O(n logn)  or the  O(n)  solution?   Example 1: Input: nums = [1,2,3,4] Output: false Explanation: There is no 132 pattern in the sequence. Example 2: Input: nums = [3,1,4,2] Output: true Explanation: There is a 132 pattern in the sequence: [1, 4, 2]. Example 3: Input: nums = [-1,3,2,0] Output: true Explanation: There are three 132 patterns in the sequence: [-1, 3, 2], [-1, 3, 0] and [-1, 2, 0].   Constraints: n == nums.length ...

Ascending Star Pattern

Java Program to print * pattern as follows., INPUT n=5 OUTPUT * ** *** **** ***** CODE import java.util.*; public class Main { public static void main(String[] args) { int i, j, n; System.out.println("Enter the size of n:"); Scanner scan = new Scanner(System.in); n = scan.nextInt(); for(i=1;i<=5;i++){     for(j=1;j<=i;j++){         System.out.print("*");     }     System.out.println();  //Printing Next Line } System.out.println("\n==>>> https://thefellowprogrammer.blogspot.com/ <<<<<=="); } } Here, we are going to solve using two for loops. 1) Get input value of n 2) Outer for loop (i) acts as as variable which controls number of rows to be printed with help of inner loop.  (Eg) If i=2, only two '*' will be printed 3) Inner for loop (j) acts as a variable which number of columns (i.e., *) to be printe...