Array Implementation Details

C :
Array declaration :
    int A[1000]; // creates an integer array of size 1000. 
    int *A = (int *) malloc(1000 * sizeof(int)); // creates an integer array of size 1000
Accessing ith element :
    A[i] 
C++ :

C style arrays work in C++ as well.

However, C++ provides vectors which is much more powerful than C arrays. We will be using C++ vectors in most of the problems on this judge.

Vector declaration :
    vector<int> V; // declares an empty integer array of size 0. O(1) 
    vector<int> V(100, 1); // declares an integer array of size 100 with all elements initialized to 1. O(size)
Accessing ith element :
    V[i] // O(1)
Size ( number of elements ) of the vector :
    V.size()  // O(1)
Adding element to the vector :
    V.push_back(new_value); // new_value will be appended to the vector.  O(1)
Removing element from end of the vector :
    V.pop_back(); // equivalent to size--; O(1)
JAVA :

Java has multiple ways of representing arrays. For the purpose of problem solving on this site, we will limit ourselves to ArrayList.

Array declaration :
    ArrayList<Integer> A = new ArrayList<Integer>(); // declares an empty integer array. O(1)
    ArrayList<Integer> A = new ArrayList<Integer>(B); // creates a copy of list B.  O(size of B)
Accessing ith element :
    A.get(i)     // O(1)
Setting ith element :
    A.set(i, newValue)  // O(1)
Size of the list :
    A.size()  // O(1) operation
Adding elements to the list :
    A.add(newValue); // appends to the end of the list. O(1) operation. 
    A.add(index, newValue); // add the value at specified index. O(size - index) operation. 
PYTHON :

Python too has multiple ways of representing arrays. lists are used primarily for the purpose. In few cases, tuples are used when we desire the array to be immutable ( its desired that no one can change the content of the array ).

List declaration :
    A = []; # declares an empty list. O(1)
Accessing the ith element :
    A[i]  # O(1)
Adding element to the list :
    A.append(newValue); # O(1) 
Size of the list :
    len(A)  # O(1) 



Walkthrough example :
SPIRAL1

Serious about Learning Programming ?

Learn this and a lot more with Scaler Academy's industry vetted curriculum which covers Data Structures & Algorithms in depth.

Arrays Problems

Value ranges
Problem Score Companies Time Status
Max Min 150
17:31
Merge Intervals 225 78:57
Merge Overlapping Intervals 225 48:24
Arrangement
Hash search
Problem Score Companies Time Status
Occurence of Each Number 200 28:07
Space recycle
Problem Score Companies Time Status
Set Matrix Zeros 300 48:04
Maximum Sum Square SubMatrix 300 58:30
lock
Topic Bonus
Bonus will be unlocked after solving min. 1 problem from each bucket