Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

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

// C# code to demonstrate Difference Array
using System;
 
class GFG {
     
    // Creates a diff array D[] for A[] and returns
    // it after filling initial values.
    static void initializeDiffArray(int []A, int []D)
    {
         
        int n = A.Length;
 
        D[0] = A[0];
        D[n] = 0;
        for (int i = 1; i < n; i++)
            D[i] = A[i] - A[i - 1];
    }
 
    // Does range update
    static void update(int []D, int l, int r, int x)
    {
        D[l] += x;
        D[r + 1] -= x;
    }
 
    // Prints updated Array
    static int printArray(int []A, int []D)
    {
        for (int i = 0; i < A.Length; 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];
 
            Console.Write(A[i] + " ");
        }
         
        Console.WriteLine();
         
        return 0;
    }
     
    // Driver Code
    public static void Main()
    {
        // Array to be updated
        int []A = { 10, 5, 20, 40 };
        int n = A.Length;
        // Create and fill difference Array
        // We use one extra space because
        // update(l, r, x) updates D[r+1]
        int []D = new int[n + 1];
        initializeDiffArray(A, D);
 
        // 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);
    }
}
 
// This code is contributed by vt_m.
Comment

PREVIOUS NEXT
Code Example
Csharp :: collection to datatable c# 
Csharp :: {} is this used for code blocks in c# 
Csharp :: material Array setter 
Csharp :: c# nunit initialize variables 
Csharp :: c# user and password verification 
Csharp :: c# catch multiple exceptions at once 
Csharp :: c# single comment 
Csharp :: search list for words c# 
Csharp :: c# convert timestamp to datetime 
Csharp :: how to make physics in unity 
Csharp :: hardcode dropdown cshtml 
Csharp :: entity framework core search keyword query example 
Csharp :: c# code for simplex method 
Csharp :: remote webdriver dotnet 
Csharp :: convert physical path to virtual path in c# 
Csharp :: C# change to different form 
Csharp :: windowsform mail sender app 
Csharp :: c# list double min max 
Csharp :: php check syntax error folder 
Csharp :: c# wtssendmessage 
Csharp :: c# using rename class 
Csharp :: Unity search all chidren of parent object 
Csharp :: C# WriteLine() and Write() 
Csharp :: web socket background.js example 
Csharp :: enable asnotracking in asp.net core at global level 
Csharp :: nunjucks if variable exists 
Csharp :: asp.net framework mvc cors error axios 
Csharp :: get fixedupdate interval unity 
Csharp :: export xml 
Csharp :: c# printwindow chrome 
ADD CONTENT
Topic
Content
Source link
Name
5+9 =