📜  偶数值的总和并更新数组上的查询

📅  最后修改于: 2021-04-22 03:06:28             🧑  作者: Mango

给定整数数组arr []和查询数组q []
对于第i查询, index = q [i] [0]value = q [i] [1] 。该任务针对每个查询,更新arr [index] = arr [index] +值,然后返回数组中所有偶数元素的和。

例子:

天真的方法:对于每个查询,更新数组中的值并计算数组中所有偶数值的总和。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
  
// Function to return the sum of even elements
// after updating value at given index
int EvenSum(vector& A, int index, int value)
{
    // Add given value to A[index]
    A[index] = A[index] + value;
  
    // To store the sum of even elements
    int sum = 0;
  
    for (int i = 0; i < A.size(); i++)
  
        // If current element is even
        if (A[i] % 2 == 0)
            sum = sum + A[i];
  
    return sum;
}
  
// Function to print the result for every query
void BalanceArray(vector& A, vector >& Q)
{
  
    // Resultant vector that stores
    // the result for every query
    vector ANS;
  
    int i, sum;
  
    for (i = 0; i < Q.size(); i++) {
  
        int index = Q[i][0];
        int value = Q[i][1];
  
        // Get sum of even elements after updating
        // value at given index
        sum = EvenSum(A, index, value);
  
        // Store sum for each query
        ANS.push_back(sum);
    }
  
    // Print the result for every query
    for (i = 0; i < ANS.size(); i++)
        cout << ANS[i] << " ";
}
  
// Driver code
int main()
{
    vector A = { 1, 2, 3, 4 };
    vector > Q = { { 0, 1 },
                               { 1, -3 },
                               { 0, -4 },
                               { 3, 2 } };
    BalanceArray(A, Q);
  
    return 0;
}


Java
// Java implementation of the approach
class GFG
{
    // Function to return the sum of even elements
    // after updating value at given index
    static int EvenSum(int [] A, int index, int value)
    {
        // Add given value to A[index]
        A[index] = A[index] + value;
      
        // To store the sum of even elements
        int sum = 0;
      
        for (int i = 0; i < A.length; i++)
      
            // If current element is even
            if (A[i] % 2 == 0)
                sum = sum + A[i];
      
        return sum;
    }
      
    // Function to print the result for every query
    static void BalanceArray(int [] A, int [][] Q)
    {
      
        // Resultant vector that stores
        // the result for every query
        int [] ANS = new int[Q.length];
        int i, sum;
      
        for (i = 0; i < Q.length; i++) 
        {
      
            int index = Q[i][0];
            int value = Q[i][1];
      
            // Get sum of even elements after updating
            // value at given index
            sum = EvenSum(A, index, value);
      
            // Store sum for each query
            ANS[i] = sum;
        }
      
        // Print the result for every query
        for (i = 0; i < ANS.length; i++)
            System.out.print(ANS[i] + " ");
    }
      
    // Driver code
    public static void main(String []args)
    {
        int [] A = { 1, 2, 3, 4 };
        int [][] Q = { { 0, 1 },
                        { 1, -3 },
                        { 0, -4 },
                        { 3, 2 } };
        BalanceArray(A, Q);
      
    }
}
  
// This code is contributed by ihritik


C#
// C# implementation of the approach
using System;
  
class GFG
{
    // Function to return the sum of even elements
    // after updating value at given index
    static int EvenSum(int [] A, int index, int value)
    {
        // Add given value to A[index]
        A[index] = A[index] + value;
      
        // To store the sum of even elements
        int sum = 0;
      
        for (int i = 0; i < A.Length; i++)
      
            // If current element is even
            if (A[i] % 2 == 0)
                sum = sum + A[i];
      
        return sum;
    }
      
    // Function to print the result for every query
    static void BalanceArray(int [] A, int [, ] Q)
    {
      
        // Resultant vector that stores
        // the result for every query
        int [] ANS = new int[Q.GetLength(0)];
        int i, sum;
      
        for (i = 0; i < Q.GetLength(0); i++) 
        {
      
            int index = Q[i, 0];
            int value = Q[i, 1];
      
            // Get sum of even elements after updating
            // value at given index
            sum = EvenSum(A, index, value);
      
            // Store sum for each query
            ANS[i] = sum;
        }
      
        // Print the result for every query
        for (i = 0; i < ANS.Length; i++)
            Console.Write(ANS[i] + " ");
    }
      
    // Driver code
    public static void Main()
    {
        int [] A = new int [] { 1, 2, 3, 4 };
        int [, ] Q = new int[ , ] { { 0, 1 },
                        { 1, -3 },
                        { 0, -4 },
                        { 3, 2 } };
        BalanceArray(A, Q);
      
    }
}
  
// This code is contributed by ihritik


Python3
# Python3 implementation of the approach
  
# Function to return the sum of even elements
# after updating value at given index
def EvenSum(A, index, value):
      
    # Add given value to A[index]
    A[index] = A[index] + value
  
    # To store the sum of even elements
    sum = 0
  
    for i in A:
  
        # If current element is even
        if (i % 2 == 0):
            sum = sum + i
  
    return sum
  
# Function to prthe result for every query
def BalanceArray(A,Q):
  
    # Resultant vector that stores
    # the result for every query
    ANS = []
  
    i, sum = 0, 0
  
    for i in range(len(Q)):
  
        index = Q[i][0]
        value = Q[i][1]
  
        # Get sum of even elements after updating
        # value at given index
        sum = EvenSum(A, index, value)
  
        # Store sum for each query
        ANS.append(sum)
  
    # Print the result for every query
    for i in ANS:
        print(i, end = " ")
  
# Driver code
A = [1, 2, 3, 4]
Q = [ [0, 1 ],
    [1, -3],
    [0, -4],
    [3, 2 ] ]
BalanceArray(A, Q)
  
# This code is contributed by mohit kumar 29


C++
// C++ implementation of the approach
#include 
using namespace std;
  
// Function to print the result for every query
void BalanceArray(vector& A, vector >& Q)
{
    vector ANS;
  
    int i, sum = 0;
  
    for (i = 0; i < A.size(); i++)
  
        // If current element is even
        if (A[i] % 2 == 0)
            sum = sum + A[i];
  
    for (i = 0; i < Q.size(); i++) {
  
        int index = Q[i][0];
        int value = Q[i][1];
  
        // If element is even then
        // remove it from sum
        if (A[index] % 2 == 0)
            sum = sum - A[index];
  
        A[index] = A[index] + value;
  
        // If the value becomes even after updating
        if (A[index] % 2 == 0)
            sum = sum + A[index];
  
        // Store sum for each query
        ANS.push_back(sum);
    }
  
    // Print the result for every query
    for (i = 0; i < ANS.size(); i++)
        cout << ANS[i] << " ";
}
  
// Driver code
int main()
{
    vector A = { 1, 2, 3, 4 };
    vector > Q = { { 0, 1 },
                               { 1, -3 },
                               { 0, -4 },
                               { 3, 2 } };
    BalanceArray(A, Q);
  
    return 0;
}


Java
// Java implementation of the approach
class GFG
{
    // Function to print the result for every query
    static void BalanceArray(int [] A, int [][] Q)
    {
        int [] ANS = new int [A.length];
      
        int i, sum = 0;
      
        for (i = 0; i < A.length; i++)
      
            // If current element is even
            if (A[i] % 2 == 0)
                sum = sum + A[i];
      
        for (i = 0; i < Q.length; i++)
        {
      
            int index = Q[i][0];
            int value = Q[i][1];
      
            // If element is even then
            // remove it from sum
            if (A[index] % 2 == 0)
                sum = sum - A[index];
      
            A[index] = A[index] + value;
      
            // If the value becomes even after updating
            if (A[index] % 2 == 0)
                sum = sum + A[index];
      
            // Store sum for each query
            ANS[i]= sum;
        }
      
        // Print the result for every query
        for (i = 0; i < ANS.length; i++)
            System.out.print(ANS[i] + " ");
    }
      
    // Driver code
    public static void main(String [] args)
    {
        int [] A = { 1, 2, 3, 4 };
        int [][] Q = { { 0, 1 },
                                { 1, -3 },
                                { 0, -4 },
                                { 3, 2 } };
        BalanceArray(A, Q);
    }
}
  
// This code is contributed by ihritik


Python3
# Python3 implementation of the approach 
  
# Function to print the result for 
# every query 
def BalanceArray(A, Q) : 
      
    ANS = []
    sum = 0
  
    for i in range(len(A)) : 
  
        # If current element is even 
        if (A[i] % 2 == 0) :
            sum += A[i]; 
  
    for i in range(len(Q)) :
  
        index = Q[i][0]; 
        value = Q[i][1]; 
  
        # If element is even then 
        # remove it from sum 
        if (A[index] % 2 == 0) :
            sum -= A[index]; 
  
        A[index] += value; 
  
        # If the value becomes even 
        # after updating 
        if (A[index] % 2 == 0) :
            sum += A[index]; 
  
        # Store sum for each query 
        ANS.append(sum); 
      
  
    # Print the result for every query 
    for i in range(len(ANS)) :
        print(ANS[i], end = " "); 
  
# Driver code 
if __name__ == "__main__" :
  
    A = [ 1, 2, 3, 4 ]; 
    Q = [ [ 0, 1 ], [ 1, -3 ], 
          [ 0, -4 ], [ 3, 2 ]]; 
    BalanceArray(A, Q); 
  
# This code is contributed by Ryuga


C#
// C# implementation of the approach
using System;
  
class GFG
{
    // Function to print the result for every query
    static void BalanceArray(int [] A, int [, ] Q)
    {
        int [] ANS = new int [A.Length];
      
        int i, sum = 0;
      
        for (i = 0; i < A.Length; i++)
      
            // If current element is even
            if (A[i] % 2 == 0)
                sum = sum + A[i];
      
        for (i = 0; i < Q.GetLength(0); i++)
        {
      
            int index = Q[i, 0];
            int value = Q[i, 1];
      
            // If element is even then
            // remove it from sum
            if (A[index] % 2 == 0)
                sum = sum - A[index];
      
            A[index] = A[index] + value;
      
            // If the value becomes even after updating
            if (A[index] % 2 == 0)
                sum = sum + A[index];
      
            // Store sum for each query
            ANS[i]= sum;
        }
      
        // Print the result for every query
        for (i = 0; i < ANS.Length; i++)
            Console.Write(ANS[i] + " ");
    }
      
    // Driver code
    public static void Main()
    {
        int [] A = { 1, 2, 3, 4 };
        int [, ] Q = { { 0, 1 },
                                { 1, -3 },
                                { 0, -4 },
                                { 3, 2 } };
        BalanceArray(A, Q);
    }
}
  
// This code is contributed by ihritik


PHP


输出:
8 6 2 4

时间复杂度: O(n 2 )

高效方法:

  • 计算arr []中的数值之和
  • 现在,如果给定索引处的arr []的值是偶数,即arr [index]%2 = 0,则从总和中减去arr [index]。
  • 将给定值添加到arr [index],即arr [index] = arr [index] +值。
  • 现在再次检查arr [index]的值是否为偶数,如果是,则将arr [index]添加到总和中。
  • 打印每个查询的总和值。

下面是上述方法的实现:

C++

// C++ implementation of the approach
#include 
using namespace std;
  
// Function to print the result for every query
void BalanceArray(vector& A, vector >& Q)
{
    vector ANS;
  
    int i, sum = 0;
  
    for (i = 0; i < A.size(); i++)
  
        // If current element is even
        if (A[i] % 2 == 0)
            sum = sum + A[i];
  
    for (i = 0; i < Q.size(); i++) {
  
        int index = Q[i][0];
        int value = Q[i][1];
  
        // If element is even then
        // remove it from sum
        if (A[index] % 2 == 0)
            sum = sum - A[index];
  
        A[index] = A[index] + value;
  
        // If the value becomes even after updating
        if (A[index] % 2 == 0)
            sum = sum + A[index];
  
        // Store sum for each query
        ANS.push_back(sum);
    }
  
    // Print the result for every query
    for (i = 0; i < ANS.size(); i++)
        cout << ANS[i] << " ";
}
  
// Driver code
int main()
{
    vector A = { 1, 2, 3, 4 };
    vector > Q = { { 0, 1 },
                               { 1, -3 },
                               { 0, -4 },
                               { 3, 2 } };
    BalanceArray(A, Q);
  
    return 0;
}

Java

// Java implementation of the approach
class GFG
{
    // Function to print the result for every query
    static void BalanceArray(int [] A, int [][] Q)
    {
        int [] ANS = new int [A.length];
      
        int i, sum = 0;
      
        for (i = 0; i < A.length; i++)
      
            // If current element is even
            if (A[i] % 2 == 0)
                sum = sum + A[i];
      
        for (i = 0; i < Q.length; i++)
        {
      
            int index = Q[i][0];
            int value = Q[i][1];
      
            // If element is even then
            // remove it from sum
            if (A[index] % 2 == 0)
                sum = sum - A[index];
      
            A[index] = A[index] + value;
      
            // If the value becomes even after updating
            if (A[index] % 2 == 0)
                sum = sum + A[index];
      
            // Store sum for each query
            ANS[i]= sum;
        }
      
        // Print the result for every query
        for (i = 0; i < ANS.length; i++)
            System.out.print(ANS[i] + " ");
    }
      
    // Driver code
    public static void main(String [] args)
    {
        int [] A = { 1, 2, 3, 4 };
        int [][] Q = { { 0, 1 },
                                { 1, -3 },
                                { 0, -4 },
                                { 3, 2 } };
        BalanceArray(A, Q);
    }
}
  
// This code is contributed by ihritik

Python3

# Python3 implementation of the approach 
  
# Function to print the result for 
# every query 
def BalanceArray(A, Q) : 
      
    ANS = []
    sum = 0
  
    for i in range(len(A)) : 
  
        # If current element is even 
        if (A[i] % 2 == 0) :
            sum += A[i]; 
  
    for i in range(len(Q)) :
  
        index = Q[i][0]; 
        value = Q[i][1]; 
  
        # If element is even then 
        # remove it from sum 
        if (A[index] % 2 == 0) :
            sum -= A[index]; 
  
        A[index] += value; 
  
        # If the value becomes even 
        # after updating 
        if (A[index] % 2 == 0) :
            sum += A[index]; 
  
        # Store sum for each query 
        ANS.append(sum); 
      
  
    # Print the result for every query 
    for i in range(len(ANS)) :
        print(ANS[i], end = " "); 
  
# Driver code 
if __name__ == "__main__" :
  
    A = [ 1, 2, 3, 4 ]; 
    Q = [ [ 0, 1 ], [ 1, -3 ], 
          [ 0, -4 ], [ 3, 2 ]]; 
    BalanceArray(A, Q); 
  
# This code is contributed by Ryuga

C#

// C# implementation of the approach
using System;
  
class GFG
{
    // Function to print the result for every query
    static void BalanceArray(int [] A, int [, ] Q)
    {
        int [] ANS = new int [A.Length];
      
        int i, sum = 0;
      
        for (i = 0; i < A.Length; i++)
      
            // If current element is even
            if (A[i] % 2 == 0)
                sum = sum + A[i];
      
        for (i = 0; i < Q.GetLength(0); i++)
        {
      
            int index = Q[i, 0];
            int value = Q[i, 1];
      
            // If element is even then
            // remove it from sum
            if (A[index] % 2 == 0)
                sum = sum - A[index];
      
            A[index] = A[index] + value;
      
            // If the value becomes even after updating
            if (A[index] % 2 == 0)
                sum = sum + A[index];
      
            // Store sum for each query
            ANS[i]= sum;
        }
      
        // Print the result for every query
        for (i = 0; i < ANS.Length; i++)
            Console.Write(ANS[i] + " ");
    }
      
    // Driver code
    public static void Main()
    {
        int [] A = { 1, 2, 3, 4 };
        int [, ] Q = { { 0, 1 },
                                { 1, -3 },
                                { 0, -4 },
                                { 3, 2 } };
        BalanceArray(A, Q);
    }
}
  
// This code is contributed by ihritik

的PHP


输出:
8 6 2 4

时间复杂度: O(n)