Arrays - Class 2

Dear Sciaku Learner you are not logged in or not enrolled in this course.

Please Click on login or enroll now button.

If you have any query feel free to chat us!

Happy Coding! Happy Learning!

Lecture 44 :- Arrays - Class 2

In continuation of Arrays - Class 2, let's explore more advanced topics related to arrays.

  1. Dynamic Arrays: Dynamic arrays, also known as dynamic or resizable arrays, allow you to create arrays whose size can change during runtime. Unlike static arrays, where the size is fixed at compile time, dynamic arrays can grow or shrink as needed.

In C++, you can use the std::vector container to create dynamic arrays. The std::vector automatically manages the memory and provides various methods to add or remove elements.

Example of using std::vector:

cppCopy code

#include <iostream> #include <vector> int main() {    std::vector<int> dynamicArray;    // Adding elements    dynamicArray.push_back(10);    dynamicArray.push_back(20);    dynamicArray.push_back(30);    // Accessing elements    std::cout << dynamicArray[1] << std::endl; // Output: 20    // Size of the dynamic array    std::cout << "Size of the dynamic array: " << dynamicArray.size() << std::endl; // Output: 3    // Removing an element    dynamicArray.pop_back();    // Updated size    std::cout << "Updated size of the dynamic array: " << dynamicArray.size() << std::endl; // Output: 2    return 0; }

  1. Two-Pointer Technique: The two-pointer technique is a common approach used to solve certain array-related problems efficiently. It involves using two pointers to traverse the array simultaneously, often from both ends or from different starting positions. This technique is useful in solving problems like finding a pair with a specific sum, checking for duplicates, or identifying patterns.

Example of two-pointer technique - Finding a pair with a given sum:

cppCopy code

#include <iostream> #include <vector> #include <algorithm> std::vector<int> findPairWithSum(std::vector<int>& nums, int targetSum) {    std::sort(nums.begin(), nums.end());    int left = 0;    int right = nums.size() - 1;    while (left < right) {        int currentSum = nums[left] + nums[right];        if (currentSum == targetSum) {            return {nums[left], nums[right]};        } else if (currentSum < targetSum) {            left++;        } else {            right--;        }    }    return {}; // Pair not found } int main() {    std::vector<int> nums = {2, 7, 11, 15};    int targetSum = 9;    std::vector<int> result = findPairWithSum(nums, targetSum);    if (!result.empty()) {        std::cout << "Pair with sum " << targetSum << " found: " << result[0] << " and " << result[1] << std::endl;    } else {        std::cout << "Pair with sum " << targetSum << " not found." << std::endl;    }    return 0; }

  1. Array Rotation: Array rotation is a common array operation where the elements of an array are shifted to the left or right by a certain number of positions. Array rotation can be useful in various scenarios, such as solving problems involving circular arrays or implementing certain algorithms efficiently.

Example of left rotation of an array:

cppCopy code

#include <iostream> #include <vector> std::vector<int> leftRotateArray(std::vector<int>& nums, int rotations) {    int n = nums.size();    std::vector<int> rotatedArray(n);    for (int i = 0; i < n; i++) {        int newIndex = (i + n - rotations) % n;        rotatedArray[newIndex] = nums[i];    }    return rotatedArray; } int main() {    std::vector<int> nums = {1, 2, 3, 4, 5};    int rotations = 2;    std::vector<int> rotatedArray = leftRotateArray(nums, rotations);    std::cout << "Original Array: ";    for (int num : nums) {        std::cout << num << " ";    }    std::cout << "\nRotated Array: ";    for (int num : rotatedArray) {        std::cout << num << " ";    }    std::cout << std::endl;    return 0; }

Arrays are a fundamental data structure, and understanding advanced array topics like dynamic arrays, two-pointer technique, and array rotation can greatly enhance your problem-solving skills in algorithms and programming.

6. Arrays, Time Complexity & Space Complexity

Comments: 2

profile
@mk.info.work
17-Feb-2024, 10:20 PM

SCIAKU Team please upload 1st video of TREE please please please, please

profile
@na3744
23-Feb-2024, 02:52 AM

I bought this course, it worth it!

profile
@mk.info.work
15-Nov-2023, 10:25 PM

Hi i want to buy this course but you dont have master card payment method please let me know how i can buy it

profile
@sciaku1
11-Jan-2024, 03:23 PM

Dear mk.info.work, Now we have all types of payment options. If you need to purchase just checkout our official website

Frequently Asked Questions (FAQs)

How do I register on Sciaku.com?
How can I enroll in a course on Sciaku.com?
Are there free courses available on Sciaku.com?
How do I purchase a paid course on Sciaku.com?
What payment methods are accepted on Sciaku.com?
How will I access the course content after purchasing a course?
How long do I have access to a purchased course on Sciaku.com?
How do I contact the admin for assistance or support?
Can I get a refund for a course I've purchased?
How does the admin grant access to a course after payment?