Must Learn Concepts in Arrays to solve any code!

Must Learn Concepts in Arrays to solve any code!

Subscribe to my newsletter and never miss my upcoming articles

Hello everyone!!💖

Today is all about arrays🤩. We know array is the most commonly used data structure of all. I guess most of us have got stuck with array concepts while coding, right? Why late? let's learn it perfectly so that we can solve any problem using an array.


Basic Operations


So, before starting with concepts we should be clear with the common syntax like declaration, instantiation, initialization, passing an array to method, etc.

Note: I will be providing java code with a Github link for every concept I discuss here!😊

  1. Insertion of an element

  2. Deletion

    int[] arr = {1, 2, 3, 4, 5}
    del value=3
    //after deletion
    arr=[1, 2, 4, 5]

    CODE:- Deleting an element from array

  3. Sorting

    CODE:- code for sort

    In this link, I have not used in-built methods or did not write a well-optimised code. Solved it in a way that you could understand😋(if newbie).

  4. Searching an element

    int[] arr = {1, 2, 3, 4, 5}
    search val=7
    if found, print true
    else false

    CODE:- Searching an element in array

  5. Reversing an array

    int[] arr = {1, 2, 3, 4, 5}
    //after reversing
    arr=[5, 4, 3, 2, 1]

    CODE:- Code for reversing an array

Rotating array using juggling algorithm

By name, we can understand that rotation means simply shifting elements of an array by a specified number of positions. Rotations can be of 2 types: left and right. See below examples to get a clear understanding of what it is.

Left Rotation: Here, k=2 (where k is the number of positions to be shifted left).

left rotate.PNG

Right Rotation: Here, k=2 (where k is the number of positions to be shifted right).

right rotate.PNG

Check this article to know more! Juggling Algorithm😊

Sliding Window Technique

Consider the following example, find Maximum sum of 'k' consecutive elements in an array.

arr[] = {5, 2, -1, 0, 3}
         k = 3
Output : 6

To find this we generally use nested for-loop. So, this technique shows how a nested for loop in a few problems can be converted to a single for loop and hence reducing the time complexity.

The Approach:




CODE:- Sliding Window Technique Code

Prefix sum array

Input  : arr[] = {10, 20, 10, 5, 15}
Output : prefixSum[] = {10, 30, 40, 45, 60}

We use this concept in many problems in long run. So, be sure you learn this.🤗

CODE:- Code for Prefix sum array

Bonus concepts and problems

Now, try solving problems after learning all these concepts and let me know your approach and views.🤗

Thank you! Will meet you all in my next article.😍

Articles you may like

Share this
Proudly part of