Skip to main content

Posts

Dominant Element

You are given an array  A  of length  N . An element  X  is called  dominant  if: The frequency (count) of  X  is  strictly greater  than the frequency of  any other element  in the array. Example: A=[2,1,4,4,4]  → 4 appears 3 times, others appear fewer times → 4 is dominant. A=[1,2,2,1]  → both 1 and 2 appear 2 times → no dominant element. Your task: For each test case, check whether  any dominant element  exists. Problem Restatement For each test case: You are given an integer  N  — the length of the array. You are given  N  integers A1, A2, ...., An. You must determine if there exists some value  X  such that: freq(X) > freq(Y)  for  all   Y != X . If such an  X  exists, print  "YES" , otherwise print  "NO" . Input Format: First line: Integer  T  — number of test cases. For each test ...

Leetcode 64: Minimum Path Sum

1. Problem Statement (Simple Explanation) You’re given an m x n grid of  non-negative integers . Start at top-left: (0, 0) End at bottom-right: (m-1, n-1) You can move only  right  or  down . You must find a path from start to end with the  minimum possible sum  of values on the path and return that sum. 2. Examples Example 1: Input: grid = [   [1,3,1],   [1,5,1],   [4,2,1] ] Minimum path: 1 → 3 → 1 → 1 → 1 (Right, Right, Down, Down or some equivalent variant) Sum = 1 + 3 + 1 + 1 + 1 = 7 Output: 7 Example 2: Input: grid = [   [1,2,3],   [4,5,6] ] Minimum path: 1 → 2 → 3 → 6 Sum = 1 + 2 + 3 + 6 = 12 Output: 12 3. Approach – Dynamic Programming (O(m·n)) Let dp[i][j] be the  minimum path sum  to reach cell (i, j) from (0, 0). Transition: To reach (i, j), you can come from: (i-1, j) (down from above) (i, j-1) (right from left) So: dp...