Given a list of numbers and a window size k, we need to find the count of distinct numbers in each sliding window of size k. This is a common sliding window problem that can be efficiently solved using a dictionary to track element frequencies. So, if the input is like nums = [2, 2, 3, 3, 4], k = 2, then the output will be [1, 2, 1, 2], as the windows are [2, 2], [2, 3], [3, 3], and [3, 4]. Algorithm To solve this, we will follow these steps − ... Read More
Finding the perimeter of an island shape in a binary matrix involves counting the exposed edges of all connected 1s. Each cell with value 1 contributes to the perimeter based on how many of its four sides are exposed (not adjacent to another 1). Problem Understanding Given a binary matrix where 0 represents empty cells and 1 represents land blocks, we need to calculate the total perimeter. Each land block initially has 4 sides, but we subtract 1 for each adjacent land block. For the input matrix: 00000 00111 00110 ... Read More
Suppose we have two linked lists l1 and l2, we have to return one linked list by interleaving elements of these two lists starting with l1. If there are any leftover nodes in a linked list, they should be appended to the result. So, if the input is like l1 = [5, 4, 6, 3, 4, 7] l2 = [8, 6, 9], then the output will be [5, 8, 4, 6, 6, 9, 3, 4, 7] Algorithm To solve this, we will follow these steps − ans := l1 ... Read More
Suppose we have a number n, we have to find the number of n-digit positive integers such that the digits are in strictly increasing order. So, if the input is like n = 3, then the output will be 84, as numbers are 123, 124, 125, ..., 678, 789. Understanding the Problem For n-digit numbers with strictly increasing digits, we need to choose n different digits from {1, 2, 3, ..., 9}. We cannot use 0 as the first digit in an n-digit number. This becomes a combination problem: C(9, n). Algorithm To solve this, ... Read More
Sometimes we need to find the minimum height increase required to reach from the top-left corner to the bottom-right corner of a matrix. We can only move to adjacent cells if their height is less than or equal to our current cell's height, but we can increase any cell's height before moving. Problem Understanding Given a matrix M where M[r][c] represents the height of that cell, we need to find the minimum total height increase to create a path from (0, 0) to (R-1, C-1). Movement is allowed to adjacent cells (up, down, left, right) only if the ... Read More
**Issue:** This article is about R programming, not Python. Since you asked me to improve a Python article, I'll convert this to Python while maintaining the same teaching approach. In Python, you can use a for loop to iterate through all elements of a list. The syntax is straightforward: for item in list_name: where item represents each element in the list. Basic Syntax The basic syntax for iterating through a list in Python is ? for item in list_name: print(item) Example with Simple List Let's start with a ... Read More
Suppose we have a list of points and a number k. The points are in the form (x, y) representing Cartesian coordinates. We can group any two points p1 and p2 if the Euclidean distance between them is ≤ k. We have to find the total number of disjoint groups. So, if the input is like points = [[2, 2], [3, 3], [4, 4], [11, 11], [12, 12]], k = 2, then the output will be 2, as it can make two groups: ([2, 2], [3, 3], [4, 4]) and ([11, 11], [12, 12]). Algorithm To solve ... Read More
When analyzing data in R, the default summary() function provides basic statistics like minimum, quartiles, mean, and maximum. However, for comprehensive statistical analysis, we need additional descriptive measures such as variance, standard deviation, skewness, and kurtosis. The basicStats() function from the fBasics package provides all these descriptive statistics in one output. Loading Required Package First, install and load the fBasics package ? library(fBasics) Example 1: mtcars Dataset Let's examine the built-in mtcars dataset ? data(mtcars) head(mtcars, 10) ... Read More
Suppose we have a number n, we have to find the number of strings of length n can be generated using the following grammar rules ? Each character is a lower case vowel [a, e, i, o, u] "a" may only be followed by one "e" "e" may only be followed by any of "a" and "i" "i" may not be followed by another "i" "o" may only be followed by any of "i" and "u" "u" may only be followed by one "a" If the result is very large, mod the result by 10^9 + ... Read More
The fractional knapsack problem is a greedy optimization algorithm where we can take fractions of items to maximize value within a weight capacity. Unlike the 0/1 knapsack, we can break items and take partial amounts. The strategy is to sort items by their value-to-weight ratio in descending order, then greedily select items starting with the highest ratio. Algorithm Steps To solve this problem, we follow these steps − Calculate value-to-weight ratio for each item Sort items by ratio in descending order For each ... Read More
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Economics & Finance