CoreJava – Arrays – Assignment - 2
Arrays Assignment -
2
- Reverse Array:
- Description:
Write a Java program to reverse an array of integers.
- Algorithm:
- Start
with two pointers, start pointing to the first element and end
pointing to the last element of the array.
- Swap
the elements at the start and end pointers.
- Move
the start pointer to the right and the end pointer to the
left.
- Repeat
steps 2-3 until the start pointer surpasses the end
pointer.
- Find Maximum Subarray:
- Description:
Given an array of integers, find the contiguous subarray with the largest
sum.
- Algorithm
(Kadane's Algorithm):
- Initialize
two variables, maxSoFar and maxEndingHere, both set to the
first element of the array.
- Iterate
through the array from the second element:
- Set
maxEndingHere to the maximum of the current element or the sum
of the current element and maxEndingHere.
- Set
maxSoFar to the maximum of maxSoFar and maxEndingHere.
- Return
maxSoFar as the result.
- Find Missing Number:
- Description:
Given an array of integers from 1 to n with one number missing, find the
missing number.
- Algorithm:
- Calculate
the expected sum using the Gauss formula: sum = (n * (n + 1)) / 2.
- Iterate
through the array and calculate the actual sum.
- The
missing number is the difference between the expected sum and the actual
sum.
- Rotate Array:
- Description:
Given an array, rotate the elements to the right by a given number of
steps.
- Algorithm:
- Store
the last element in a temporary variable.
- Shift
all elements to the right starting from the second-to-last element.
- Place
the temporary variable at the beginning of the array.
- Find Duplicates:
- Description:
Given an array of integers, find all the duplicate elements.
- Algorithm:
- Sort
the array to bring duplicate elements together.
- Iterate
through the sorted array and check for adjacent duplicate elements.
- Merge Sorted Arrays:
- Description:
Given two sorted arrays, merge them into a single sorted array.
- Algorithm:
- Create
a new array with a size equal to the sum of the sizes of the two input
arrays.
- Use
two pointers, one for each input array, to compare elements and place
them in the new array in sorted order.
- Continue
this process until all elements from both arrays are merged into the new
array.
- Find Peak Element:
- Description:
Given an array of integers, find a peak element, which is greater than
its neighbors.
- Algorithm:
- Use
binary search to find a peak element by comparing the middle element
with its neighbors.
- If
the middle element is smaller than its adjacent elements, search in the
direction of the larger adjacent element.
- Repeat
the process until a peak element is found.
- Find Kth Largest Element:
- Description:
Given an array of integers, find the kth largest element.
- Algorithm
(Quickselect):
- Choose
a pivot element from the array.
- Partition
the array into two subarrays: elements smaller than the pivot and
elements greater than the pivot.
- If
the pivot index is equal to k, return the pivot element.
- If
the pivot index is greater than k, recursively search in the left
subarray.
- If
the pivot index is less than k, recursively search in the right
subarray.
- Move Zeroes to the End:
- Description:
Given an array of integers, move all the zeroes to the end while
maintaining the order of other elements.
- Algorithm:
- Use
two pointers, i and j, initialized to 0.
- Iterate
through the array with the i pointer.
- If
the element at i is non-zero, swap it with the element at j
and increment j.
- After
iterating through the array, all non-zero elements will be at the
beginning, and j will be the index of the first zero.
- Set
all elements from index j to the end of the array as zero.
- Find Longest Consecutive
Sequence:
- Description:
Given an unsorted array of integers, find the length of the longest
consecutive elements sequence.
- Algorithm:
- Create
a hash set and add all elements from the array to it.
- Iterate
through the array and for each element, check if it is the starting
point of a sequence.
- If
an element is the starting point, keep counting the consecutive elements
by incrementing a counter variable until the sequence ends.
- Update
the maximum length of the sequence encountered so far.
- Return
the maximum length of the consecutive sequence.
- Find Intersection of Two
Arrays:
- Description:
Given two arrays, find their intersection (common elements).
- Algorithm:
- Create
two hash sets, one for each array, and store all elements from each
array in their respective sets.
- Iterate
through one of the sets and check if each element is present in the
other set.
- If
an element is common to both sets, add it to the result set.
- Return
the result set containing the intersection elements.
- Find Median of Two Sorted
Arrays:
- Description:
Given two sorted arrays, find the median element.
- Algorithm:
- Merge
the two sorted arrays into a new sorted array.
- Calculate
the length of the merged array.
- If
the length is odd, return the middle element.
- If
the length is even, return the average of the two middle elements.
- Find Subarray with Given
Sum:
- Description:
Given an array of integers and a target sum, find a subarray that sums up
to the target sum.
- Algorithm:
- Use
two pointers, start and end, initially pointing to the
first element.
- Initialize
a variable currentSum to the value of the first element.
- Iterate
through the array:
- If
currentSum is equal to the target sum, return the subarray from start
to end.
- If
currentSum is less than the target sum, increment end and
add the element at end to currentSum.
- If
currentSum is greater than the target sum, subtract the element
at start from currentSum and increment start.
- If
the target sum is not found, return null or an empty array.
- Sort Colors (Dutch National
Flag Problem):
- Description:
Given an array containing only 0s, 1s, and 2s, sort the array in-place.
- Algorithm
(Dutch National Flag Algorithm):
- Use
three pointers, low, mid, and high, initially
pointing to the first element of the array.
- Iterate
through the array:
- If
the element at mid is 0, swap it with the element at low
and increment both low and mid.
- If
the element at mid is 1, move to the next element by
incrementing mid.
- If
the element at mid is 2, swap it with the element at high
and decrement high.
- Repeat
these steps until mid surpasses high.
- Next Permutation:
- Description:
Given an array of integers, find the next lexicographically greater
permutation.
- Algorithm:
- Starting
from the right, find the first pair of adjacent elements where the left
element is smaller than the right element.
- If
such a pair is found, it means there is a possible permutation.
- Find
the smallest element to the right of the left element that is larger
than the left element.
- Swap
the left and right elements.
- Reverse
the subarray to the right of the original left element.
- If
no such pair is found in step 1, it means the array is in descending
order. In this case, reverse the entire array to get the next
permutation.
- Search in Rotated Sorted
Array:
- Description:
Given a rotated sorted array and a target element, find the index of the
target element (if it exists).
- Algorithm:
- Use
binary search to find the pivot point, which is the element where the
array is rotated.
- Determine
which part of the array (left or right of the pivot) the target element
lies in.
- Perform
a regular binary search on the appropriate side of the pivot to find the
target element.
- Find Duplicate Number:
- Description:
Given an array of integers containing n+1 elements where each integer is
between 1 and n (inclusive), find the duplicate number.
- Algorithm
(Floyd's Tortoise and Hare Algorithm):
- Initialize
two pointers, slow and fast, pointing to the first element
of the array.
- Move
slow one step at a time and fast two steps at a time until
they meet.
- Once
they meet, reset the slow pointer to the first element and keep
the fast pointer where it is.
- Move
both pointers one step at a time until they meet again.
- The
meeting point is the duplicate number.
- Sort Array by Parity:
- Description:
Given an array of integers, sort the array such that all even elements
come before odd elements.
- Algorithm:
- Use
two pointers, left and right, initially pointing to the
first and last elements of the array.
- Move
the left pointer to the right until it encounters an odd element.
- Move
the right pointer to the left until it encounters an even
element.
- Swap
the elements at the left and right pointers.
- Repeat
steps 2-4 until the left pointer surpasses the right
pointer.
- Find All Anagrams in a
String:
- Description:
Given a string and a pattern, find all the starting indices of the
anagrams of the pattern in the string.
- Algorithm:
- Create
two hash arrays to store the character counts for the pattern and the
current window of the string.
- Initialize
two pointers, start and end, pointing to the first element
of the string.
- Iterate
through the string with the end pointer:
- Decrement
the count of the character at end in the window hash array.
- If
the window size exceeds the pattern size, increment the count of the
character at start and move the start pointer to the
right.
- If
the window hash array matches the pattern hash array, add the start
pointer to the result.
- Return
the list of starting indices of the anagrams.
- Valid Sudoku:
- Description:
Determine if a 9x9 Sudoku board is valid.
- Algorithm:
- Create
hash sets for each row, column, and 3x3 subgrid.
- Iterate
through each cell in the Sudoku board.
- Check
if the current cell value is already present in the corresponding row,
column, or subgrid hash set.
- If
it is present, the Sudoku board is not valid.
- If
it is not present, add the value to the corresponding hash sets.
- Repeat
steps 2-5 for all cells in the Sudoku board.
- If
all cells pass the checks, the Sudoku board is valid.
- Spiral Matrix:
- Description:
Given a matrix of m x n elements, return all elements in spiral order.
- Algorithm:
- Initialize
four variables: top, bottom, left, and right,
representing the boundaries of the matrix.
- Initialize
a result array.
- Iterate
in a spiral order:
- Traverse
from left to right along the top row and add each
element to the result array.
- Increment
top to exclude the top row.
- Traverse
from top to bottom along the right column and add
each element to the result array.
- Decrement
right to exclude the rightmost column.
- Traverse
from right to left along the bottom row and add
each element to the result array.
- Decrement
bottom to exclude the bottom row.
- Traverse
from bottom to top along the left column and add
each element to the result array.
- Increment
left to exclude the leftmost column.
- Repeat
steps 3 until all elements are added to the result array.
- Return
the result array.
- Jump Game:
- Description:
Given an array of non-negative integers, determine if you can reach the
last index starting from the first index.
- Algorithm:
- Initialize
a variable maxReach to the first element of the array.
- Iterate
through the array:
- If
maxReach is less than the current index, it means we cannot
reach the current index. Return false.
- Update
maxReach to the maximum of maxReach and the sum of the
current index and its value.
- If
maxReach is greater than or equal to the last index, return
true.
- If
the loop completes, it means we can reach the last index. Return true.
- Two Sum:
- Description:
Given an array of integers and a target value, find two numbers that sum
up to the target.
- Algorithm:
- Create
a hash map to store the complement of each element and its index.
- Iterate
through the array:
- Check
if the complement of the current element exists in the hash map. If it
does, return the indices of the two numbers.
- If
the complement does not exist, add the current element and its index to
the hash map.
- If
no solution is found, return an empty array or null.
- Maximum Subarray Product:
- Description:
Given an array of integers, find the contiguous subarray with the maximum
product.
- Algorithm:
- Initialize
two variables, maxSoFar and minSoFar, both set to the
first element of the array.
- Initialize
a variable maxProduct to maxSoFar.
- Iterate
through the array from the second element:
- Update
maxSoFar and minSoFar by comparing the current element
with the product of the current element and maxSoFar or minSoFar.
- Update
maxProduct to the maximum of maxProduct and maxSoFar.
- Return
maxProduct as the result.
- Valid Mountain Array:
- Description:
Given an array of integers, determine if it is a valid mountain array.
- Algorithm:
- Check
if the array length is at least 3. If not, return false.
- Iterate
through the array until the elements are strictly increasing.
- If
there are no increasing elements or the increasing elements are at the
beginning or end of the array, return false.
- Continue
iterating until the elements are strictly decreasing.
- If
there are no decreasing elements or the decreasing elements reach the
end of the array, return true.
- If
the loop completes without returning, the array is not a valid mountain
array. Return false.
Comments
Post a Comment