topics content@scaler.com
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights New
    • Engagement control
    • Make a copy
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Make a copy Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       Owned this note    Owned this note      
    Published Linked with GitHub
    2
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    # DSA : Subarrays --- title: Introduction to arrays description: duration: 900 card_type: cue_card --- ### Introduction #### Definition A subarray is a contiguous part of an array. **Note:** * Single element is also a subarray. * Whole array is also a subarray. * Subarray is considered from left to right. Consider the following array of integers: | 4 | 1 | 2 | 3 | -1 | 6 | 9 | 8 | 12 | | - | - | - | - | - | - | - | - | - | * `2, 3, -1, 6` is a subarray of length 4. * `9` is a subarray of single element. * `4, 1, 2, 3, -1, 6, 9, 8, 12` is a subarray consisting of all the array elements. * `4, 12` is **not** a subarray because loop does not count as subarray. * `1, 2, 6` is **not** a subarray beacuse the array elements must be contiguous. * `3, 2, 1, 4` is **not** a subarray because order of the elements in a subarray should be same as in the array i.e. left to right. #### Length of subarray Given an array, the formula to find the length of subarray with start index as `start` and end index as `end` is given by - ```cpp Length of subarray = end - start + 1 ``` --- title: Quiz 1 description: duration: 45 card_type: quiz_card --- # Question How many subarrays of the following array start from index 1 [4, 2, 10, 3, 12, -2, 15] # Choices - [ ] 6 - [ ] 7 - [x] 21 - [ ] 36 --- title: Quiz Explanation description: duration: 600 card_type: cue_card --- **Explanation:** To count the number of subarrays that start from index 1, we need to consider all the possible contiguous part of array that start at index 1. Here are all the possible subarrays that start from index 1: [2] (starting from index 1) [2, 10] [2, 10, 3] [2, 10, 3, 12] [2, 10, 3, 12, -2] [2, 10, 3, 12, -2, 15] Therefore, there are a total of 6 subarrays that start from index 1. --- title: Quiz 2 description: duration: 45 card_type: quiz_card --- # Question How many subarrays of the following array start from index 3 [4, 2, 10, 3, 12, -2, 15] # Choices - [ ] 3 - [x] 4 - [ ] 5 - [ ] 6 --- title: Quiz Explanation description: duration: 600 card_type: cue_card --- **Explanation:** Here are all the possible subarrays that start from index 3: [3] (starting from index 3) [3, 12] [3, 12, -2] [3, 12, -2, 15] Therefore, there are a total of 4 subarrays that start from index 3. --- title: Quiz 3 description: duration: 45 card_type: quiz_card --- # Question Number of subarrays in the given array? [4, 2, 10, 3] # Choices - [ ] 7 - [ ] 8 - [ ] 9 - [x] 10 --- title: Quiz 4 description: duration: 45 card_type: quiz_card --- # Question Given ar[N] elements, how many subarrays are starting from index 0 # Choices - [ ] N-1 - [ ] N-2 - [x] N - [ ] 1 --- title: Introduction to arrays continued description: Formula to count no. of subarrays duration: 600 card_type: cue_card --- #### Formula to count no. of subarrays Let's say we have an array of size N. No. of subarrays starting from index 0 = N No. of subarrays starting from index 1 = N-1 No. of subarrays starting from index 2 = N-2 No. of subarrays starting from index 3 = N-3 ........... ........... ........... No. of subarrays starting from index N-1 = 1 So, Using the formula for the sum of an arithmetic series, total number of subarrays = N + (N-1) + (N-2) + (N-3) + . . . . . . + 3 + 2 + 1 = N(N+1)/2. ``` cpp Total_no_subarrs = n(n + 1) / 2 ``` --- title: Print the subarray description: Print the subarray of the array that starts from the start index and ends at the end index duration: 900 card_type: cue_card --- ### Coding Question 1 #### Problem Statement Given an array of integers and two indices, a start index and an end index, we need to print the subarray of the array that starts from the start index and ends at the end index (both inclusive). #### Solution To solve this problem, we can simply iterate over the elements of the array from the start index to the end index (both inclusive) and print each element. #### Pseudocode ```java public static void printSubarray(int[] arr, int start, int end) { for (int i = start; i <= end; i++) { System.out.print(arr[i] + " "); } } ``` #### Time and Space Complexity What will be T.C and S.C for the above approach? * TC - O(n) * SC - O(1) --- title: Print all possible subarrays description: Print all possible subarrays of the array duration: 900 card_type: cue_card --- ### Coding Question 2 #### Problem Statement: Given an array of integers, we need to print all possible subarrays of the array. #### Example ```cpp Input: [1, 2, 3] Output: [1] [1, 2] [1, 2, 3] [1, 3] [2] [2, 3] [3] ``` #### Solution We can generate all possible subarrays of the array as follows - * Begin a nested for loop with variable i initialized to 0 and running up to n-1. The variable i will be used to represent the starting index of a subarray. * Inside the first for loop, begin another nested for loop with variable j initialized to i and running up to n-1. The variable j will be used to represent the ending index of a subarray. * Inside the second for loop, begin a third for loop with variable k initialized to i and running up to j. This loop will be used to iterate through the subarray and print its elements. #### Pseudocode ```java public static void printSubarrays(int[] arr, int n) { // Generate all possible subarrays for (int i = 0; i < n; i++) { for (int j = i; j < n; j++) { // Print the current subarray for (int k = i; k <= j; k++) { System.out.print(arr[k] + " "); } System.out.println(); } } } ``` #### Time and Space Complexity What will be T.C and S.C for the above approach? * TC - O(n^3) * SC - O(1) --- title: Sum of all possible subarrays description: Find the sum of all possible subarrays of the array duration: 2700 card_type: cue_card --- ### Coding Question 3 #### Problem Statement Given an array of integers, we need to find the sum of all possible subarrays of the array. #### Example ```cpp Input: [1, 2, 3] Output: [1] = 1 [1, 2] = 3 [1, 2, 3] = 6 [2] = 2 [2, 3] = 5 [3] = 3 ``` #### Solution To solve this problem, we can use two nested loops to generate all possible subarrays of the array. At each iteration, we calculate the sum of the subarray using another loop, and print its value when corresponding subarray sum has been calculated. #### Pseudocode ```java public static void printSubarraySum(int[] arr, int n) { // Generate all possible subarrays and calculate their sum for (int i = 0; i < n; i++) { for (int j = i; j < n; j++) { int subarray_sum = 0; for (int k = i; k <= j; k++) { subarray_sum += arr[k]; } System.out.println(subarray_sum); } } } ``` #### Time and Space Complexity What will be T.C and S.C for the above approach? * TC - O(n^3) * SC - O(1) #### Optimized Solution We can optimize the solution using the prefix sum technique. * First calculate the prefix sum array of the input array. * Then use two nested loops to iterate over all possible subarrays of the array. At each iteration, calculate the sum of the subarray using the prefix sum array. * If the starting index of the subarray is 0, directly use the prefix sum of the ending index as the sum of the subarray. * Otherwise, subtract the prefix sum of the starting index-1 from the prefix sum of the ending index to get the sum of the subarray. #### Pseudocode ```cpp public static void printSubarraySum(int[] arr, int n) { // Calculate the prefix sum of the array int[] prefix = new int[n]; prefix[0] = arr[0]; for (int i = 1; i < n; i++) { prefix[i] = prefix[i-1] + arr[i]; } // Print the sum of all possible subarrays using the prefix sum for (int i = 0; i < n; i++) { for (int j = i; j < n; j++) { if (i == 0) { System.out.println(prefix[j]); } else { System.out.println(prefix[j] - prefix[i-1]); } } } } ``` #### Time and Space Complexity What will be T.C and S.C for the above approach? * TC - O(n^2) * SC - O(n) #### More optimized Solution Before going to the main carry forward approach, lets try to solve below question - **Question -** Given an array arr[N], print the sum of all subarrays starting at 3rd index. **Example -** arr[10] = [3, 8, 4, 7, 9, 4, 3, 2, 10, 6] **Solution -** ![](https://d2beiqkhq929f0.cloudfront.net/public_assets/assets/000/033/291/original/Screenshot_2023-05-01_at_3.55.12_PM.png) With the help of above example, we can now build up the logic to find the sum starting from all start indexes. **Main logic -** The idea to find the sum of all possible subarrays of the array is to initialise the sum with 0 for every start index. Now, carry forward the sum from start index to end index. #### Pseudocode ```cpp public static void printSubarraySum(int[] arr, int n) { for (int i = 0; i < n; i++) { int subarray_sum = 0; for (int j = i; j < n; j++) { subarray_sum += arr[j]; System.out.println(subarray_sum); } sum += subarray_sum; } } ``` #### Time and Space Complexity What will be T.C and S.C for the above approach? * TC - O(n^2) * SC - O(1) --- title: Total sum of all possible subarrays description: Find the total sum of all possible subarrays of the array duration: 1200 card_type: cue_card --- ### Coding Question 4 #### Problem Statement Given an array of integers, find the total sum of all possible subarrays. #### Solution For each such pair of indices, calculate the sum of the corresponding subarray and add it to the total sum of all subarrays. This can be done by maintaining a variable say `subarray_sum` that stores the sum of the current subarray using carry forward technique and adding it to the total sum say `total_sum` for each iteration of the inner loop. #### Pseudocode ```cpp public static void printSubarraySum(int[] arr, int n) { int total_sum = 0; for (int i = 0; i < n; i++) { int subarray_sum = 0; for (int j = i; j < n; j++) { subarray_sum += arr[j]; total_sum += subarray_sum; } } System.out.println(total_sum); } ``` #### Time and Space Complexity What will be T.C and S.C for the above approach? * TC - O(n^2) * SC - O(1) #### Optimized Solution (Contribution Technique) We can optimize the above solution further by observing a pattern in the subarray sums. Let's take the example array ``[1, 2, 3]`` again. The subarrays and their sums are: ``` [1] -> 1 [1, 2] -> 3 [1, 2, 3] -> 6 [2] -> 2 [2, 3] -> 5 [3] -> 3 ``` * the first element 1 appears in 3 subarrays: [1], [1, 2], and [1, 2, 3]. * the second element 2 appears in 4 subarrays: [2], [1, 2], [2, 3], and [1, 2, 3]. * the third element 3 appears in 3 subarrays: [3], [2, 3], and [1, 2, 3]. --- title: Quiz 5 description: duration: 60 card_type: quiz_card --- # Question In how many subarrays, the element at index 1 will be present? A: [3, -2, 4, -1, 2, 6 ] # Choices - [ ] 6 - [ ] 3 - [x] 10 - [ ] 8 --- title: Quiz 6 duration: 90 card_type: quiz_card --- # Question In an array of size N, how many subarrays will contain the element at index i? # Choices - [ ] i*(N-1) - [ ] (i+1)*(N-i+1) - [x] (i+1)*(N-i) - [ ] (i-1)*(N-i) <!-- Notice that each element in the array appears in exactly `(i+1) * (n-i)` subarrays, where i is the index of the element starting (0-based indexing) and n is the length of the array. --> --- title: Quiz explanation description: duration: 600 card_type: cue_card --- **Explanation:** If we take an array 'arr', There are i+1 elements from arr[0] to arr[i]. There are n-i elements from arr[i] to arr[n-1]. Thus, the total number of subarrays containing arr[i] is i+1 multiplied by n-i. ![](https://d2beiqkhq929f0.cloudfront.net/public_assets/assets/000/033/292/original/Screenshot_2023-05-01_at_4.29.18_PM.png) This gives us the expression `(i+1) * (n-i)` where i is the index of the element starting (0-based indexing) and n is the length of the array. --- title: Total sum of all possible subarrays continued description: duration: 1200 card_type: cue_card --- We can use above pattern to compute the total sum of all subarrays in O(n) time complexity. The steps are as follows: * Initialize a variable total_sum to zero. * Iterate over all elements of the input array arr. For each element arr[i], compute `arr[i] * (i+1) * (n-i)` and add it to total_sum. * Return total_sum as the output of the function. #### Pseudocode ```cpp public static int findTotalSubarraySum(ArrayList<Integer> arr) { int n = arr.size(); int total_sum = 0; // Iterate over all elements of the array and compute the sum of all subarrays containing that element for (int i = 0; i < n; i++) { total_sum += arr[i] * (i+1) * (n-i); } return total_sum; } ``` #### Time and Space Complexity What will be T.C and S.C for the above approach? * TC - O(n) * SC - O(1)

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully