This repository contains a collection of Java array interview questions along with their solutions in the form of Java programs.
Arrays are fundamental data structures in Java, allowing you to store multiple values of the same type in a single variable. An array in Java is a container object that holds a fixed number of values of a single data type. Arrays can be of any data type, including primitive types such as int, char, double, etc., or reference types such as objects.
Arrays in Java are zero-indexed, meaning the index of the first element is 0, the index of the second element is 1, and so on. Arrays have a fixed size, which is determined at the time of their creation and cannot be changed dynamically. However, Java provides dynamic arrays through ArrayList, which can resize itself dynamically.
dataType[] arrayName;
Initialization:
dataType[] arrayName = new dataType[arraySize];
Example:
// Declaration
int[] numbers;
// Initialization
numbers = new int[5];
// Array Literal Initialization
int[] numbers = {1, 2, 3, 4, 5};
Accessing Elements:
arrayName[index];
Example:
int[] numbers = {10, 20, 30, 40, 50};
System.out.println(numbers[0]); // Output: 10
System.out.println(numbers[2]); // Output: 30
Array Length:
arrayName.length;
Example:
int[] numbers = {10, 20, 30, 40, 50};
System.out.println(numbers.length); // Output: 5
Definition: A multidimensional array in Java is an array of arrays. It allows you to store elements in multiple dimensions, such as rows and columns.
Declaration and Initialization:
// Declaration
int[][] matrix;
// Initialization
matrix = new int[3][4]; // Creates a 2D array with 3 rows and 4 columns
Accessing Elements:
Elements of a multidimensional array are accessed using multiple indices. For a 2D array, you need two indices: one for the row and one for the column.
matrix[0][0] = 1; // Assigns 1 to the element at the first row and first column
int y = matrix[1][2]; // Retrieves the value of the element at the second row and third column
Initialization with Values:
You can initialize a multidimensional array with specific values at the time of declaration.
int[][] matrix = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} }; // Initializes a 2D array with specific values
Ragged Arrays:
In Java, multidimensional arrays can have different lengths for each row. Such arrays are called ragged arrays.
int[][] raggedArray = { {1, 2}, {3, 4, 5}, {6} }; // Initializes a ragged 2D array
- WAP to create an array of size n
- WAP to calculate sum of all the array elements
- WAP to product of all the array elements
- WAP to calculate the difference of sum of even elements and odd elements
- WAP to display all numbers between two given range
- WAP to reverse an array
- WAP to find largest element from an array
- WAP to find smallest element from an array
- WAP to find the difference of maximum and minimum element of an array
- WAP to find the second largest element in an array
- WAP to find the second smallest element in an array
- WAP to sort an array in ascending order
- WAP to sort an array in descending order
- WAP to Sort first half in ascending order and second half in descending
- WAP to Finding the frequency of elements in an array
- WAP to Sorting elements of an array by frequency
- WAP to Finding the Longest Palindrome in an Array
- WAP to Counting Distinct Elements in an Array
- WAP to Finding Repeating elements in an Array
- WAP to Finding Non Repeating elements in an Array
- WAP to Removing Duplicate elements from an array
- WAP to Finding Minimum scalar product of two vectors
- WAP to Finding Maximum scalar product of two vectors in an array
- WAP to Counting the number of even and odd elements in an array
- WAP to Find all Symmetric pairs in an array
- WAP to Find maximum product sub-array in a given array
- WAP to Finding Arrays are disjoint or not
- WAP to Determine Array is a subset of another array or not
- WAP to Determine can all numbers of an array be made equal
- WAP to Finding Minimum sum of absolute difference of given array
- WAP to Sort an array according to the order defined by another array
- WAP to Replace each element of the array by its rank in the array
- WAP to Finding equilibrium index of an array
- WAP to left rotate an array by k position
- WAP to right rotate an array by k position
- WAP to find Balanced Parenthesis
- WAP to sort an array which consists of only 0, 1 and 2.
- WAP to Find the “Kth” max and min element of an array
- WAP to Move all the negative elements to one side of the array
- WAP to Find the Union and Intersection of the two sorted arrays.
- WAP to Find Largest sum contiguous Subarray
- WAP to Minimize the maximum difference between heights
- WAP to Minimum no. of Jumps to reach the end of an array
- WAP to Find duplicate in an array of N+1 Integers
- WAP to Merge 2 sorted arrays without using extra space
- WAP for Kadane’s Algorithm
- WAP for Merge Intervals
- WAP for Count Inversion
- WAP for Best time to buy and Sell stock
- WAP to Find all pairs on integer array whose sum is equal to given number
- WAP to Find if there is any subarray with sum equal to 0
- WAP to Find factorial of a Large Number
- WAP to Find common elements In 3 sorted arrays
- WAP to Rearrange the array in alternating positive and negative items
- WAP to Find all elements that appear more than ” n/k ” times
- WAP to Maximize profit by buying and selling a share at-most twice
- WAP to find Next Permutation
- WAP to Find longest consecutive subsequence
- WAP for Trapping Rain water problem
- WAP for Chocolate Distribution problem
- WAP to find Smallest Subarray with sum greater than a given value
- WAP for Three way partitioning of an array around a given value
- WAP that require Minimum no. of operations to make an array palindrome
- WAP to find Median of 2 sorted arrays of equal size
- WAP to find Median of 2 sorted arrays of different size
- WAP to Find a peak element which is not smaller than its neighbours
- WAP to Find the missing integer
- WAP to Count Pairs with the given sum
- WAP to Find a triplet that sums to a given value
- WAP for Coin Change Problem