0% found this document useful (0 votes)
39 views12 pages

Day 4 Notes

Uploaded by

1234 ABCD
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)
39 views12 pages

Day 4 Notes

Uploaded by

1234 ABCD
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
You are on page 1/ 12

Day - 04

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 < nums.size(); i++)
nums[i] += nums[i - 1];
return nums;
}

Java

int[] runningSum(int[] nums) {


for (int i = 1; i < nums.length; 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;
res.insert(res.end(), nums.begin(), nums.end());
return res;
}

Java

int[] getConcatenation(int[] nums) {


int[] res = new int[nums.length * 2];
for(int i = 0; i < nums.length; i++) {
res[i] = nums[i];
res[i + nums.length] = nums[i];
}
return res;
}

✅ 3. [LeetCode 1672] Richest Customer Wealth

Find row with max sum

Python

def maximumWealth(accounts):
return max(map(sum, accounts))

C++

int maximumWealth(vector<vector<int>>& accounts) {


int maxWealth = 0;
for(auto& acc : accounts) {
int sum = 0;
for(int m : acc) sum += m;
maxWealth = max(maxWealth, sum);
}
return maxWealth;
}

Java

int maximumWealth(int[][] accounts) {


int max = 0;
for(int[] acc : accounts) {
int sum = 0;
for(int money : acc)
sum += money;
max = Math.max(max, sum);
}
return max;
}

✅ 4. [LeetCode 1431] Kids With the Greatest Number of Candies

Check if each child can have the most candies

Python
def kidsWithCandies(candies, extra):
max_c = max(candies)
return [c + extra >= max_c for c in candies]

C++

vector<bool> kidsWithCandies(vector<int>& candies, int extra) {


int max_c = *max_element(candies.begin(), candies.end());
vector<bool> res;
for(int c : candies)
res.push_back(c + extra >= max_c);
return res;
}

Java

List<Boolean> kidsWithCandies(int[] candies, int extra) {


int max = 0;
for (int c : candies)
max = Math.max(max, c);
List<Boolean> res = new ArrayList<>();
for (int c : candies)
res.add(c + extra >= max);
return res;
}

✅ 5. [LeetCode 1365] How Many Numbers Are Smaller Than the


Current Number

Count how many numbers are smaller than current

Python

def smallerNumbersThanCurrent(nums):
return [sum(n < x for n in nums) for x in nums]

C++

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


vector<int> res;
for(int i = 0; i < nums.size(); i++) {
int count = 0;
for(int j = 0; j < nums.size(); j++)
if(nums[j] < nums[i])
count++;
res.push_back(count);
}
return res;
}

Java

int[] smallerNumbersThanCurrent(int[] nums) {


int[] res = new int[nums.length];
for(int i = 0; i < nums.length; i++) {
int count = 0;
for(int j = 0; j < nums.length; j++)
if(nums[j] < nums[i])
count++;
res[i] = count;
}
return res;
}

LeetCode 560 – Subarray Sum Equals K solved in O(n²) complexity.

✅ C++ Solution (O(n²))

#include <bits/stdc++.h>
using namespace std;

class Solution {
public:
int subarraySum(vector<int>& nums, int k) {
int n = nums.size();
int count = 0;

// Try every subarray starting at i


for (int i = 0; i < n; i++) {
int sum = 0;
for (int j = i; j < n; j++) {
sum += nums[j]; // running sum
if (sum == k) count++;
}
}
return count;
}
};

int main() {
Solution s;
vector<int> nums = {1, 2, 3};
int k = 3;
cout << s.subarraySum(nums, k) << endl; // Output: 2
}

✅ Java Solution (O(n²))

class Solution {
public int subarraySum(int[] nums, int k) {
int n = nums.length;
int count = 0;

for (int i = 0; i < n; i++) {


int sum = 0;
for (int j = i; j < n; j++) {
sum += nums[j]; // running sum
if (sum == k) count++;
}
}
return count;
}

public static void main(String[] args) {


Solution s = new Solution();
int[] nums = {1, 2, 3};
int k = 3;
System.out.println(s.subarraySum(nums, k)); // Output: 2
}
}

✅ Python Solution (O(n²))

class Solution:
def subarraySum(self, nums, k):
n = len(nums)
count = 0

for i in range(n):
total = 0
for j in range(i, n):
total += nums[j] # running sum
if total == k:
count += 1
return count

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 < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++)
System.out.print(arr[i][j] + " ");
System.out.println();
}
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;
System.out.println("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++)
System.out.print(arr[j][i] + " ");
System.out.println();
}

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;
System.out.println("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++;
System.out.println("Even: " + even + ", Odd: " + odd);

✅ Final Summary

Feature 1D Array 2D Array (Matrix)

Structure Linear list Grid (rows × columns)

Access arr[i] arr[i][j]

Use Store values Tabular data

Sum Max,
Common Ops Row-wise/Column-wise ops
Reverse

1. C++ – vector
Definition

 vector is a dynamic array in C++ (from STL – Standard Template Library).


 Unlike normal arrays, its size can grow/shrink at runtime.

Types

 vector<int> → integers
 vector<string> → strings
 vector<vector<int>> → 2D vector

Common Operations
#include <bits/stdc++.h>
using namespace std;

int main() {
vector<int> v; // empty vector
v.push_back(10); // insert at end
v.push_back(20);
v[1] = 25; // update
cout << v[0] << endl; // access
cout << v.size() << endl; // size
v.pop_back(); // remove last element
for (int x : v) cout << x << " "; // iterate
}

2. Java – ArrayList
Definition

 Java does not have vector (except the old Vector class, which is synchronized and
rarely used now).
 The closest equivalent is ArrayList (part of java.util).
 It’s also a dynamic array that resizes automatically.

Types

 ArrayList<Integer>
 ArrayList<String>
 ArrayList<ArrayList<Integer>> (for 2D)

Common Operations
import java.util.*;

class Example {
public static void main(String[] args) {
ArrayList<Integer> arr = new ArrayList<>();

arr.add(10); // insert
arr.add(20);
arr.set(1, 25); // update
System.out.println(arr.get(0)); // access
System.out.println(arr.size()); // size
arr.remove(arr.size()-1); // remove last
for (int x : arr) System.out.print(x + " "); // iterate
}
}

3. Python – list
Definition

 In Python, the built-in list is like a dynamic array.


 It is the most used collection type, resizes automatically, and supports mixed data
types (unlike C++/Java).

Types
 list[int] → integers
 list[str] → strings
 list[list[int]] → 2D list

Common Operations
arr = [] # empty list
arr.append(10) # insert
arr.append(20)
arr[1] = 25 # update
print(arr[0]) # access
print(len(arr)) # size
arr.pop() # remove last
for x in arr: # iterate
print(x, end=" ")

� Summary (Quick Table)

Dynamic Array
Language Example Notes
Name
C++ vector<T> vector<int> v; Part of STL
ArrayList<Integer> arr = new
Java ArrayList<T>
ArrayList<>(); In java.util
Built-in, very
Python list arr = [1,2,3]
flexible

You might also like