Topic 1: Introduction To Array: Remember: "Location of The Next Index Depends On The Data Type We Use"
Topic 1: Introduction To Array: Remember: "Location of The Next Index Depends On The Data Type We Use"
An array is a collection of items of the same data type stored at contiguous memory
locations. This makes it easier to calculate the position of each element by simply
adding an offset to a base value, i.e., the memory location of the first element of the
array (generally denoted by the name of the array).
For simplicity, we can think of an array as a fleet of stairs where, on each step is
placed a value (let’s say one of your friends). Here, you can identify the location of
any of your friends by simply knowing the count of the step they are on.
Remember: “Location of the next index depends on the data type we use”.
The above image can be viewed as a top-level view of a staircase where you are at
the base of the staircase. Each element can be uniquely identified by their index in
the array (in a similar way as you could identify your friends by the step on which
they were on in the above example).
Defining an Array: Array definition is similar to defining any other variable. There
are two things that need to be kept in mind, data type of the array elements and
the size of the array. The size of the array is fixed and the memory for an array
needs to be allocated before use, size of an array cannot be increased or decreased
dynamically.
dataType arrayName[arraySize];
Examples:
Searching in an Array
Searching an element in an array means to check if a given element is present in an
array or not. This can be done by accessing elements of the array one by one
starting from the first element and checking if any of the elements matches with the
given element.
We can use loops to perform the above operation of array traversal and access the
elements using indexes.
Time Complexity of this search operation will be O(N) in the worst case as we are
checking every element of the array from 1st to last, so the number of operations is
N.
Topic 2: Insertion and Deletion in Arrays
Insertion in Arrays
Given an array of a given size. The task is to insert a new element in this array.
There are two possible ways of inserting elements in an array:
Special Case: A special case needs to be considered, whether the array is already
full or not. If the array is full, then the new element can not be inserted.
Consider the given array is arr[] and the initial size of the array is N, that is the array
can contain a maximum of N elements and the length of the array is len. That is,
there are len number of elements already present in this array.
● Insert an element K at end in arr[]: The first step is to check if there is any
space left in the array for the new element. To do this check,
if(len < N)
// space left
else
// array is full
If there is space left for the new element, insert it directly at the end at
position len + 1 and index len:
arr[len] = k;
if(len < N)
// space left
else
// array is full
Now, if there is space left, the element can be inserted. The index of the new
element will be idx = pos - 1.
Now, before inserting the element at the index idx, shift all elements from the
index idx till end of the array to the right by 1 place. This can be done as:
arr[idx] = K;
Time Complexity in worst case of this insertion operation can be linear i.e.
O(N) as we might have to shift all of the elements by one place to the right.
Deletion in Arrays
To delete a given element from an array, we will have to first search the element in
the array. If the element is present in the array then delete operation is performed for
the element otherwise the user is notified that the array does not contain the given
element.
Consider the given array is arr[] and the initial size of the array is N, that is the array
can contain a maximum of N elements and the length of the array is len. That is,
there are len number of elements already present in this array.
Deleting an element K from the array arr[]: Search the element K in the array arr[]
to find the index at which it is present.
len = len-1;
Time Complexity in worst case of this insertion operation can be linear i.e. O(N) as
we might have to shift all of the elements by one place to the left.
Topic 3 : Array Rotation
As the term rotation signifies, array rotation means to rotate the elements of an array
by given positions.
Note: The similar approach can also be applied for clockwise array rotation.
Implementations
● Simple Method: The simplest way to rotate an array is to implement the
above visually observed approach by using extra space.
leftRotate(arr[], d, n)
start
For i = 0 to i < d
Left rotate all elements of arr[] by one
end
Reversing an array means reversing the order of elements in the given array.
Problem: Given an array of N elements. The task is to reverse the order of elements
in the given array.
For Example:
Iterative Solution
● Method 1 (Using Temporary Array): The idea is to first copy all of the
elements of the given array in a temporary array. Then traverse the temporary
array from end and replace elements in original array by elements of temp
array.
Recursive Solution
The recursive approach is almost similar to that of the method 2 of the iterative
solution. Below is the recursive algorithm to reverse an array:
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.
Let’s start with a problem for illustration where we can apply this technique:
The Naive Approach to solve this problem is to calculate sum for each of the blocks
of K consecutive elements and compare which block has the maximum sum
possible. The time complexity of this approach will be O(n * k).
The above problem can be solved in Linear Time Complexity by using Window
Sliding Technique by avoiding the overhead of calculating sum repeatedly for each
block of k elements.
The technique can be best understood with the window pane in bus, consider a
window of length n and the pane which is fixed in it of length k. Consider, initially the
pane is at extreme left i.e., at 0 units from the left. Now, co-relate the window with
array arr[] of size n and plane with current_sum of size k elements. Now, if we apply
force on the window such that it moves a unit distance ahead. The pane will cover
the next k consecutive elements.
The below representation will make it clear how the window slides over the array.
This is the initial phase where we have calculated the initial window sum starting
from index 0 . At this stage the window sum is 6. Now, we set the maximum_sum as
current_window i.e 6.
Now, we slide our window by a unit index. Therefore, now it discards 5 from the
window and adds 0 to the window. Hence, we will get our new window sum by
subtracting 5 and then adding 0 to it. So, our window sum now becomes 1. Now, we
will compare this window sum with the maximum_sum. As it is smaller we want to
change the maximum_sum.
Similarly, now once again we slide our window by a unit index and obtain the new
window sum to be 2. Again we check if this current window sum is greater than the
maximum_sum till now. Once, again it is smaller so we don't change the
maximum_sum.
Prefix Sum Array: The prefix sum array of any array, arr[] is defined as an array of
same size say, prefixSum[] such that the value at any index i in prefixSum[] is sum of
all elements from indexes 0 to i in arr[].
That is,
Examples:
Below function generates a prefix sum array for a given array arr[] of size N:
1
2
3
4
5
6
7
8
9
10
11
void fillPrefixSum(int arr[], int N, int prefixSum[])
{
prefixSum[0] = arr[0];
Finding sum in a Range: We can easily calculate the sum with-in a range [i, j] in an
array using the prefix sum array. Since the array prefixSum[i] stores the sum of all
elements upto i. Therefore, prefixSum[j] - prefixSum[i] will give:
sum of elements upto j-th index - sum of elements upto i-th element
The above total sum will exclude the i-th element.
prefixSum[j] - prefixSum[i-1]
Therefore,
sumInRange = prefixSum[j] , if i = 0
otherwise,
Sample Problem: Consider an array of size N with all initial values as 0. Perform
given 'm' add operations from index 'a' to 'b' and evaluate the highest element in the
array. An add operation adds 100 to all elements from index a to b (both inclusive).
Example:
Explanation :
After I operation -
A : 0 100 100 100 0
After II operation -
A : 100 200 200 100 0
After I operation -
A : 0 100 0 0 -100
Prefix Sum Array : 0 100 100 100 0
After II operation -
A : 100 100 0 -100 -100
Prefix Sum Array : 100 200 200 100 0
Some of the most commonly used classes for implementing sequential lists or arrays
are:
● Vector
● List
Vector
Vector in C++ STL is a class that represents a dynamic array. The advantages of
vector over normal arrays are,
Vectors are the same as dynamic arrays with the ability to resize itself automatically
when an element is inserted or deleted, with their storage being handled
automatically by the container. Vector elements are placed in contiguous storage so
that they can be accessed and traversed using iterators. In vectors, data is inserted
at the end. Inserting at the end takes differential time, as sometimes there may be a
need of extending the array. Removing the last element takes only constant time
because no resizing happens. Inserting and erasing at the beginning or in the middle
is linear in time.
To use the Vector class, include the below header file in your program:
Declaring Vector:
vector< Type_of_element > vector_name;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
// C++ program to illustrate the above functions
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int> v;
// Push elements
for (int i = 1; i <= 5; i++)
v.push_back(i);
cout << "Size : " << v.size();
Size : 5
Vector is not empty
Output of begin and end: 1 2 3 4 5
The first element is: 5
The last element is: 20
Vector size after erase(): 0
List
Lists are sequence containers that allow non-contiguous memory allocation. List in
C++ STL implements a doubly linked list and not arrays. As compared to vector, list
has slow traversal, but once a position has been found, insertion and deletion are
quick. Normally, when we say a List, we talk about doubly linked lists. For
implementing a singly linked list, we can use forward_list class in C++ STL.
To use the List class, include the below header file in your program:
Declaring List:
List 1 (gqlist1) is : 0 2 4 6
8 10 12 14 16 18
List 2 (gqlist2) is : 27 24 21 18
15 12 9 6 3 0
gqlist1.front() : 0
gqlist1.back() : 18
gqlist1.pop_front() : 2 4 6 8
10 12 14 16 18
gqlist2.pop_back() : 27 24 21 18
15 12 9 6 3
gqlist1.reverse() : 18 16 14 12
10 8 6 4 2
gqlist2.sort(): 3 6 9 12
15 18 21 24 27
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include<iostream>
int main()
vector<int> ar = { 1, 2, 3, 4, 5 };
vector<int>::iterator ptr;
// Displaying vector elements using begin() and end()
return 0;
Run
Output:
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
// advance()
#include<iostream>
#include<iterator> // for iterators
int main()
vector<int> ar = { 1, 2, 3, 4, 5 };
// points to 4
advance(ptr, 3);
return 0;
Run
Output:
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include<iostream>
int main()
vector<int> ar = { 1, 2, 3, 4, 5 };
// points to 4
// points to 3
Run
Output:
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
// inserter()
#include<iostream>
int main()
vector<int> ar = { 1, 2, 3, 4, 5 };
advance(ptr, 3);
return 0;
Run
Output:
Arrays in Java are used to store a group of elements of the same data type at
contiguous memory locations.
type array-name[];
OR
type[] array-name;
An array declaration has two components: the type and the name. Type declares the
element type of the array. The element type determines the data type of each
element that comprises the array. Like an array of int type, we can also create an
array of other primitive data types like char, float, double..etc or user defined data
type(objects of a class). Thus, the element type for the array determines what type of
data the array will hold.
For Example:
// both are valid declarations
int intArray[];
or int[] intArray;
byte byteArray[];
short shortsArray[];
boolean booleanArray[];
long longArray[];
float floatArray[];
double doubleArray[];
char charArray[];
Here,
Accessing Java Array Elements using for Loop: Each element in the array is
accessed by its index. The index begins with 0 and ends at (total array size)-1. All
the elements of the array can be accessed using Java for Loop as shown below.
Implementation:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
// Java program to illustrate creating an array
// of integers, puts some values in the array,
// and prints each value to standard output.
class GFG
{
public static void main (String[] args)
{
// declares an Array of integers.
int[] arr;
// so on...
arr[2] = 30;
arr[3] = 40;
arr[4] = 50;
Element at index 0 : 10
Element at index 1 : 20
Element at index 2 : 30
Element at index 3 : 40
Element at index 4 : 50
Java also provides some inbuilt classes which can be used for implementing arrays
or sequential lists. Let's look at some of these in detail.
ArrayList in Java
ArrayList is a part of the collection framework and is present in the java.util package.
It provides us dynamic arrays in Java. Though, it may be slower than standard
arrays but can be helpful in programs where lots of array manipulation is needed.
Implementation:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
// Java program to demonstrate working of
// ArrayList in Java
import java.io.*;
import java.util.*;
class arrayli
{
public static void main(String[] args)
throws IOException
{
// size of ArrayList
int n = 5;
// Printing elements
System.out.println(arrli);
Run
Output:
[1, 2, 3, 4, 5]
[1, 2, 3, 5]
1 2 3 5
Constructor:
Implementation:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// Java code to illustrate Vector
import java.util.*;
class Vector_demo {
public static void main(String[] arg)
{
// Create a vector
Vector<Integer> v = new Vector<Integer>();
Vector is [1, 2, 3, 4, 3]
Topic 10 : Sample Problems on Array
Input
[4, 5, 3, 2, 5]
3
0 3
2 4
1 3
Output
14 (4+5+3+2)
10 (3+2+5)
10 (5+3+2)
Solution : The numbers of queries are large. It will be very inefficient to iterate over the
array and calculate the sum for each query separately. We have to devise the solution so
that we can get an answer to the query in constant time. We will be storing the sum upto a
particular index in the prefix sum Array. We will be using the prefix sum array to calculate the
sum for the given range.
Pseudo Code
// n : size of array
// q : Number of queries
// l, r : Finding Sum of range between index l and r
// l and r (inclusive) and 0 based indexing
void range_sum(arr, n)
{
prefix[n] = {0}
prefix[0] = arr[0]
for i = 1 to n-1 :
prefix[i] = a[i] + prefix[i-1]
for (i = 1 to q )
{
if (l == 0)
{
ans = prefix[r]
print(ans)
}
else
{
ans = prefix[r] - prefix[l-1]
print(ans)
}
}
}
if leftsum == rightsum :
return i
}
Time Complexity : O(n^2)
Auxiliary Space : O(1)
Tricky Solution : The idea is to get total sum of array first. Then Iterate through the array
and keep updating the left sum which is initialized as zero. In the loop, we can get right sum
by subtracting the elements one by one. Then check whether Leftsum and Rightsum are
equal.
Pseudo Code
// n : size of array
int eqindex(arr, n)
{
sum = 0
leftsum = 0
for (i=0 to n-1)
sum += arr[i]
Input
[-3, 4, -1, -2, 1, 5]
Output
7
(4+(-1)+(-2)+1+5)
Solution : Simple idea is to look for all positive contiguous segments of the array
(max_ending_here is used for this). And keep track of the maximum sum contiguous
segment among all positive segments (max_so_far is used for this). Each time we get a
positive sum, compare it with max_so_far and if it is greater than max_so_far, update
max_so_far.
Pseudo Code
//n : size of array
int largestsum(arr, n)
{
max_so_far = INT_MIN
max_ending_here = 0
if max_ending_here < 0 :
max_ending_here = 0
}
return max_so_far
}
Time Complexity : O(n)
Auxiliary Space : O(1)
Input
1 3 4 6
2 5 7 8
Output
1 2 3 4 5 6 7 8
Solution : Idea is to traverse both arrays simultaneously and compare the current numbers
from the both the Arrays. Pick the smaller element and copy to arr3[ ] and advance the
current index of the array, from where the smaller element is picked. When we reach the end
of one of the array, Copy the remaining elements of another array to arr3[ ].
Pseudo Code
// input arrays - arr1(size m), arr2(size n)
void merge_sorted(arr1, arr2, m, n)
{
arr3[m+n] // merged array
i=0,j=0,k=0
while(i < m && j < n)
{
if arr1[i] < arr2[j] :
arr3[k++] = arr1[i++]
else :
arr3[k++] = arr2[j++]
}
while(i < m)
arr3[k++] = arr1[i++]
while(j < n)
arr3[k++] = arr2[j++]
}
Time Complexity : O(m+n)
Auxiliary Space : O(m+n)