Skip to content

Commit

Permalink
commit
Browse files Browse the repository at this point in the history
  • Loading branch information
abhishekghoshh committed Dec 8, 2024
1 parent e568ffd commit c87522f
Show file tree
Hide file tree
Showing 7 changed files with 140 additions and 23 deletions.
4 changes: 1 addition & 3 deletions README-old.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,7 @@
- bit-manipulation
- [Longest Consecutive Sequence in an Array](/src/com/problems/array/LongestConsecutiveSequence.java)
- hashing
- [Product of Array Except Self](/src/com/problems/array/ProductOfArrayExceptSelf.java)
- hashing
- prefix-sum
-
- [Longest Subarray with given Sum K(Positives)](/src/com/problems/array/LongestSubarrayWithSumEqualsK_1.java)
- prefix-sum
- two-pointer
Expand Down
2 changes: 2 additions & 0 deletions resources/arrays.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
- [Intersection of Two Arrays](/src/com/problems/array/IntersectionOfTwoArrays.java)
- [Number of Students Unable to Eat Lunch](/src/com/problems/array/NumberOfStudentsUnableToEatLunch.java)
- [Time Needed to Buy Tickets](/src/com/problems/array/TimeNeededToBuyTickets.java)
- [Special Array With X Elements Greater Than or Equal X](/src/com/problems/array/SpecialArrayWithXElementsGreaterThanOrEqualX.java)


### Bit Manipulation
Expand All @@ -38,5 +39,6 @@
- [Find Pivot Index](/src/com/problems/array/FindPivotIndex.java)
- [Range Sum Query - Immutable](/src/com/problems/array/RangeSumQueryImmutable.java)
- [Largest Substring Between Two Equal Characters](/src/com/problems/array/LargestSubstringBetweenTwoEqualCharacters.java)
- [Product of Array Except Self](/src/com/problems/array/ProductOfArrayExceptSelf.java)


32 changes: 21 additions & 11 deletions src/com/problems/array/PascalTriangleOneRow.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,33 @@ public static void main(String[] args) {
type1();
}

// print the specific row
// one specific row
// Create all rows of pascal triangle
// 1
// 1 1
// 1 2 1
// 1 3 3 1
// if we consider one row then the coefficient are something like (a+b) ^ (n-1)
// for row 1st row 1
// 2nd row a+b
// 3rd row a^2 + 2ab + b^2
// 4th row a^3 + 3a^2b + 3ab^2 + b^3
// for a particular ith cell it is (n-1-i) C i
// for the first row it is 1 then (n/i) then n*(n-1) / i*(i+1) and so on,
// so we will start with n and i and decrease n and increase i
private static void type1() {
int numRow = 5;
List<Integer> row = getRow(numRow);
int numRow = 2;
List<Integer> row = getRow1(numRow);
System.out.println("5th row is " + row);
}

private static List<Integer> getRow(int row) {
// this code is 0 indexed the rows are (a+b) ^ (n-1)
private static List<Integer> getRow1(int row) {
List<Integer> ans = new ArrayList<>();
int denominator = 1;
ans.add(1);
while (row != 1) {
row--;
int last = ans.get(ans.size() - 1);
ans.add(last * row / denominator);
denominator++;
for (double upper = row, lower = 1; lower <= row; lower++, upper--) {
double last = ans.get(ans.size() - 1);
double curr = Math.round(last * upper / lower);
ans.add((int) curr);
}
return ans;
}
Expand Down
11 changes: 5 additions & 6 deletions src/com/problems/array/ProductOfArrayExceptSelf.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,16 @@

/*
* Problem links:
* https://leetcode.com/problems/product-of-array-except-self/
* https://leetcode.com/problems/product-of-array-except-self/description/
* https://neetcode.io/problems/products-of-array-discluding-self
*
* Solution link :
* https://www.youtube.com/watch?v=bNvIQI2wAjk
*
*
* */
/*
* Tags:
* Array, Hashing
* https://neetcode.io/solutions/product-of-array-except-self
* */

// Tags : Array, Hashing, Prefix sum
public class ProductOfArrayExceptSelf {
public static void main(String[] args) {
type1();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package com.problems.array;

import java.util.Arrays;

/*
*
* problem links :
* https://leetcode.com/problems/special-array-with-x-elements-greater-than-or-equal-x/description/
*
* Solution link :
* https://www.youtube.com/watch?v=Z51jYCeBLVI
*
* https://neetcode.io/solutions/special-array-with-x-elements-greater-than-or-equal-x
* */

// Tags : Arrays, hashing, sorting, binary search
public class SpecialArrayWithXElementsGreaterThanOrEqualX {
public static void main(String[] args) {
type1();
type2();
type3();
}

// most optimized approach
// using freq array
// we know one thing that the answer will lie between 1 and n
// even if nums[i] > n because if there is n elements then
// we can have at max n elements which can be greater than the special element,
// we will count the freq of the array and for the elements > n we will increment freq[n]
private static void type3() {
int[] nums = {3, 9, 7, 8, 3, 8, 6, 6};
int ans = specialArray3(nums);
System.out.println(ans);
}

public static int specialArray3(int[] nums) {
int n = nums.length;
// creating the freq array
int[] freq = new int[n + 1];
for (int num : nums) {
// if the array is greater than n then we will take the n
num = Math.min(num, n);
freq[num]++;
}
int count = 0;
// now we will go from the last, and count the numbers which are greater than num
// count is the cumulative freq
for (int num = n; num >= 0; num--) {
count += freq[num];
if (count == num) {
return num;
}
}
return -1;
}

// optimized approach using sort
// lets sort it and count the elements from right to left
// lets say numbers are 1 2 4 5 6,
// and we are now currently 4 so that means on right there is 3 numbers
// is 3 is in between 2 and 4, which is yes, so we will return 3
// we will go till to start
private static void type2() {
int[] nums = {3, 9, 7, 8, 3, 8, 6, 6};
int ans = specialArray2(nums);
System.out.println(ans);
}

private static int specialArray2(int[] nums) {
Arrays.sort(nums);
int n = nums.length;
int i = n - 1;
// checking from the last
while (i >= 0) {
int curr = nums[i];
// skipping all the duplicates
while (i > 0 && curr == nums[i - 1]) i--;
// finding the count on the right side
int count = (n - i);
int prev = (i > 0) ? nums[i - 1] : Integer.MIN_VALUE;
// here we are checking the actual condition
if (prev < count && count <= curr) return count;
i--;
}
return -1;
}

// brute force approach
private static void type1() {
int[] nums = {3, 9, 7, 8, 3, 8, 6, 6};
int ans = specialArray1(nums);
System.out.println(ans);
}

public static int specialArray1(int[] nums) {
int n = nums.length;
for (int i = 1; i <= n; i++) {
int count = 0;
for (int num : nums) {
if (i <= num) count++;
}
if (i == count) return i;
}
return -1;
}
}
4 changes: 3 additions & 1 deletion src/com/problems/heap/KMostFrequentElements.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
*
* Solution link :
* https://www.youtube.com/watch?v=7VoJn544QrM&list=PL_z_8CaSLPWdtY9W22VjnPxG30CXNZpI9&index=6
*
* https://www.youtube.com/watch?v=YPTqKIgVk-k
*
* https://neetcode.io/solutions/top-k-frequent-elements
* */

// Tags : Array, Heap, Hashing
Expand Down
4 changes: 2 additions & 2 deletions src/com/problems/string/EncodeAndDecodeStrings.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
/*
* Problem link :
* https://leetcode.com/problems/encode-and-decode-strings/description/
* https://www.lintcode.com/problem/659/
* https://neetcode.io/problems/string-encode-and-decode
* https://www.lintcode.com/problem/659/
*
* Solution link :
* https://www.youtube.com/watch?v=B1k_sxOSgv8
*
* https://neetcode.io/solutions/encode-and-decode-strings
* https://medium.com/@miniChang8/leetcode-encode-and-decode-strings-4dde7e0efa1c
*
*/

// Tags : Array, String, Hashing
Expand Down

0 comments on commit c87522f

Please sign in to comment.