Skip to main content

Posts

Equal Elements

You are given an array  A  of size  N . In  one operation , you can: Select indices  i  and  j  with  i≠j Set  A i = A j You need to find the  minimum number of operations  required to make  all elements of the array equal . 1. Problem Restatement For each test case: You have an array  A  of size  N . Operation: pick two positions  i  and  j   (i≠j)  and assign  A i = A j . After some operations, you want all elements of  A  to be the same. Goal:   Output the  minimum  number of operations needed. Input Format: First line: integer  T  — number of test cases. For each test case: First line: integer  N  — array size. Second line:  N  space-separated integers — elements of array  A . Output Format: For each test case, print the minimum number of operations on a new line. Constraints: 1 ≤ T ≤ 1000 1 ≤ N ≤ 2* 10 5 1 ≤ A i ≤ ...

Leetcode 62: Unique Paths

1. Problem Statement (Simple Explanation) You have an m x n grid: Start at top-left: (0, 0) Goal is bottom-right: (m-1, n-1) At each step you can move  only right  or  down . You must return the  number of unique paths  from start to goal. Answer is guaranteed ≤ 2 * 10 9 . 2. Examples Example 1: Input: m = 3, n = 7 There are 28 distinct paths. Output: 28 Example 2: Input: m = 3, n = 2 Paths: Right → Down → Down Down → Down → Right Down → Right → Down Total = 3. Output: 3 3. Approach 1 – Dynamic Programming (O(m·n)) Let dp[i][j] = number of unique paths to reach cell (i, j). Rules: Only move right or down: To reach (i, j), you can come from: (i-1, j) (from above) (i, j-1) (from left) So: dp[i][j] = dp[i-1][j] + dp[i][j-1] Base cases: First row i = 0: You can only move right, so dp[0][j] = 1 for all j. First column j = 0: You can only move down, so dp[i][0] = 1 for all i...