Posts

Set Matrix Zero Problem LeetCode

Image
 Given an m x n integer matrix matrix, if an element is 0, set its entire row and column to 0's. Example 1: Input: matrix = [[1,1,1],[1,0,1],[1,1,1]] Output: [[1,0,1],[0,0,0],[1,0,1]] Example 2: Input: matrix = [[0,1,2,0],[3,4,5,2],[1,3,1,5]] Output: [[0,0,0,0],[0,4,5,0],[0,3,1,0]] class Solution {     public void setZeroes ( int [][] matrix ) {                 int rows = matrix . length ;         int cols = matrix[ 0 ]. length ;         boolean [] zeroRows = new boolean [rows];         boolean [] zeroCols = new boolean [cols];         // First pass to find all zeroes         for ( int i = 0 ; i < rows; i++) {             for ( int j = 0 ; j < cols; j++) {                 if (matrix[i][j] == 0 ) {             ...

Rotate Matrix By 90 Degree problem LeetCode

Image
You are given an n x n 2D matrix representing an image, rotate the image by 90 degrees (clockwise). You have to rotate the image in-place, which means you have to modify the input 2D matrix directly. DO NOT allocate another 2D matrix and do the rotation. Example 1:  Input: matrix = [[1,2,3],[4,5,6],[7,8,9]] Output: [[7,4,1],[8,5,2],[9,6,3]] Input: matrix = [[5,1,9,11],[2,4,8,10],[13,3,6,7],[15,14,12,16]] Output: [[15,13,2,5],[14,3,4,1],[12,6,8,9],[16,7,10,11]] Solution : class Solution {     public void rotate ( int [][] matrix ) {                 int n = matrix . length ;         int [][] temp = new int [n][n];                 for ( int i = 0 ;i<n;i++)         {             for ( int j = 0 ;j<n;j++)             {                 temp[j][n...

Longest Consecutive Sequence problem LeetCode

 Given an unsorted array of integers nums, return the length of the longest consecutive elements sequence. You must write an algorithm that runs in O(n) time.   Example 1: Input: nums = [100,4,200,1,3,2] Output: 4 Explanation: The longest consecutive elements sequence is [1, 2, 3, 4]. Therefore its length is 4. Example 2: Input: nums = [0,3,7,2,5,8,4,6,0,1] Output: 9 Example 3: Input: nums = [1,0,1,2] Output: 3   Constraints: 0 <= nums.length <= 105 -109 <= nums[i] <= 109 Solution : class Solution {     public int longestConsecutive ( int [] nums ) {                 if ( nums . length == 0 ) return 0 ;         Arrays . sort (nums); // Sort the array         int longest = 1 ;   // Stores the maximum length of the sequence         int currentStreak = 1 ;           for ( int i = 1 ; i < nums . length ; i++) {...

Missing Number Problem HackerRank

 Given two arrays of integers, find which elements in the second array are missing from the first array. Example The  array is the orginal list. The numbers missing are . Notes If a number occurs multiple times in the lists, you must ensure that the frequency of that number in both lists is the same. If that is not the case, then it is also a missing number. Return the missing numbers sorted ascending. Only include a missing number once, even if it is missing multiple times. The difference between the maximum and minimum numbers in the original list is less than or equal to . Function Description Complete the missingNumbers function in the editor below. It should return a sorted array of missing numbers. missingNumbers has the following parameter(s): int arr[n]: the array with missing numbers int brr[m]: the original array of numbers Returns int[]: an array of integers Input Format There will be four lines of input:  - the size of the first list,  The next line conta...

Smart Number Problem hackerRank

 In this challenge, the task is to debug the existing code to successfully execute all provided test files. A number is called a smart number if it has an odd number of factors. Given some numbers, find whether they are smart numbers or not. Debug the given function is_smart_number to correctly check if a given number is a smart number. Note: You can modify only one line in the given code and you cannot add or remove any new lines. To restore the original code, click on the icon to the right of the language selector. Input Format The first line of the input contains , the number of test cases. The next  lines contain one integer each. Constraints , where  is the  integer. Output Format The output should consist of  lines. In the  line print YES if the  integer has an odd number of factors, else print NO. Sample Input 4 1 2 7 169 Sample Output YES NO NO YES Explanation The factors of 1 are just 1 itself.So the answer is YES. The factors of 2 are 1 and 2...

How to Find Factors of a Number in Java

  How to Find Factors of a Number in Java A factor of a number N is an integer that divides N without leaving a remainder. 1️⃣ Basic Approach (Loop from 1 to N) Time Complexity: O ( N ) O(N) O ( N ) Simple but inefficient for large numbers public class Factors {     public static void main(String[] args) {         int num = 12; // Change this to any number         System.out.print("Factors of " + num + " are: ");                  for (int i = 1; i <= num; i++) {             if (num % i == 0) {                 System.out.print(i + " ");             }         }     } } 2️⃣ Optimized Approach (Loop Until √N) Time Complexity: O ( N ) O(\sqrt{N}) O ( N ​ ) More efficient Finds factors in pairs public class OptimizedFactors {     public static void ma...

Strong Password Problem hackerRank

 Louise joined a social networking site to stay in touch with her friends. The signup page required her to input a name and a password. However, the password must be strong. The website considers a password to be strong if it satisfies the following criteria: Its length is at least . It contains at least one digit. It contains at least one lowercase English character. It contains at least one uppercase English character. It contains at least one special character. The special characters are: !@#$%^&*()-+ She typed a random string of length  in the password field but wasn't sure if it was strong. Given the string she typed, can you find the minimum number of characters she must add to make her password strong? Note: Here's the set of types of characters in a form you can paste in your solution: numbers = "0123456789" lower_case = "abcdefghijklmnopqrstuvwxyz" upper_case = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" special_characters = "!@#$%^&*()-+...

Min Max Problem HackerRank

 You will be given a list of integers, , and a single integer . You must create an array of length  from elements of  such that its unfairness is minimized. Call that array . Unfairness of an array is calculated as Where: - max denotes the largest integer in  - min denotes the smallest integer in  Example Pick any two elements, say . Testing for all pairs, the solution  provides the minimum unfairness. Note: Integers in  may not be unique. Function Description Complete the maxMin function in the editor below. maxMin has the following parameter(s): int k: the number of elements to select int arr[n]:: an array of integers Returns int: the minimum possible unfairness Input Format The first line contains an integer , the number of elements in array . The second line contains an integer . Each of the next  lines contains an integer  where . Solution :// Sliding Window Approach import java.io.*; import java.math.*; import java.security.*; import...

pickingNumbers Problem HackerRank

 Given an array of integers, find the longest subarray where the absolute difference between any two elements is less than or equal to . Example There are two subarrays meeting the criterion:  and . The maximum length subarray has  elements. Function Description Complete the pickingNumbers function in the editor below. pickingNumbers has the following parameter(s): int a[n]: an array of integers Returns int: the length of the longest subarray that meets the criterion Input Format The first line contains a single integer , the size of the array . The second line contains  space-separated integers, each an . Solution : import java.io.*; import java.math.*; import java.security.*; import java.text.*; import java.util.*; import java.util.concurrent.*; import java.util.function.*; import java.util.regex.*; import java.util.stream.*; import static java.util.stream.Collectors.joining; import static java.util.stream.Collectors.toList; class Result {   ...

Rearrange Array Elements by Sign LeetCode Problem

 You are given a 0-indexed integer array nums of even length consisting of an equal number of positive and negative integers. You should return the array of nums such that the the array follows the given conditions: Every consecutive pair of integers have opposite signs. For all integers with the same sign, the order in which they were present in nums is preserved. The rearranged array begins with a positive integer. Return the modified array after rearranging the elements to satisfy the aforementioned conditions.   Example 1: Input: nums = [3,1,-2,-5,2,-4] Output: [3,-2,1,-5,2,-4] Explanation: The positive integers in nums are [3,1,2]. The negative integers are [-2,-5,-4]. The only possible way to rearrange them such that they satisfy all conditions is [3,-2,1,-5,2,-4]. Other ways such as [1,-2,2,-5,3,-4], [3,1,2,-2,-5,-4], [-2,3,-5,1,-4,2] are incorrect because they do not satisfy one or more conditions.   Example 2: Input: nums = [-1,1] Output: [1,-1] Explanation:...