🔢Java Arrays Programs
Array problems are where interviewers check whether you can move from a brute-force loop to an efficient one-pass solution. These programs cover searching, sorting, two-pointer tricks and 2D matrices, each with its time and space complexity.68 programs · each with full code, output and a step-by-step explanation
Most Asked in Interviews
Adds two matrices of the same size by summing the values at each matching row and column.
View Program →Calculates the average of an array's elements.
View Program →Determines whether an array's elements are in ascending order.
View Program →Checks whether two arrays hold the same elements in the same order.
View Program →Joins two arrays end to end into a single array.
View Program →Creates an independent copy of an array.
View Program →Counts how many even numbers appear in an array.
View Program →Counts how many negative numbers appear in an array.
View Program →Counts how many odd numbers appear in an array.
View Program →Counts how many positive numbers appear in an array.
View Program →Counts how many zero values appear in an array.
View Program →Declares an array and prints every element it holds.
View Program →Scans an array to find its largest value.
View Program →Scans an array to find its second-largest value.
View Program →Scans an array to find its second-smallest value.
View Program →Scans an array to find its smallest value.
View Program →Adds up the primary and secondary diagonals of a square matrix separately.
View Program →Scans an array to find its third-largest value.
View Program →Searches an array for a target value by checking every element in order.
View Program →Moves every zero in an array to the end, keeping the non-zero elements in order.
View Program →Reads an array's size and elements from the console.
View Program →Removes repeated elements from an array, keeping each one's first occurrence.
View Program →Reverses the elements of an integer array in place.
View Program →Rotates an array's elements to the left by a given number of positions.
View Program →Rotates an array's elements to the right by a given number of positions.
View Program →Splits an array's elements into separate lists of even and odd numbers.
View Program →Splits an array's elements into separate lists of positive and negative numbers.
View Program →Sorts an array into ascending order using the built-in sort method.
View Program →Sorts an array into descending order.
View Program →Adds up every element of an array.
View Program →Flips a matrix over its main diagonal so that every row becomes a column.
View Program →Searches a sorted array for a target value by repeatedly halving the search range.
View Program →Sorts an array by repeatedly swapping adjacent out-of-order elements.
View Program →Sorts small-range integers without comparisons by tallying how often each value appears.
View Program →Finds every element that appears more than once in an array.
View Program →Finds the index where the sum of elements to its left equals the sum to its right.
View Program →Counts how many times each element appears in an array.
View Program →Finds every element that appears in both of two arrays.
View Program →Finds every element that is greater than all the elements to its right.
View Program →Finds the element that appears least often in an array.
View Program →Finds an element that is smaller than both of its neighbors.
View Program →Finds the largest value obtainable by subtracting an earlier element from a later one.
View Program →Finds the smallest absolute difference between any two elements in an array.
View Program →Finds the smallest sum achievable by any contiguous subarray.
View Program →Finds the single missing number from a sequence of 1 to n.
View Program →Finds the element that appears most often in an array.
View Program →Finds two numbers in an array that add up to a target sum.
View Program →Finds an element that is greater than both of its neighbors.
View Program →Finds the single number that repeats in an array of n + 1 values from 1 to n.
View Program →Finds a contiguous subarray whose elements add up to a target sum.
View Program →Finds every distinct element present in either of two arrays.
View Program →Finds the single element that appears once while every other element appears exactly twice.
View Program →Sorts an array by inserting each element into its correct position among the ones already sorted.
View Program →Sorts an array by splitting it in half, sorting each half recursively and merging the sorted halves.
View Program →Merges two sorted arrays into a single sorted array.
View Program →Rearranges an array in place so every negative number comes before the positives.
View Program →Multiplies two matrices by pairing each row of the first with each column of the second.
View Program →Reads a matrix clockwise from the outside in, peeling off one ring of cells at a time.
View Program →Sorts an array by partitioning it around a pivot, then sorting the smaller and larger sides recursively.
View Program →Rotates an array by k positions in place using the reversal algorithm.
View Program →Sorts an array by repeatedly selecting the smallest remaining element.
View Program →Finds the kth largest value in an array with a size-k min-heap, without sorting the whole array.
View Program →Finds the length of the longest run of consecutive integers in an unsorted array.
View Program →Finds the element that appears more than half the time in an array.
View Program →Finds the largest product achievable by any contiguous subarray.
View Program →Finds the largest sum achievable by any contiguous subarray, using Kadane's algorithm.
View Program →Finds three numbers in an array that add up to a target sum.
View Program →Sorts an array by arranging it as a max-heap and repeatedly moving the largest value to the end.
View Program →