int A[1000]; // creates an integer array of size 1000.
int *A = (int *) malloc(1000 * sizeof(int)); // creates an integer array of size 1000
A[i]
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<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)
V[i] // O(1)
V.size() // O(1)
V.push_back(new_value); // new_value will be appended to the vector. O(1)
V.pop_back(); // equivalent to size--; O(1)
Java has multiple ways of representing arrays. For the purpose of problem solving on this site, we will limit ourselves to ArrayList.
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)
A.get(i) // O(1)
A.set(i, newValue) // O(1)
A.size() // O(1) operation
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 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 ).
A = []; # declares an empty list. O(1)
A[i] # O(1)
A.append(newValue); # O(1)
len(A) # O(1)
Walkthrough example :
SPIRAL1
Problem | Score | Companies | Time | Status |
---|---|---|---|---|
Pick from both sides! | 100 |
|
56:36 | |
Min Steps in Infinite Grid | 150 |
|
37:51 | |
Minimum Lights to Activate | 200 |
|
75:28 | |
Maximum Sum Triplet | 200 |
|
82:43 | |
Max Sum Contiguous Subarray | 225 |
|
33:39 | |
Add One To Number | 225 |
|
43:43 | |
Maximum Absolute Difference | 250 |
|
65:51 | |
Partitions | 300 |
|
75:27 | |
Maximum Area of Triangle! | 350 |
|
61:49 | |
Flip | 400 |
|
78:22 |
Problem | Score | Companies | Time | Status |
---|---|---|---|---|
Max Min | 150 |
|
17:31 | |
Merge Intervals | 225 |
|
78:57 | |
Merge Overlapping Intervals | 225 |
|
48:24 |
Problem | Score | Companies | Time | Status |
---|---|---|---|---|
Perfect Peak of Array | 200 |
|
49:19 | |
Move Zeroes | 200 |
|
29:37 | |
Make equal elements Array | 200 |
|
37:09 | |
Segregate 0s and 1s in an array | 200 |
|
16:37 | |
Array Sum | 200 |
|
37:42 | |
Kth Row of Pascal's Triangle | 225 |
|
28:32 | |
Spiral Order Matrix II | 225 |
|
48:40 | |
Pascal Triangle | 225 |
|
26:46 | |
Anti Diagonals | 225 |
|
41:46 |
Problem | Score | Companies | Time | Status |
---|---|---|---|---|
Triplets with Sum between given range | 200 |
|
76:31 | |
Balance Array | 200 |
|
63:01 | |
Find Duplicate in Array | 450 |
|
40:13 | |
Maximum Consecutive Gap | 450 |
|
58:46 |
Problem | Score | Companies | Time | Status |
---|---|---|---|---|
Sort array with squares! | 200 |
|
31:22 | |
Largest Number | 225 |
|
70:26 | |
Rotate Matrix | 300 |
|
60:26 | |
Next Permutation | 300 |
|
63:13 | |
Find Permutation | 300 |
|
56:00 |
Problem | Score | Companies | Time | Status |
---|---|---|---|---|
Occurence of Each Number | 200 |
|
28:20 |
Problem | Score | Companies | Time | Status |
---|---|---|---|---|
Noble Integer | 200 |
|
43:30 | |
Reorder Data in Log Files | 200 |
|
49:49 | |
Set Intersection | 200 |
|
57:21 | |
Wave Array | 225 |
|
22:08 | |
Hotel Bookings Possible | 225 |
|
66:06 | |
Max Distance | 250 |
|
68:14 | |
Maximum Unsorted Subarray | 250 |
|
68:52 |
Problem | Score | Companies | Time | Status |
---|---|---|---|---|
Set Matrix Zeros | 300 |
|
48:04 | |
Maximum Sum Square SubMatrix | 300 |
|
58:57 |
Problem | Score | Companies | Time | Status |
---|---|---|---|---|
First Missing Integer | 300 |
|
64:38 | |
Repeat and Missing Number Array | 350 |
|
63:55 | |
N/3 Repeat Number | 600 |
|
68:22 |