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