Arrays Data Structure

Introduction

  1. 1. Array is a linear data structure.
  2. 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. 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. 1. int[] intArr = new int[2]; //Store only integer values
    2. 2. String[] strArr = new String[2] //Store only String values
  4. 4. Array is one of the simple and popular data structure used in programming language.
  5. 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. 1. int arr[]; // integer array declaration
  2. 2. char arr[]; // character array declaration
  3. 3. float arr[]; // float array declaration

Initialization of an Array

In Java Array can initialized like below:

  1. 1. int arr[] = { 1, 2, 3, 4, 5 }; // integer array Initialization
  2. 2. char arr[] = { 'a', 'b', 'c', 'd', 'e' }; // character array Initialization
  3. 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.

  1. 2D array can be iterated with the help of two for loops
  2. 3D array can be iterated with the help of three for loops
  3. 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
  1. Find the largest / smallest element in an array
  2. Reverse an array
  3. Check if array is sorted
  4. Count even and odd numbers
  5. Sum of all elements
  6. Find second largest element
  7. Find missing number (1 to N)
  8. Find duplicate in array
  9. Linear search
  10. Binary search
  11. Move all zeros to the end
  12. Move all negative numbers to one side
  13. Remove duplicates from sorted array
  14. Rotate array by K positions
  15. Insert element at a given index
  16. Delete element from array
  17. Find frequency of each element
  18. Find common elements in two arrays
  19. Replace each element with greatest on right
  20. Find leaders in an array
  21. Two Sum
  22. Pair with given sum
  23. Remove duplicates from unsorted array
  24. Sort array of 0s, 1s, 2s (Dutch National Flag)
  25. Merge two sorted arrays
  26. Reverse words in an array/string
  27. Squares of a sorted array
  28. Maximum sum subarray of size K
  29. First negative number in every window of size K
  30. Longest subarray with sum ≤ K
  31. Count subarrays with given sum
  32. Subarray with zero sum
  33. Equilibrium index
  34. Find subarray with given sum
  35. Longest consecutive sequence
  36. Count frequency using hashmap
  37. Majority element (Boyer Moore)
  38. Maximum subarray sum (Kadane’s Algorithm)
  39. Maximum product subarray
  40. Minimum subarray sum
  41. Spiral matrix traversal
  42. Rotate matrix by 90 degrees
  43. Search in a sorted matrix
  44. Set matrix zeros
  45. Diagonal traversal
  46. Trapping Rain Water
  47. Best time to buy and sell stock (I, II, III)
  48. Product of array except self
  49. Find minimum number of jumps to reach end
  50. Advanced Hashing / Logic
  51. Subarrays divisible by K
  52. Kth largest element
  53. Find repeating and missing number
  54. Majority element II (> n/3 times)
  55. Longest increasing subsequence (Array DP)