Java notes
Java notes
com/in/anshulagarwal30
xplanation:
E
Traverse the array while updating the maximum value when a larger element is encountered.
xplanation:
E
Keep two variables— firstfor the maximum and
secondfor the second maximum. Update
them accordingly during iteration.
xplanation:
E
Swap elements from the start and end of the array until you reach the middle.
https://www.linkedin.com/in/anshulagarwal30
xplanation:
E
Iterate through the array and check if each element is greater than or equal to its
predecessor.
https://www.linkedin.com/in/anshulagarwal30
xplanation:
E
Since the array is sorted, duplicate elements are adjacent. Use two pointers to rewrite
unique elements.
https://www.linkedin.com/in/anshulagarwal30
xplanation:
E
Reverse the entire array, then reverse the first k elements and the remaining elements
separately.
https://www.linkedin.com/in/anshulagarwal30
xplanation:
E
Calculate the expected sum for numbers 1 to N and subtract the sum of the given array.
xplanation:
E
Use two pointers: one to keep track of the next non-zero insertion and then fill the rest with
zeros.
xplanation:
E
Use hash sets to store elements and then find common values.
https://www.linkedin.com/in/anshulagarwal30
xplanation:
E
Combine elements from both arrays into a set which naturally removes duplicates.
xplanation:
E
Iteratively compute the maximum sum ending at each index and update the global
maximum.
xplanation:
E
Keep track of both the maximum and minimum products because a negative number can flip
the sign.
https://www.linkedin.com/in/anshulagarwal30
xplanation:
E
Use a HashMap to count how many times each element appears.
xplanation:
E
Iterate through the array and use a set to record seen elements. The first element that is
seen again is returned.
xplanation:
E
A LinkedHashMap is used to preserve insertion order while counting frequencies; then the
first element with count 1 is returned.
https://www.linkedin.com/in/anshulagarwal30
xplanation:
E
Using XOR properties, identical numbers cancel out, leaving the unique element.
https://www.linkedin.com/in/anshulagarwal30
xplanation:
E
Use three pointers, one for each array, to traverse them simultaneously and find common
elements.
https://www.linkedin.com/in/anshulagarwal30
xplanation:
E
s,
Use the sliding window technique. Expand the window until the sum exceeds or equals
then shrink it while maintaining the condition.
https://www.linkedin.com/in/anshulagarwal30
xplanation:
E
Separate positive and negative numbers into two lists then merge them alternatively.
https://www.linkedin.com/in/anshulagarwal30
xplanation:
E
Using a hash set to store the required complement for each element. If you encounter an
element that is already in the set, a valid pair exists.
https://www.linkedin.com/in/anshulagarwal30
xplanation:
E
Sort the array and use a two-pointer approach for each element to find a triplet that sums to
the target.
https://www.linkedin.com/in/anshulagarwal30
xplanation:
E
The Moore’s Voting Algorithm finds a candidate by cancelling out different elements; a
second pass may be added for verification.
https://www.linkedin.com/in/anshulagarwal30
xplanation:
E
Use a stack to keep indexes and update the result array when a greater element is found.
https://www.linkedin.com/in/anshulagarwal30
xplanation:
E
Merge the two sorted arrays into one and then compute the median based on the merged
length.
https://www.linkedin.com/in/anshulagarwal30
xplanation:
E
Convert 0s to -1s, then use a cumulative sum approach with a hash map to find the longest
subarray that sums to 0.
https://www.linkedin.com/in/anshulagarwal30
xplanation:
E
dp[i]stores thelength of the longest increasing
Dynamic programming solution where
i.
subsequence ending at index
https://www.linkedin.com/in/anshulagarwal30
xplanation:
E
Similar to LIS, but instead of length, maintain the sum of the increasing subsequence.
https://www.linkedin.com/in/anshulagarwal30
xplanation:
E
Count the number of “good” elements (≤ k) and slide a window of that size to find the window
with minimum “bad” elements.
https://www.linkedin.com/in/anshulagarwal30
xplanation:
E
Traverse the array from right to left and maintain the maximum element found so far. An
element is a leader if it is greater than or equal to that maximum.
https://www.linkedin.com/in/anshulagarwal30
xplanation:
E
Place each positive integer in its correct position (i.e., 1 in index 0, 2 in index 1, etc.). The
first index that doesn’t have the correct number indicates the missing positive integer.