Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
3 views

Java notes

The document lists 30 array coding questions along with Java solutions and explanations for each problem. Topics include finding the largest and second largest elements, reversing arrays, checking if arrays are sorted, and various algorithms for manipulating arrays. The document serves as a comprehensive guide for practicing array-related coding challenges.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Java notes

The document lists 30 array coding questions along with Java solutions and explanations for each problem. Topics include finding the largest and second largest elements, reversing arrays, checking if arrays are sorted, and various algorithms for manipulating arrays. The document serves as a comprehensive guide for practicing array-related coding challenges.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

‭https://www.linkedin.

com/in/anshulagarwal30‬

‭30 array coding questions with Java solution‬

‭ . Find the Largest Element in an Array‬


1
‭2. Find the Second Largest Element‬
‭3. Reverse an Array‬
‭4. Check if Array Is Sorted‬
‭5. Remove Duplicates from a Sorted Array‬
‭6. Rotate an Array by K Positions‬
‭7. Find the Missing Number in an Array (1 to N)‬
‭8. Move All Zeros to the End of an Array‬
‭9. Find the Intersection of Two Arrays‬
‭10. Find the Union of Two Arrays‬
‭11. Maximum Subarray Sum (Kadane’s Algorithm)‬
‭12. Maximum Product Subarray‬
‭13. Count Frequency of Elements in an Array‬
‭14. Find the First Repeating Element in an Array‬
‭15. Find the First Non-Repeating Element in an Array‬
‭16. Find the Element That Appears Only Once (XOR Method)‬
‭17. Find Common Elements in Three Sorted Arrays‬
‭18. Find the Smallest Subarray with Sum Greater Than X‬
‭19. Rearrange Positive and Negative Numbers Alternately‬
‭20. Find a Pair in an Array That Sums to K‬
‭21. Find a Triplet in an Array That Sums to K‬
‭22. Find the Majority Element (Moore’s Voting Algorithm)‬
‭23. Next Greater Element for Each Element in an Array‬
‭24. Find the Median of Two Sorted Arrays‬
‭25. Maximum Length of an Equal 0-1 Subarray‬
‭26. Longest Increasing Subsequence (LIS)‬
‭27. Maximum Sum Increasing Subsequence‬
‭28. Minimum Swaps to Bring Elements Less Than K Together‬
‭29. Find the Leader Elements in an Array‬
‭30. Find the Smallest Missing Positive Integer‬
‭https://www.linkedin.com/in/anshulagarwal30‬

‭1. Find the Largest Element in an Array‬

‭ xplanation:‬
E
‭Traverse the array while updating the maximum value when a larger element is encountered.‬

‭2. Find the Second Largest Element‬


‭https://www.linkedin.com/in/anshulagarwal30‬

‭ xplanation:‬
E
‭Keep two variables—‬‭ first‬‭for the maximum and‬‭
second‬‭for the second maximum. Update‬
‭them accordingly during iteration.‬

‭3. Reverse an Array‬

‭ xplanation:‬
E
‭Swap elements from the start and end of the array until you reach the middle.‬
‭https://www.linkedin.com/in/anshulagarwal30‬

‭4. Check if Array Is Sorted‬

‭ 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‬

‭5. Remove Duplicates from a Sorted Array‬


‭(Assuming in-place removal and returning new length)‬

‭ xplanation:‬
E
‭Since the array is sorted, duplicate elements are adjacent. Use two pointers to rewrite‬
‭unique elements.‬
‭https://www.linkedin.com/in/anshulagarwal30‬

‭6. Rotate an Array by K Positions‬


‭(Right rotation)‬

‭ xplanation:‬
E
‭Reverse the entire array, then reverse the first k elements and the remaining elements‬
‭separately.‬
‭https://www.linkedin.com/in/anshulagarwal30‬

‭7. Find the Missing Number in an Array (1 to N)‬

‭ xplanation:‬
E
‭Calculate the expected sum for numbers 1 to N and subtract the sum of the given array.‬

‭8. Move All Zeros to the End of an Array‬


‭https://www.linkedin.com/in/anshulagarwal30‬

‭ xplanation:‬
E
‭Use two pointers: one to keep track of the next non-zero insertion and then fill the rest with‬
‭zeros.‬

‭9. Find the Intersection of Two Arrays‬


‭(Unique intersection)‬

‭ xplanation:‬
E
‭Use hash sets to store elements and then find common values.‬
‭https://www.linkedin.com/in/anshulagarwal30‬

‭10. Find the Union of Two Arrays‬


‭(Unique union)‬

‭ xplanation:‬
E
‭Combine elements from both arrays into a set which naturally removes duplicates.‬

‭11. Maximum Subarray Sum (Kadane’s Algorithm)‬


‭https://www.linkedin.com/in/anshulagarwal30‬

‭ xplanation:‬
E
‭Iteratively compute the maximum sum ending at each index and update the global‬
‭maximum.‬

‭12. Maximum Product Subarray‬

‭ 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‬

‭13. Count Frequency of Elements in an Array‬

‭ xplanation:‬
E
‭Use a HashMap to count how many times each element appears.‬

‭14. Find the First Repeating Element in an Array‬


‭https://www.linkedin.com/in/anshulagarwal30‬

‭ xplanation:‬
E
‭Iterate through the array and use a set to record seen elements. The first element that is‬
‭seen again is returned.‬

‭15. Find the First Non-Repeating Element in an Array‬

‭ 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‬

‭ 6. Find the Element That Appears Only Once (XOR‬


1
‭Method)‬

‭ xplanation:‬
E
‭Using XOR properties, identical numbers cancel out, leaving the unique element.‬
‭https://www.linkedin.com/in/anshulagarwal30‬

‭17. Find Common Elements in Three Sorted Arrays‬

‭ xplanation:‬
E
‭Use three pointers, one for each array, to traverse them simultaneously and find common‬
‭elements.‬
‭https://www.linkedin.com/in/anshulagarwal30‬

‭ 8. Find the Smallest Subarray with Sum Greater Than‬


1
‭X‬

‭ 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‬

‭ 9. Rearrange Positive and Negative Numbers‬


1
‭Alternately‬
‭(Assumes equal or almost equal counts)‬

‭ xplanation:‬
E
‭Separate positive and negative numbers into two lists then merge them alternatively.‬
‭https://www.linkedin.com/in/anshulagarwal30‬

‭20. Find a Pair in an Array That Sums to K‬

‭ 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‬

‭21. Find a Triplet in an Array That Sums to K‬

‭ 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‬

‭ 2. Find the Majority Element (Moore’s Voting‬


2
‭Algorithm)‬

‭ 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‬

‭23. Next Greater Element for Each Element in an Array‬

‭ 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‬

‭24. Find the Median of Two Sorted Arrays‬


‭(Simplified approach: merge and find median)‬

‭ 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‬

‭25. Maximum Length of an Equal 0-1 Subarray‬


‭(For binary arrays)‬

‭ 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‬

‭26. Longest Increasing Subsequence (LIS)‬

‭ xplanation:‬
E
dp[i]‬‭stores the‬‭length of the longest increasing‬
‭Dynamic programming solution where‬‭
i‭.‬‬
‭subsequence ending at index‬‭
‭https://www.linkedin.com/in/anshulagarwal30‬

‭27. Maximum Sum Increasing Subsequence‬

‭ xplanation:‬
E
‭Similar to LIS, but instead of length, maintain the sum of the increasing subsequence.‬
‭https://www.linkedin.com/in/anshulagarwal30‬

‭ 8. Minimum Swaps to Bring Elements Less Than K‬


2
‭Together‬

‭ 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‬

‭29. Find the Leader Elements in an Array‬

‭ 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‬

‭30. Find the Smallest Missing Positive Integer‬

‭ 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.‬

You might also like