Skip to content

Commit c82aebc

Browse files
author
Sanajit Jana
committed
Add more questions
1 parent d053ed8 commit c82aebc

File tree

57 files changed

+5138
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+5138
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* 231. Power of Two
3+
* Difficulty: Easy
4+
* URL: https://leetcode.com/problems/power-of-two/description/
5+
*
6+
* ---------------------
7+
* Approach 1: Iterative Division
8+
*
9+
* - A number `n` is a power of two if it can be written as 2^k (where k ≥ 0).
10+
* - If `n` is positive and divisible by 2, we keep dividing it by 2.
11+
* - If after dividing repeatedly, the result becomes 1, then it was a power of two.
12+
* - Otherwise, it is not.
13+
*
14+
* Example:
15+
* n = 16 → 16/2=8 → 8/2=4 → 4/2=2 → 2/2=1 ✅ Power of Two
16+
* n = 12 → 12/2=6 → 6/2=3 ❌ not 1 → Not a Power of Two
17+
*
18+
* ---------------------
19+
* Time Complexity: O(log n)
20+
* - In each step we divide by 2 → about log₂(n) steps.
21+
*
22+
* Space Complexity: O(1)
23+
* - Only using a few variables, no extra space.
24+
*/
25+
class Solution {
26+
public boolean isPowerOfTwo(int n) {
27+
if (n <= 0)
28+
return false;
29+
while (n % 2 == 0) {
30+
n = n / 2;
31+
}
32+
return n == 1;
33+
}
34+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* 342. Power of Four
3+
* Difficulty: Easy
4+
* URL: https://leetcode.com/problems/power-of-four/
5+
*
6+
* ---------------------
7+
* Approach: Iterative Division
8+
* ---------------------
9+
* - If n <= 0, return false (negative numbers and zero can’t be powers of 4).
10+
* - Repeatedly divide n by 4 while it is divisible by 4.
11+
* - After the loop, if n reduces to 1 → it’s a power of 4, otherwise not.
12+
*
13+
* Time Complexity: O(log₄ n) // dividing by 4 each time
14+
* Space Complexity: O(1)
15+
*/
16+
17+
18+
class Solution {
19+
public boolean isPowerOfFour(int n) {
20+
if (n <= 0)
21+
return false;
22+
while (n % 4 == 0)
23+
n = n / 4;
24+
return n == 1;
25+
}
26+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
Question: Largest Triangle Area
3+
Link: https://leetcode.com/problems/largest-triangle-area/
4+
5+
Question Info:
6+
You are given an array of points on a 2D plane. Each point is represented as an integer coordinate [x, y].
7+
Your task is to return the largest area of a triangle formed by any three points in the given array.
8+
9+
Approach:
10+
1. Use the shoelace formula (determinant method) to compute the area of a triangle formed by three points (x1,y1), (x2,y2), (x3,y3).
11+
Formula:
12+
Area = 0.5 * |x1(y2 − y3) + x2(y3 − y1) + x3(y1 − y2)|
13+
2. Iterate over all possible triplets of points using three nested loops.
14+
3. For each triplet, calculate the area using the formula and update the maximum area found.
15+
4. Return the maximum area.
16+
17+
Dry Run:
18+
Example: points = [[0,0],[0,1],[1,0],[0,2],[2,0]]
19+
20+
- Pick (0,0), (0,1), (1,0) → Area = 0.5
21+
- Pick (0,0), (0,2), (2,0) → Area = 2
22+
- Pick (0,1), (0,2), (2,0) → Area = 1
23+
- After checking all triplets, maximum = 2
24+
25+
Time Complexity: O(n^3), since we check all triplets of points.
26+
Space Complexity: O(1), only a few variables used.
27+
*/
28+
29+
class Solution {
30+
public double largestTriangleArea(int[][] points) {
31+
int length = points.length;
32+
double maxArea = Double.MIN_VALUE;
33+
34+
for (int i = 0; i < length - 2; i++) {
35+
for (int j = i + 1; j < length - 1; j++) {
36+
for (int k = j + 1; k < length; k++) {
37+
38+
int x1 = points[i][0], y1 = points[i][1];
39+
int x2 = points[j][0], y2 = points[j][1];
40+
int x3 = points[k][0], y3 = points[k][1];
41+
42+
double area = 0.5 * Math.abs(
43+
x1 * (y2 - y3) +
44+
x2 * (y3 - y1) +
45+
x3 * (y1 - y2));
46+
47+
maxArea = Math.max(maxArea, area);
48+
}
49+
}
50+
}
51+
return maxArea;
52+
}
53+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/**
2+
* Title: Positions of Large Groups
3+
* LeetCode Link: https://leetcode.com/problems/positions-of-large-groups/
4+
*
5+
* Question Info:
6+
* In a string s of lowercase letters, a group is a consecutive run of the same character.
7+
* A large group is defined as a group that has length >= 3.
8+
* Return the intervals [start, end] of every large group sorted in increasing order of start index.
9+
*
10+
* Example:
11+
* Input: s = "abbxxxxzzy"
12+
* Output: [[3,6]]
13+
*
14+
* Approach:
15+
* - Use two pointers to track the start of a group and the current index.
16+
* - Iterate through the string from i = 1 to n.
17+
* - If we reach the end OR the current char differs from the start char:
18+
* - Check if the group length >= 3.
19+
* - If yes, record the interval [start, i-1].
20+
* - Move start = i to begin a new group.
21+
* - Return the list of all intervals.
22+
*
23+
* Dry Run (s = "abbxxxxzzy"):
24+
* i=1: s[1]='b', s[0]='a' => different, group size=1 (<3), skip, start=1
25+
* i=2: s[2]='b', s[1]='b' => same, continue
26+
* i=3: s[3]='x', s[1]='b' => different, group size=2 (<3), skip, start=3
27+
* i=4,5,6: still 'x', continue
28+
* i=7: s[7]='z', s[3]='x' => different, group size=4 (>=3), record [3,6], start=7
29+
* i=8: s[8]='z', s[7]='z' => same
30+
* i=9: s[9]='y', s[7]='z' => different, group size=2 (<3), skip, start=9
31+
* i=10: reached end, group size=1 (<3), skip.
32+
* Result: [[3,6]]
33+
*
34+
* Time Complexity: O(n), where n = length of string (single pass).
35+
* Space Complexity: O(1), ignoring output list.
36+
*/
37+
38+
class Solution {
39+
public List<List<Integer>> largeGroupPositions(String s) {
40+
List<List<Integer>> ans = new ArrayList<>();
41+
int n = s.length();
42+
int start = 0;
43+
44+
for (int i = 1; i <= n; i++) {
45+
if (i == n || s.charAt(i) != s.charAt(start)) {
46+
if (i - start >= 3) {
47+
ans.add(Arrays.asList(start, i - 1));
48+
}
49+
start = i;
50+
}
51+
}
52+
53+
return ans;
54+
}
55+
}
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/*
2+
* Problem Link:
3+
* https://leetcode.com/problems/largest-perimeter-triangle/?envType=daily-question&envId=2025-09-28
4+
*
5+
* Q: Largest Perimeter Triangle
6+
*
7+
* You are given an array nums of non-negative integers.
8+
* Return the largest perimeter of a triangle with non-zero area, formed from three of these lengths.
9+
* If it is impossible to form any triangle of non-zero area, return 0.
10+
*
11+
* ------------------------------------------------------
12+
* Approach 1: Brute Force (O(n^3))
13+
* ------------------------------------------------------
14+
* Idea:
15+
* - Sort the array.
16+
* - Try every possible triplet (i, j, k).
17+
* - Check the triangle inequality:
18+
* a + b > c, a + c > b, b + c > a
19+
* - Keep track of the maximum perimeter.
20+
*
21+
* Dry Run Example:
22+
* nums = [2, 1, 2]
23+
* After sort = [1, 2, 2]
24+
* Pick (1,2,2):
25+
* 1 + 2 > 2 (yes)
26+
* 1 + 2 > 2 (yes)
27+
* 2 + 2 > 1 (yes)
28+
* Valid triangle, perimeter = 5
29+
* Answer = 5
30+
*
31+
* Time Complexity: O(n^3)
32+
* Space Complexity: O(1)
33+
*/
34+
35+
import java.util.Arrays;
36+
37+
class SolutionBruteForce {
38+
public int largestPerimeter(int[] nums) {
39+
Arrays.sort(nums);
40+
int max = 0;
41+
for (int i = 0; i < nums.length - 2; i++) {
42+
int x = nums[i];
43+
for (int j = i + 1; j < nums.length - 1; j++) {
44+
int y = nums[j];
45+
for (int k = j + 1; k < nums.length; k++) {
46+
int z = nums[k];
47+
if (x + y > z && x + z > y && y + z > x) {
48+
max = Math.max(max, x + y + z);
49+
}
50+
}
51+
}
52+
}
53+
return max;
54+
}
55+
}
56+
57+
58+
/*
59+
* ------------------------------------------------------
60+
* Approach 2: Optimized Greedy (O(n log n))
61+
* ------------------------------------------------------
62+
* Idea:
63+
* - Sort the array.
64+
* - The largest perimeter will always come from the three largest numbers
65+
* that can form a valid triangle.
66+
* - Starting from the end of the sorted array, check triplets (i, i-1, i-2).
67+
* - As soon as we find a valid triangle, return its perimeter.
68+
*
69+
* Why consecutive triplets are enough:
70+
* - After sorting, if nums[i-2] + nums[i-1] > nums[i], then
71+
* (nums[i-2], nums[i-1], nums[i]) is valid and maximizes perimeter.
72+
* - If not, moving left reduces the largest side, so we must keep checking.
73+
*
74+
* Dry Run Example:
75+
* nums = [2, 1, 2]
76+
* After sort = [1, 2, 2]
77+
* Start from i = 2:
78+
* nums[0] + nums[1] = 1 + 2 = 3 > 2 → valid
79+
* perimeter = 1 + 2 + 2 = 5
80+
* Answer = 5
81+
*
82+
* Time Complexity: O(n log n) (due to sorting)
83+
* Space Complexity: O(1)
84+
*/
85+
86+
class SolutionOptimized {
87+
public int largestPerimeter(int[] nums) {
88+
Arrays.sort(nums);
89+
int n = nums.length;
90+
for (int i = n - 1; i >= 2; i--) {
91+
if (nums[i - 2] + nums[i - 1] > nums[i]) {
92+
return nums[i] + nums[i - 1] + nums[i - 2];
93+
}
94+
}
95+
return 0;
96+
}
97+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/**
2+
* Problem: Generate an array of n unique integers such that their sum is 0.
3+
*
4+
* Approach 1:
5+
* - Start from 1 and alternate adding -num and +num into the array.
6+
* - If n is odd, include 0 in the array.
7+
* - This ensures symmetry and guarantees the sum is zero.
8+
*
9+
* Time Complexity: O(n)
10+
* Space Complexity: O(n)
11+
*/
12+
class Solution {
13+
public int[] sumZero(int n) {
14+
if (n == 1) return new int[n];
15+
16+
int[] arr = new int[n];
17+
int index = 0;
18+
if (n % 2 == 1) {
19+
index = 1; // Leave space for 0 if n is odd
20+
}
21+
22+
boolean flag = true;
23+
int num = 1;
24+
25+
while (index < n) {
26+
if (flag) {
27+
arr[index++] = -num;
28+
flag = false;
29+
} else {
30+
arr[index++] = num;
31+
flag = true;
32+
num++;
33+
}
34+
}
35+
36+
return arr;
37+
}
38+
}
39+
40+
/**
41+
* Approach 2 (Optimized and Cleaner):
42+
* - Pair numbers symmetrically: -1 & 1, -2 & 2, ..., until n/2 pairs.
43+
* - If n is odd, append 0 at the end.
44+
* - Cleaner logic without flags or while-loops.
45+
*
46+
* Time Complexity: O(n)
47+
* Space Complexity: O(n)
48+
*/
49+
class Solution {
50+
public int[] sumZero(int n) {
51+
int[] arr = new int[n];
52+
int pairs = n / 2;
53+
int index = 0;
54+
55+
for (int i = 1; i <= pairs; i++) {
56+
arr[index++] = -i;
57+
arr[index++] = i;
58+
}
59+
60+
if (n % 2 == 1) {
61+
arr[index] = 0;
62+
}
63+
64+
return arr;
65+
}
66+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/**
2+
* Problem:
3+
* https://leetcode.com/problems/convert-integer-to-the-sum-of-two-no-zero-integers/
4+
*
5+
* Given an integer n, return two integers a and b such that:
6+
* - a + b == n
7+
* - Neither a nor b contains the digit zero in their decimal representation.
8+
* You can return any valid pair [a, b].
9+
*
10+
* Example:
11+
* Input: n = 11
12+
* Output: [2, 9]
13+
* Explanation: 2 + 9 = 11 and neither 2 nor 9 contains zero.
14+
*
15+
* Constraints:
16+
* - 2 <= n <= 10^4
17+
*
18+
* Approach:
19+
* We iterate from i = 1 to n - 1. For each i, we check if both i and (n - i)
20+
* do not contain any zero digit by using the containsZero helper method.
21+
* As soon as we find such a pair, we return it.
22+
* The containsZero method checks if a number has the digit 0 by iterating
23+
* through its digits using modulo and division.
24+
*
25+
* Time Complexity:
26+
* O(n * log(n)) — In the worst case, we iterate up to n and for each number,
27+
* checking if it contains zero takes O(log n) time (based on the number of digits).
28+
*
29+
* Space Complexity:
30+
* O(1) — We only use a constant amount of extra space for variables.
31+
*/
32+
class Solution {
33+
public int[] getNoZeroIntegers(int n) {
34+
for (int i = 1; i < n; i++) {
35+
if (!containsZero(i) && !containsZero(n - i)) {
36+
return new int[] { i, n - i };
37+
}
38+
}
39+
return new int[] { 1, n - 1 };
40+
}
41+
42+
private boolean containsZero(int num) {
43+
while (num > 0) {
44+
int digit = num % 10;
45+
if (digit == 0) {
46+
return true;
47+
}
48+
num = num / 10;
49+
}
50+
return false;
51+
}
52+
}

0 commit comments

Comments
 (0)