0% found this document useful (0 votes)
11 views6 pages

Array

An array is a collection of elements accessed by an index, with 1D and 2D arrays being the primary types. The document provides syntax examples and operations for both types in Python, C++, and Java, including cumulative sums, concatenation, printing elements, summing values, transposing matrices, finding maximum elements, and counting even and odd numbers. It illustrates these concepts with code snippets for better understanding.

Uploaded by

Fighter Flash
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views6 pages

Array

An array is a collection of elements accessed by an index, with 1D and 2D arrays being the primary types. The document provides syntax examples and operations for both types in Python, C++, and Java, including cumulative sums, concatenation, printing elements, summing values, transposing matrices, finding maximum elements, and counting even and odd numbers. It illustrates these concepts with code snippets for better understanding.

Uploaded by

Fighter Flash
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

What is an Array?

An array is a collection of elements (like numbers or characters)

stored under a single variable name, and each element is accessed

using an index. Part 1: 1D Array (One-Dimensional Array)

A 1D array is like a list of items in a row. Example:

Marks = [10, 20, 30, 40]

Index 0 1 2 3

✅ Syntax:

Language Syntax

Python arr = [10, 20, 30]

C++ int arr[3] = {10, 20, 30};

Java int[] arr = {10, 20, 30};

✅ 1. [LeetCode 1480] Running Sum of 1D Array

� Compute cumulative sum

Python

def runningSum(nums):

for i in range(1, len(nums)):

nums[i] += nums[i - 1]

return nums

C++

vector<int> runningSum(vector<int>& nums) {

for(int i = 1; i < [Link](); i++)

nums[i] += nums[i - 1];

return nums;

Java
int[] runningSum(int[] nums) {

for (int i = 1; i < [Link]; i++)

nums[i] += nums[i - 1];

return nums;

✅ 2. [LeetCode 1929] Concatenation of


Array
Duplicate array into one
Python

def getConcatenation(nums):

return nums + nums

C++

vector<int> getConcatenation(vector<int>& nums) {

vector<int> res = nums;

[Link]([Link](), [Link](), [Link]());

return res;

Java

int[] getConcatenation(int[] nums) {

int[] res = new int[[Link] * 2];

for(int i = 0; i < [Link]; i++) {

res[i] = nums[i];

res[i + [Link]] = nums[i];

return res;

}
Part 2: 2D Array (Two-Dimensional Array)
A 2D array is like a grid or table — an array of arrays.

Example (2 rows × 3 columns):

arr = [

[1, 2, 3],

[4, 5, 6]

✅ Syntax:

Language Syntax

Python arr = [[1, 2], [3, 4]]

C++ int arr[2][2] = {{1,2}, {3,4}};

Java int[][] arr = {{1,2},{3,4}};

1. Print all elements (row-wise)


Python

arr = [[1, 2], [3, 4]]for row in arr:

for val in row:

print(val, end=" ")

print()

C++

int arr[2][2] = {{1, 2}, {3, 4}};for (int i = 0; i < 2; i++) {

for (int j = 0; j < 2; j++)

cout << arr[i][j] << " ";

cout << endl;

Java

int[][] arr = {{1, 2}, {3, 4}};for (int i = 0; i < [Link]; i++) {

for (int j = 0; j < arr[i].length; j++)


[Link](arr[i][j] + " ");

[Link]();

2. Sum of all elements


Python

arr = [[1, 2], [3, 4]]

total = sum(sum(row) for row in arr)print("Sum:", total)

C++

int sum = 0, arr[2][2] = {{1, 2}, {3, 4}};for (int i = 0; i < 2; i++)

for (int j = 0; j < 2; j++)

sum += arr[i][j];

cout << "Sum: " << sum;

Java

int[][] arr = {{1, 2}, {3, 4}};int sum = 0;for (int[] row : arr)

for (int val : row)

sum += val;

[Link]("Sum: " + sum);

3. Transpose of matrix
Python

arr = [[1, 2], [3, 4]]for i in range(2):

for j in range(2):

print(arr[j][i], end=" ")

print()

C++

int arr[2][2] = {{1, 2}, {3, 4}};for (int i = 0; i < 2; i++) {

for (int j = 0; j < 2; j++)

cout << arr[j][i] << " ";

cout << endl;


}

Java

int[][] arr = {{1, 2}, {3, 4}};for (int i = 0; i < 2; i++) {

for (int j = 0; j < 2; j++)

[Link](arr[j][i] + " ");

[Link]();

4. Find maximum element


Python

arr = [[5, 7], [2, 9]]print("Max:", max(max(row) for row in arr))

C++

int arr[2][2] = {{5, 7}, {2, 9}}, max = arr[0][0];for (int i = 0; i < 2; i++)

for (int j = 0; j < 2; j++)

if (arr[i][j] > max) max = arr[i][j];

cout << "Max: " << max;

Java

int[][] arr = {{5, 7}, {2, 9}};int max = arr[0][0];for (int[] row : arr)

for (int val : row)

if (val > max) max = val;

[Link]("Max: " + max);

5. Count even & odd numbers


Python

arr = [[1, 2], [3, 4]]

even = odd = 0for row in arr:

for val in row:

if val % 2 == 0: even += 1

else: odd += 1print("Even:", even, "Odd:", odd)

C++
int even = 0, odd = 0, arr[2][2] = {{1, 2}, {3, 4}};for (int i = 0; i < 2; i++)

for (int j = 0; j < 2; j++)

arr[i][j] % 2 == 0 ? even++ : odd++;

cout << "Even: " << even << ", Odd: " << odd;

Java

int[][] arr = {{1, 2}, {3, 4}};int even = 0, odd = 0;for (int[] row : arr)

for (int val : row)

if (val % 2 == 0) even++;

else odd++;

[Link]("Even: " + even + ", Odd: " + odd);

You might also like