Search
 
SCRIPT & CODE EXAMPLE
 

CPP

c++ Difference Array | Range update query in O(1)

// C++ code to demonstrate Difference Array
#include <bits/stdc++.h>
using namespace std;
 
// Creates a diff array D[] for A[] and returns
// it after filling initial values.
vector<int> initializeDiffArray(vector<int>& A)
{
    int n = A.size();
 
    // We use one extra space because
    // update(l, r, x) updates D[r+1]
    vector<int> D(n + 1);
 
    D[0] = A[0], D[n] = 0;
    for (int i = 1; i < n; i++)
        D[i] = A[i] - A[i - 1];
    return D;
}
 
// Does range update
void update(vector<int>& D, int l, int r, int x)
{
    D[l] += x;
    D[r + 1] -= x;
}
 
// Prints updated Array
int printArray(vector<int>& A, vector<int>& D)
{
    for (int i = 0; i < A.size(); i++) {
        if (i == 0)
            A[i] = D[i];
 
        // Note that A[0] or D[0] decides
        // values of rest of the elements.
        else
            A[i] = D[i] + A[i - 1];
 
        cout << A[i] << " ";
    }
    cout << endl;
}
 
// Driver Code
int main()
{
    // Array to be updated
    vector<int> A{ 10, 5, 20, 40 };
 
    // Create and fill difference Array
    vector<int> D = initializeDiffArray(A);
 
    // After below update(l, r, x), the
    // elements should become 20, 15, 20, 40
    update(D, 0, 1, 10);
    printArray(A, D);
 
    // After below updates, the
    // array should become 30, 35, 70, 60
    update(D, 1, 3, 20);
    update(D, 2, 2, 30);
    printArray(A, D);
 
    return 0;
}
Comment

PREVIOUS NEXT
Code Example
Cpp :: function and function prototype. 
Cpp :: cout ascii art c++ 
Cpp :: find substring after character 
Cpp :: check .h files syntax c++ 
Cpp :: move semantics in c++ 
Cpp :: KL/wweiok#L['.[- 
Cpp :: c++ argument list for class template is missing 
Cpp :: c++ template function in class 
Cpp :: turn it codechef solution in c++ 
Cpp :: c++ check if cin got the wrong type 
Cpp :: nothrow new in cpp 
Cpp :: Increase IQ codechef solution in c++ 
Cpp :: powershell script query mssql windows authentication 
Cpp :: CPP Find options passed from command line 
Cpp :: preorder to postorder converter online 
Cpp :: dfs in tree using adjacency list 
Cpp :: Chef and the Wildcard Matching codechef solution in c++ 
Cpp :: gcc compile multi thread 
Cpp :: left margin c++ 
Cpp :: ue4 set size of widget c++ 
Cpp :: Snake Procession codechef solution in c++ 
Cpp :: initialise a vector c++ 
Cpp :: iff cpp 
Cpp :: C:UsersBBCDocumentsc n c++ project8PuzzleSolvemain.c|38|warning: suggest parentheses around assignment used as truth value [-Wparentheses]| 
Cpp :: test3 
Cpp :: is plaindrome 
Cpp :: for in c++ example 
Cpp :: find the second aperrence of a char in string c++ 
Cpp :: lambda - print-out array and add comment 
Cpp :: e.cpp 
ADD CONTENT
Topic
Content
Source link
Name
4+8 =