📌  相关文章
📜  范围和查询后从给定数组中查找初始数组

📅  最后修改于: 2021-06-26 19:30:45             🧑  作者: Mango

给定一个数组arr [] ,这是在原始数组上执行许多查询时的结果数组。查询的格式为[l,r,x] ,其中l是数组中的开始索引, r是数组中的结束索引, x是必须添加到索引范围内所有元素的整数元素[l,r]任务是找到原始数组。

例子:

天真的方法:对于从lr的每个范围,请减去相应的x以获得初始数组。

下面是该方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
  
// Utility function to print the contents of an array
void printArr(int arr[], int n)
{
    for (int i = 0; i < n; i++) {
        cout << arr[i] << " ";
    }
}
  
// Function to find the original array
void findOrgArr(int arr[], int l[], int r[], int x[],
                int n, int q)
{
    for (int j = 0; j < q; j++) {
        for (int i = l[j]; i <= r[j]; i++) {
  
            // Decrement elements between
            // l[j] and r[j] by x[j]
            arr[i] = arr[i] - x[j];
        }
    }
  
    printArr(arr, n);
}
  
// Driver code
int main()
{
    // Final array
    int arr[] = { 20, 30, 20, 70, 100 };
  
    // Size of the array
    int n = sizeof(arr) / sizeof(arr[0]);
  
    // Queries
    int l[] = { 0, 1, 3 };
    int r[] = { 2, 4, 4 };
    int x[] = { 10, 20, 30 };
  
    // Number of queries
    int q = sizeof(l) / sizeof(l[0]);
  
    findOrgArr(arr, l, r, x, n, q);
  
    return 0;
}


Java
// Java implementation of the approach
import java.util.*;
  
class GFG
{
  
// Utility function to print the contents of an array
static void printArr(int arr[], int n)
{
    for (int i = 0; i < n; i++) {
        System.out.print(arr[i]+" ");
    }
}
  
// Function to find the original array
static void findOrgArr(int arr[], int l[], int r[], int x[],
                int n, int q)
{
    for (int j = 0; j < q; j++) {
        for (int i = l[j]; i <= r[j]; i++) {
  
            // Decrement elements between
            // l[j] and r[j] by x[j]
            arr[i] = arr[i] - x[j];
        }
    }
  
    printArr(arr, n);
}
  
// Driver code
public static void  main(String args[])
{
    // Final array
    int arr[] = { 20, 30, 20, 70, 100 };
  
    // Size of the array
    int n =  arr.length;
  
    // Queries
    int l[] = { 0, 1, 3 };
    int r[] = { 2, 4, 4 };
    int x[] = { 10, 20, 30 };
  
    // Number of queries
    int q = l.length;
  
    findOrgArr(arr, l, r, x, n, q);
  
}
}
  
// This code is contributed by
// Shashank_Sharma


Python3
# Python3 implementation of the approach
import math as mt
  
# Utility function to print the 
# contents of an array
def printArr(arr, n):
  
    for i in range(n): 
        print(arr[i], end = " ")
  
# Function to find the original array
def findOrgArr(arr, l, r, x, n, q):
  
    for j in range(q):
        for i in range(l[j], r[j] + 1):
              
            # Decrement elements between
            # l[j] and r[j] by x[j]
            arr[i] = arr[i] - x[j]
          
    printArr(arr, n)
  
# Driver code
  
# Final array
arr = [20, 30, 20, 70, 100] 
  
# Size of the array
n = len(arr)
  
# Queries
l = [0, 1, 3] 
r = [ 2, 4, 4] 
x = [ 10, 20, 30 ]
  
# Number of queries
q = len(l)
  
findOrgArr(arr, l, r, x, n, q)
  
# This code is contributed by 
# mohit kumar 29


C#
// C# implementation of the approach
using System;
  
class GFG
{
  
// Utility function to print the 
// contents of an array
static void printArr(int[] arr, int n)
{
    for (int i = 0; i < n; i++)
    {
        Console.Write(arr[i] + " ");
    }
}
  
// Function to find the original array
static void findOrgArr(int[] arr, int[] l, 
                       int[] r, int[] x, 
                       int n, int q)
{
    for (int j = 0; j < q; j++) 
    {
        for (int i = l[j]; i <= r[j]; i++)
        {
  
            // Decrement elements between
            // l[j] and r[j] by x[j]
            arr[i] = arr[i] - x[j];
        }
    }
  
    printArr(arr, n);
}
  
// Driver code
public static void Main()
{
    // Final array
    int[] arr = { 20, 30, 20, 70, 100 };
  
    // Size of the array
    int n = arr.Length;
  
    // Queries
    int[] l = { 0, 1, 3 };
    int[] r = { 2, 4, 4 };
    int[] x = { 10, 20, 30 };
  
    // Number of queries
    int q = l.Length;
  
    findOrgArr(arr, l, r, x, n, q);
  
}
}
  
// This code is contributed by
// Akanksha Rai


PHP


C++
// C++ implementation of the approach
#include 
using namespace std;
  
// Utility function to print the contents of an array
void printArr(int arr[], int n)
{
    for (int i = 0; i < n; i++) {
        cout << arr[i] << " ";
    }
}
  
// Function to find the original array
void findOrgArr(int arr[], int l[], int r[], int x[],
                int n, int q)
{
    int b[n] = { 0 };
  
    for (int i = 0; i < q; i++) {
  
        // Decrement the element at l[i]th index by -x
        b[l[i]] += -x[i];
  
        // Increment the element at (r[i] + 1)th index
        // by x if (r[i] + 1) is a valid index
        if (r[i] + 1 < n)
            b[r[i] + 1] += x[i];
    }
  
    for (int i = 1; i < n; i++)
        // Prefix sum of array b
        b[i] = b[i - 1] + b[i];
  
    // Update the original array
    for (int i = 0; i < n; i++)
        arr[i] = arr[i] + b[i];
  
    printArr(arr, n);
}
  
// Driver code
int main()
{
    // Final array
    int arr[] = { 20, 30, 20, 70, 100 };
  
    // Size of the array
    int n = sizeof(arr) / sizeof(arr[0]);
  
    // Queries
    int l[] = { 0, 1, 3 };
    int r[] = { 2, 4, 4 };
    int x[] = { 10, 20, 30 };
  
    // Number of queries
    int q = sizeof(l) / sizeof(l[0]);
  
    findOrgArr(arr, l, r, x, n, q);
  
    return 0;
}


Java
// Java implementation of above approach 
class GFG{
  
    // Utility function to print the contents of an array 
    static void printArr(int arr[], int n) 
    { 
        for (int i = 0; i < n; i++) 
        { 
        System.out.print(arr[i] + " ") ; 
        } 
    } 
      
    // Function to find the original array 
    static void findOrgArr(int arr[], int l[], int r[], int x[], 
                    int n, int q) 
    { 
        int b[] = new int[n] ;
          
        for (int i = 0; i < q; i++)
            b[i] = 0 ;
      
        for (int i = 0; i < q; i++)
        { 
      
            // Decrement the element at l[i]th index by -x 
            b[l[i]] += -x[i]; 
      
            // Increment the element at (r[i] + 1)th index 
            // by x if (r[i] + 1) is a valid index 
            if (r[i] + 1 < n) 
                b[r[i] + 1] += x[i]; 
        } 
      
        for (int i = 1; i < n; i++) 
            // Prefix sum of array b 
            b[i] = b[i - 1] + b[i]; 
      
        // Update the original array 
        for (int i = 0; i < n; i++) 
            arr[i] = arr[i] + b[i]; 
      
        printArr(arr, n); 
    } 
      
    // Driver code 
    public static void main(String []args)
    { 
        // Final array 
        int arr[] = { 20, 30, 20, 70, 100 }; 
      
        // Size of the array 
        int n = arr.length ;
      
        // Queries 
        int l[] = { 0, 1, 3 }; 
        int r[] = { 2, 4, 4 }; 
        int x[] = { 10, 20, 30 }; 
      
        // Number of queries 
        int q = l.length ;
      
        findOrgArr(arr, l, r, x, n, q); 
        } 
}
  
// This code is contributed by aishwarya.27


Python3
# Python3 implementation of the approach
  
# Utility function to print the contents 
# of an array
def printArr(arr, n):
  
    for i in range(n):
        print(arr[i], end = " ")
  
  
# Function to find the original array
def findOrgArr(arr, l, r, x, n, q):
  
    b = [0 for i in range(n)]
  
    for i in range(q):
  
        # Decrement the element at l[i]th 
        # index by -x
        b[l[i]] += -x[i]
  
        # Increment the element at (r[i] + 1)th 
        # index by x if (r[i] + 1) is a valid index
        if (r[i] + 1 < n):
            b[r[i] + 1] += x[i]
      
    for i in range(n):
          
        # Prefix sum of array b
        b[i] = b[i - 1] + b[i]
  
    # Update the original array
    for i in range(n):
        arr[i] = arr[i] + b[i]
  
    printArr(arr, n)
  
# Driver code
arr = [20, 30, 20, 70, 100]
  
# Size of the array
n = len(arr)
  
# Queries
l = [0, 1, 3 ]
r = [2, 4, 4 ]
x = [10, 20, 30 ]
  
# Number of queries
q = len(l)
  
findOrgArr(arr, l, r, x, n, q)
  
# This code Is contributed by
# Mohit kumar 29


C#
// C# implementation of above approach 
using System;
  
class GFG
{
  
// Utility function to print the 
// contents of an array 
static void printArr(int[] arr, int n) 
{ 
    for (int i = 0; i < n; i++) 
    { 
        Console.Write(arr[i] + " "); 
    } 
} 
  
// Function to find the original array 
static void findOrgArr(int[] arr, int[] l, 
                       int[] r, int[] x, 
                       int n, int q) 
{ 
    int[] b = new int[n];
      
    for (int i = 0; i < q; i++)
        b[i] = 0 ;
  
    for (int i = 0; i < q; i++)
    { 
  
        // Decrement the element at l[i]th 
        // index by -x 
        b[l[i]] += -x[i]; 
  
        // Increment the element at (r[i] + 1)th 
        // index by x if (r[i] + 1) is a valid index 
        if (r[i] + 1 < n) 
            b[r[i] + 1] += x[i]; 
    } 
  
    for (int i = 1; i < n; i++) 
      
        // Prefix sum of array b 
        b[i] = b[i - 1] + b[i]; 
  
    // Update the original array 
    for (int i = 0; i < n; i++) 
        arr[i] = arr[i] + b[i]; 
  
    printArr(arr, n); 
} 
  
// Driver code 
public static void Main()
{ 
    // Final array 
    int[] arr = { 20, 30, 20, 70, 100 }; 
  
    // Size of the array 
    int n = arr.Length;
  
    // Queries 
    int[] l = { 0, 1, 3 }; 
    int[] r = { 2, 4, 4 }; 
    int[] x = { 10, 20, 30 }; 
  
    // Number of queries 
    int q = l.Length;
  
    findOrgArr(arr, l, r, x, n, q); 
} 
}
  
// This code is contributed 
// by Akanksha Rai


输出:

10 0 -10 20 50 

时间复杂度: O(n 2 )

高效方法:请按照以下步骤操作以到达初始阵列:

  • 取给定数组大小的数组b [] ,并用0初始化其所有元素。
  • 在数组b []中,对于每个查询更新b [l] = b [l] – x ,如果r + 1 ,则b [r + 1] = b [r + 1] + x 。这是因为x在执行前缀求和时将抵消-x的影响。
  • 取数组b []的前缀和,并将其添加到给定的数组中,这将产生初始数组。

C++

// C++ implementation of the approach
#include 
using namespace std;
  
// Utility function to print the contents of an array
void printArr(int arr[], int n)
{
    for (int i = 0; i < n; i++) {
        cout << arr[i] << " ";
    }
}
  
// Function to find the original array
void findOrgArr(int arr[], int l[], int r[], int x[],
                int n, int q)
{
    int b[n] = { 0 };
  
    for (int i = 0; i < q; i++) {
  
        // Decrement the element at l[i]th index by -x
        b[l[i]] += -x[i];
  
        // Increment the element at (r[i] + 1)th index
        // by x if (r[i] + 1) is a valid index
        if (r[i] + 1 < n)
            b[r[i] + 1] += x[i];
    }
  
    for (int i = 1; i < n; i++)
        // Prefix sum of array b
        b[i] = b[i - 1] + b[i];
  
    // Update the original array
    for (int i = 0; i < n; i++)
        arr[i] = arr[i] + b[i];
  
    printArr(arr, n);
}
  
// Driver code
int main()
{
    // Final array
    int arr[] = { 20, 30, 20, 70, 100 };
  
    // Size of the array
    int n = sizeof(arr) / sizeof(arr[0]);
  
    // Queries
    int l[] = { 0, 1, 3 };
    int r[] = { 2, 4, 4 };
    int x[] = { 10, 20, 30 };
  
    // Number of queries
    int q = sizeof(l) / sizeof(l[0]);
  
    findOrgArr(arr, l, r, x, n, q);
  
    return 0;
}

Java

// Java implementation of above approach 
class GFG{
  
    // Utility function to print the contents of an array 
    static void printArr(int arr[], int n) 
    { 
        for (int i = 0; i < n; i++) 
        { 
        System.out.print(arr[i] + " ") ; 
        } 
    } 
      
    // Function to find the original array 
    static void findOrgArr(int arr[], int l[], int r[], int x[], 
                    int n, int q) 
    { 
        int b[] = new int[n] ;
          
        for (int i = 0; i < q; i++)
            b[i] = 0 ;
      
        for (int i = 0; i < q; i++)
        { 
      
            // Decrement the element at l[i]th index by -x 
            b[l[i]] += -x[i]; 
      
            // Increment the element at (r[i] + 1)th index 
            // by x if (r[i] + 1) is a valid index 
            if (r[i] + 1 < n) 
                b[r[i] + 1] += x[i]; 
        } 
      
        for (int i = 1; i < n; i++) 
            // Prefix sum of array b 
            b[i] = b[i - 1] + b[i]; 
      
        // Update the original array 
        for (int i = 0; i < n; i++) 
            arr[i] = arr[i] + b[i]; 
      
        printArr(arr, n); 
    } 
      
    // Driver code 
    public static void main(String []args)
    { 
        // Final array 
        int arr[] = { 20, 30, 20, 70, 100 }; 
      
        // Size of the array 
        int n = arr.length ;
      
        // Queries 
        int l[] = { 0, 1, 3 }; 
        int r[] = { 2, 4, 4 }; 
        int x[] = { 10, 20, 30 }; 
      
        // Number of queries 
        int q = l.length ;
      
        findOrgArr(arr, l, r, x, n, q); 
        } 
}
  
// This code is contributed by aishwarya.27

Python3

# Python3 implementation of the approach
  
# Utility function to print the contents 
# of an array
def printArr(arr, n):
  
    for i in range(n):
        print(arr[i], end = " ")
  
  
# Function to find the original array
def findOrgArr(arr, l, r, x, n, q):
  
    b = [0 for i in range(n)]
  
    for i in range(q):
  
        # Decrement the element at l[i]th 
        # index by -x
        b[l[i]] += -x[i]
  
        # Increment the element at (r[i] + 1)th 
        # index by x if (r[i] + 1) is a valid index
        if (r[i] + 1 < n):
            b[r[i] + 1] += x[i]
      
    for i in range(n):
          
        # Prefix sum of array b
        b[i] = b[i - 1] + b[i]
  
    # Update the original array
    for i in range(n):
        arr[i] = arr[i] + b[i]
  
    printArr(arr, n)
  
# Driver code
arr = [20, 30, 20, 70, 100]
  
# Size of the array
n = len(arr)
  
# Queries
l = [0, 1, 3 ]
r = [2, 4, 4 ]
x = [10, 20, 30 ]
  
# Number of queries
q = len(l)
  
findOrgArr(arr, l, r, x, n, q)
  
# This code Is contributed by
# Mohit kumar 29

C#

// C# implementation of above approach 
using System;
  
class GFG
{
  
// Utility function to print the 
// contents of an array 
static void printArr(int[] arr, int n) 
{ 
    for (int i = 0; i < n; i++) 
    { 
        Console.Write(arr[i] + " "); 
    } 
} 
  
// Function to find the original array 
static void findOrgArr(int[] arr, int[] l, 
                       int[] r, int[] x, 
                       int n, int q) 
{ 
    int[] b = new int[n];
      
    for (int i = 0; i < q; i++)
        b[i] = 0 ;
  
    for (int i = 0; i < q; i++)
    { 
  
        // Decrement the element at l[i]th 
        // index by -x 
        b[l[i]] += -x[i]; 
  
        // Increment the element at (r[i] + 1)th 
        // index by x if (r[i] + 1) is a valid index 
        if (r[i] + 1 < n) 
            b[r[i] + 1] += x[i]; 
    } 
  
    for (int i = 1; i < n; i++) 
      
        // Prefix sum of array b 
        b[i] = b[i - 1] + b[i]; 
  
    // Update the original array 
    for (int i = 0; i < n; i++) 
        arr[i] = arr[i] + b[i]; 
  
    printArr(arr, n); 
} 
  
// Driver code 
public static void Main()
{ 
    // Final array 
    int[] arr = { 20, 30, 20, 70, 100 }; 
  
    // Size of the array 
    int n = arr.Length;
  
    // Queries 
    int[] l = { 0, 1, 3 }; 
    int[] r = { 2, 4, 4 }; 
    int[] x = { 10, 20, 30 }; 
  
    // Number of queries 
    int q = l.Length;
  
    findOrgArr(arr, l, r, x, n, q); 
} 
}
  
// This code is contributed 
// by Akanksha Rai

输出:

10 0 -10 20 50 

时间复杂度: O(n)

如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。