Arrays Data Structure
Introduction
- 1. Array is a linear data structure.
- 2. Array is a fixed in size, it means once we set the size of an array it will not grow during the runtime.
-
3. Array is a collection of similar data elements. it means if we create the integer type array will be able to store only integer values.
if we store the string arrays then will be able to store only string values
Example:
- 1. int[] intArr = new int[2]; //Store only integer values
- 2. String[] strArr = new String[2] //Store only String values
- 4. Array is one of the simple and popular data structure used in programming language.
- 5. In array all the elements are stored in contiguous memory location which help the program to efficiently access and manipulate the elements
Declaration of an Array
In Java Array can declared like below:
- 1. int arr[]; // integer array declaration
- 2. char arr[]; // character array declaration
- 3. float arr[]; // float array declaration
Initialization of an Array
In Java Array can initialized like below:
- 1. int arr[] = { 1, 2, 3, 4, 5 }; // integer array Initialization
- 2. char arr[] = { 'a', 'b', 'c', 'd', 'e' }; // character array Initialization
- 3. float arr[] = { 1.4f, 2.0f, 24f, 5.0f, 0.0f }; // float array Initialization
Array Representations
Types of an Array
Arrays are two different types:
1. One dimensional array
2. Multi dimensional array
One dimensional array
In Java one dimensional arrays are stored in a single row like shown in below image. in one dimensional arrays
the elements are stored one after aother. In one dimensional array the elements can be accessed based on the
index by iterating a single loop one by one like below.
Multi dimensional array
Array of array is nothing but multi dimensional array, multi dimensional array is used to store the complex
data structure. 2 dimensional (2D), 3 dimensional(3D), 4 dimensional(4D) so on will be considered a multi dimensional array.
In multi dimensional array the data will be stored as rows and columns like below.
- 2D array can be iterated with the help of two for loops
- 3D array can be iterated with the help of three for loops
- 4D array can be iterated with the help of four for loops
2-D Multidimensional arrays can be considered as an array of arrays or as a matrix consisting of rows and columns.
Array coding problem
- Find the largest / smallest element in an array
- Reverse an array
- Check if array is sorted
- Count even and odd numbers
- Sum of all elements
- Find second largest element
- Find missing number (1 to N)
- Find duplicate in array
- Linear search
- Binary search
- Move all zeros to the end
- Move all negative numbers to one side
- Remove duplicates from sorted array
- Rotate array by K positions
- Insert element at a given index
- Delete element from array
- Find frequency of each element
- Find common elements in two arrays
- Replace each element with greatest on right
- Find leaders in an array
- Two Sum
- Pair with given sum
- Remove duplicates from unsorted array
- Sort array of 0s, 1s, 2s (Dutch National Flag)
- Merge two sorted arrays
- Reverse words in an array/string
- Squares of a sorted array
- Maximum sum subarray of size K
- First negative number in every window of size K
- Longest subarray with sum ≤ K
- Count subarrays with given sum
- Subarray with zero sum
- Equilibrium index
- Find subarray with given sum
- Longest consecutive sequence
- Count frequency using hashmap
- Majority element (Boyer Moore)
- Maximum subarray sum (Kadane’s Algorithm)
- Maximum product subarray
- Minimum subarray sum
- Spiral matrix traversal
- Rotate matrix by 90 degrees
- Search in a sorted matrix
- Set matrix zeros
- Diagonal traversal
- Trapping Rain Water
- Best time to buy and sell stock (I, II, III)
- Product of array except self
- Find minimum number of jumps to reach end
- Advanced Hashing / Logic
- Subarrays divisible by K
- Kth largest element
- Find repeating and missing number
- Majority element II (> n/3 times)
- Longest increasing subsequence (Array DP)