Penggunaan fungsi GETSTART pada PHP

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    Given two integers N and K, the task is to generate a series of N terms in which every term is sum of the previous K terms.
    Note: The first term of the series is 1. if there are not enough previous terms, then other terms are supposed to be 0.
    Examples: 

    Input: N = 8, K = 3 
    Output: 1 1 2 4 7 13 24 44 
    Explanation: 
    Series is generated as follows: 
    a[0] = 1 
    a[1] = 1 + 0 + 0 = 1 
    a[2] = 1 + 1 + 0 = 2 
    a[3] = 2 + 1 + 1 = 4 
    a[4] = 4 + 2 + 1 = 7 
    a[5] = 7 + 4 + 2 = 13 
    a[6] = 13 + 7 + 4 = 24 
    a[7] = 24 + 13 + 7 = 44
    Input: N = 10, K = 4 
    Output: 1 1 2 4 8 15 29 56 108 208 

    Naive Approach: The idea is to run two loops to generate N terms of series. Below is the illustration of the steps:  

    • Traverse the first loop from 0 to N – 1, to generate every term of the series.
    • Run a loop from max[0, i – K] to i to compute the sum of the previous K terms.
    • Update the sum to the current index of series as the current term.

    Below is the implementation of the above approach: 

    C++

    #include

    using namespace std;

    void sumOfPrevK[int N, int K]

    {

        int arr[N];

        arr[0] = 1;

        for [int i = 1; i < N; i++] {

            int j = i - 1, count = 0,

                sum = 0;

            while [j >= 0 && count < K] {

                sum += arr[j];

                j--;

                count++;

            }

            arr[i] = sum;

        }

        for [int i = 0; i < N; i++] {

            cout = 0 and count < K]:

                sum = sum + arr[j]

                j = j - 1

                count = count + 1

            arr[i] = sum

        for i in range[0, N]:

            print[arr[i]]

    N = 10

    K = 4

    sumOfPrevK[N, K]

    C#

    using System;

    class Sum {

        void sumOfPrevK[int N, int K]

        {

            int []arr = new int[N];

            arr[0] = 1;

            for [int i = 1; i < N; i++] {

                int j = i - 1, count = 0,

                    sum = 0;

                while [j >= 0 && count < K] {

                    sum += arr[j];

                    j--;

                    count++;

                }

                arr[i] = sum;

            }

            for [int i = 0; i < N; i++] {

                Console.Write[arr[i] + " "];

            }

        }

        public static void Main[String []args]

        {

            Sum s = new Sum[];

            int N = 10, K = 4;

            s.sumOfPrevK[N, K];

        }

    }

    Javascript

    function sumOfPrevK[N, K]

    {

        let arr = new Array[N];

        arr[0] = 1;

        for [let i = 1; i < N; i++] {

            let j = i - 1, count = 0, sum = 0;

            while [j >= 0 && count < K] {

                sum += arr[j];

                j--;

                count++;

            }

            arr[i] = sum;

        }

        for [let i = 0; i < N; i++] {

            document.write[arr[i] + " "];

        }

    }

    let N = 10, K = 4;

    sumOfPrevK[N, K];

    Output:

    1 1 2 4 8 15 29 56 108 208 

    Performance analysis: 

    • Time Complexity: O[N * K]
    • Space Complexity: O[N]

    Improved Approach: The idea is to store the current sum in a variable and subtract the last Kth term in every step and add the last term into the pre-sum to compute every term of the series.
    Below is the implementation of the above approach:  

    C++

    #include

    #include

    using namespace std;

    void sumOfPrevK[int N, int K]

    {

        vector arr[N];

        arr[0] = 1;

        int start = 0, end = 1, sum = 1;

        while [end < N] {

            if [end - start > K] {

                sum -= arr[start];

                ++start;

            }

            arr[end] = sum;

            sum += arr[end];

            ++end;

        }

        for [int i = 0; i < N; ++i] {

            cout K] {

            sum -= arr[start];

            ++start;

          }

          arr[end] = sum;

          sum += arr[end];

          ++end;

        }

        for [int i = 0; i < N; ++i] {

          Console.Write[arr[i] + " "];

        }

        Console.Write["\n"];

      }

      public static void Main[string[] args]

      {

        int N = 10, K = 4;

        sumOfPrevK[N, K];

      }

    }

    Javascript

    function sumOfPrevK[N, K]

    {

        let arr = new Array[N];

        arr[0] = 1;

        let start = 0, end = 1, sum = 1;

        while [end < N] {

            if [end - start > K] {

                sum -= arr[start];

                ++start;

            }

            arr[end] = sum;

            sum += arr[end];

            ++end;

        }

        console.log[arr.join[" "]]

    }

    let N = 10, K = 4;

    sumOfPrevK[N, K];

    Complexity analysis: 

    • Time Complexity: O[N]
    • Space Complexity: O[N]

    Another Approach: Sliding Window

    The idea is to use a fixed-size sliding window of size K to get the sum of previous K elements. 

    • Initialize the answer array with arr[0] = 1 and a variable sum = 1, representing the sum of the last K elements. Initially, the previous sum = 1. [If there are less than K elements in the last window, then the sum will store the sum of all the elements in the window]
    • The sliding window is initialized by making start = 0 and end = 1.
    • If the window size exceeds K, then decrement arr[start] from sum and increment start toupdate the window size and sum. 
    • Make arr[end] = sum, which will be the sum of the previous K elements, and increment sum by arr[end] for the next iteration. Increment end by 1. 

    Output

    1 1 2 4 8 15 29 56 108 208 

    Complexity analysis:

    • Time Complexity: O[N]
    • Auxiliary Space: O[N]

    JavaScript does not have any print object or print methods. You cannot access output devices from JavaScript. The only exception is that you can call the window.print[] method in the browser to print the content of the current window.

    const num1 = parseInt[prompt['Enter the first number ']]; const num2 = parseInt[prompt['Enter the second number ']]; Then, the sum of the numbers is computed. const sum = num1 + num2; Finally, the sum is displayed.

    Javascript Number[] Function object: This parameter holds the objects that will be converted any type of javascript variable to number type. Return Values: Number[] function returns the number format for any type of javascript variable. Example 2: Not a number is returned by the compiler.

    Bài mới nhất

    Chủ Đề