📌  相关文章
📜  用该元素后的正负数之和的差替换所有元素

📅  最后修改于: 2021-10-26 05:50:57             🧑  作者: Mango

给定一组正负元素。任务是用i+1 到 N范围内正负元素的绝对和的绝对差替换数组的每个第i 个元素。即,求i+1 到 N范围内所有正元素的绝对和和所有负元素的绝对和。现在找出这两个和的绝对差并用第 i 个元素替换。

注意:更新数组的最后一个元素将为零。

例子:

Input : N = 5,  arr[] = {1, -1, 2, 3, -2}
Output : arr[] = {2, 3, 1, 2, 0}

Input : N = 6,  arr[] = {-3, -4, -2, 5, 1, -2}
Output : arr[] = {2, 2, 4, 1, 2, 0}.

朴素的方法:朴素的方法是运行两个 for 循环,对于所有第 i 个元素,计算索引在 i+1 到 N 范围内的所有正负元素之和的绝对值。现在找到两者的绝对差求和并替换为第 i 个元素。
这种方法的时间复杂度为 O(N 2 ),其中 N 是数组中的元素数。

下面是上述方法的实现:

C++
// C++ program to implement above approach
 
#include 
using namespace std;
 
// Function to print the array elements
void printArray(int N, int arr[])
{
    for (int i = 0; i < N; i++)
        cout << arr[i] << " ";
 
    cout << endl;
}
 
// Function to replace all elements with absolute
// difference of absolute sums of positive
// and negative elements
void replacedArray(int N, int arr[])
{
    int pos_sum, neg_sum, i, j, diff;
 
    for (i = 0; i < N; i++) {
        pos_sum = 0;
        neg_sum = 0;
 
        // Calculate absolute sums of possitive
        // and negative elements in range i+1 to N
        for (j = i + 1; j < N; j++) {
            if (arr[j] > 0)
                pos_sum += arr[j];
            else
                neg_sum += arr[j];
        }
 
        // calculate difference of both sums
        diff = abs(pos_sum) - abs(neg_sum);
 
        // replace i-th elements with absolute
        // difference
        arr[i] = abs(diff);
    }
}
 
// Driver code
int main()
{
    int N = 5;
    int arr[] = { 1, -1, 2, 3, -2 };
    replacedArray(N, arr);
    printArray(N, arr);
 
    N = 6;
    int arr1[] = { -3, -4, -2, 5, 1, -2 };
    replacedArray(N, arr1);
    printArray(N, arr1);
 
    return 0;
}


Java
// Java program to implement above approach
class GFG
{
     
// Function to print the array elements
static void printArray(int N, int []arr)
{
    for (int i = 0; i < N; i++)
        System.out.print(arr[i] + " ");
 
    System.out.println();
}
 
// Function to replace all elements with
// absolute difference of absolute sums
// of positive and negative elements
static void replacedArray(int N, int []arr)
{
    int pos_sum, neg_sum, i, j, diff;
 
    for (i = 0; i < N; i++)
    {
        pos_sum = 0;
        neg_sum = 0;
 
        // Calculate absolute sums of possitive
        // and negative elements in range i+1 to N
        for (j = i + 1; j < N; j++)
        {
            if (arr[j] > 0)
                pos_sum += arr[j];
            else
                neg_sum += arr[j];
        }
 
        // calculate difference of both sums
        diff = Math.abs(pos_sum) - Math.abs(neg_sum);
 
        // replace i-th elements with absolute
        // difference
        arr[i] = Math.abs(diff);
    }
}
 
// Driver code
public static void main(String args[])
{
    int N = 5;
    int []arr = { 1, -1, 2, 3, -2 };
    replacedArray(N, arr);
    printArray(N, arr);
 
    N = 6;
    int []arr1 = { -3, -4, -2, 5, 1, -2 };
    replacedArray(N, arr1);
    printArray(N, arr1);
}
}
 
// This code is contributed by Akanksha Rai


Python3
# Python 3 program to implement
# above approach
 
# Function to print the array elements
def printArray(N, arr):
    for i in range(N):
        print(arr[i], end = " ")
 
    print("\n", end = "")
 
# Function to replace all elements with
# absolute difference of absolute sums
# of positive and negative elements
def replacedArray(N, arr):
    for i in range(N):
        pos_sum = 0
        neg_sum = 0
 
        # Calculate absolute sums of possitive
        # and negative elements in range i+1 to N
        for j in range(i + 1, N, 1):
            if (arr[j] > 0):
                pos_sum += arr[j]
            else:
                neg_sum += arr[j]
 
        # calculate difference of both sums
        diff = abs(pos_sum) - abs(neg_sum)
 
        # replace i-th elements with absolute
        # difference
        arr[i] = abs(diff)
 
# Driver code
if __name__ == '__main__':
    N = 5
    arr = [1, -1, 2, 3, -2]
    replacedArray(N, arr)
    printArray(N, arr)
 
    N = 6
    arr1 = [-3, -4, -2, 5, 1, -2]
    replacedArray(N, arr1)
    printArray(N, arr1)
 
# This code is contributed by
# Surendra_Gangwar


C#
// C# program to implement above approach
using System;
 
class GFG
{
     
// Function to print the array elements
static void printArray(int N, int []arr)
{
    for (int i = 0; i < N; i++)
        Console.Write(arr[i] + " ");
 
    Console.WriteLine();
}
 
// Function to replace all elements with
// absolute difference of absolute sums
// of positive and negative elements
static void replacedArray(int N, int []arr)
{
    int pos_sum, neg_sum, i, j, diff;
 
    for (i = 0; i < N; i++)
    {
        pos_sum = 0;
        neg_sum = 0;
 
        // Calculate absolute sums of possitive
        // and negative elements in range i+1 to N
        for (j = i + 1; j < N; j++)
        {
            if (arr[j] > 0)
                pos_sum += arr[j];
            else
                neg_sum += arr[j];
        }
 
        // calculate difference of both sums
        diff = Math.Abs(pos_sum) - Math.Abs(neg_sum);
 
        // replace i-th elements with absolute
        // difference
        arr[i] = Math.Abs(diff);
    }
}
 
// Driver code
static void Main()
{
    int N = 5;
    int []arr = { 1, -1, 2, 3, -2 };
    replacedArray(N, arr);
    printArray(N, arr);
 
    N = 6;
    int []arr1 = { -3, -4, -2, 5, 1, -2 };
    replacedArray(N, arr1);
    printArray(N, arr1);
}
}
 
// This code is contributed by mits


Javascript


C++
// C++ program to implement above approach
 
#include 
using namespace std;
 
// Function to print the array elements
void printArray(int N, int arr[])
{
    for (int i = 0; i < N; i++)
        cout << arr[i] << " ";
 
    cout << endl;
}
 
// Function to replace all elements with absolute
// difference of absolute sums of positive
// and negative elements
void replacedArray(int N, int arr[])
{
    int pos_sum, neg_sum, i, j, diff;
 
    pos_sum = 0;
    neg_sum = 0;
 
    for (i = N - 1; i >= 0; i--) {
 
        // calculate differenbce of both sums
        diff = abs(pos_sum) - abs(neg_sum);
 
        // if i-th element is positive,
        // add it to positive sum
        if (arr[i] > 0)
            pos_sum += arr[i];
 
        // if i-th element is negative,
        // add it to negative sum
        else
            neg_sum += arr[i];
 
        // replace i-th elements with
        // absolute difference
        arr[i] = abs(diff);
    }
}
 
// Driver Code
int main()
{
    int N = 5;
    int arr[] = { 1, -1, 2, 3, -2 };
    replacedArray(N, arr);
    printArray(N, arr);
 
    N = 6;
    int arr1[] = { -3, -4, -2, 5, 1, -2 };
    replacedArray(N, arr1);
    printArray(N, arr1);
 
    return 0;
}


Java
// Java program to implement above approach
class GFG
{
     
    // Function to print the array elements
    static void printArray(int N, int arr[])
    {
        for (int i = 0; i < N; i++)
            System.out.print(arr[i] + " ");
     
        System.out.println();
    }
     
    // Function to replace all elements with absolute
    // difference of absolute sums of positive
    // and negative elements
    static void replacedArray(int N, int arr[])
    {
        int pos_sum, neg_sum, i, j, diff;
     
        pos_sum = 0;
        neg_sum = 0;
     
        for (i = N - 1; i >= 0; i--)
        {
     
            // calculate differenbce of both sums
            diff = Math.abs(pos_sum) - Math.abs(neg_sum);
     
            // if i-th element is positive,
            // add it to positive sum
            if (arr[i] > 0)
                pos_sum += arr[i];
     
            // if i-th element is negative,
            // add it to negative sum
            else
                neg_sum += arr[i];
     
            // replace i-th elements with
            // absolute difference
            arr[i] = Math.abs(diff);
        }
    }
     
    // Driver Code
    public static void main (String[] args)
    {
        int N = 5;
        int arr[] = { 1, -1, 2, 3, -2 };
        replacedArray(N, arr);
        printArray(N, arr);
     
        N = 6;
        int arr1[] = { -3, -4, -2, 5, 1, -2 };
        replacedArray(N, arr1);
        printArray(N, arr1);
    }
}
 
// This code is contributed by ihritik


Python3
# Python program to implement above approach
 
# Function to print the array elements
def printArray(N, arr) :
 
    for i in range (0, N) :
        print(arr[i], end=" ")
 
    print()
 
 
# Function to replace all elements with absolute
# difference of absolute sums of positive
# and negative elements
def replacedArray(N, arr) :
 
     
    pos_sum = 0
    neg_sum = 0
 
    for i in range (N - 1,-1, -1) :
 
        # calculate differenbce of both sums
        diff = abs(pos_sum) - abs(neg_sum)
 
        # if i-th element is positive,
        # add it to positive sum
        if (arr[i] > 0) :
            pos_sum = pos_sum + arr[i]
 
        # if i-th element is negative,
        # add it to negative sum
        else :
            neg_sum = neg_sum + arr[i]
 
        # replace i-th elements with
        # absolute difference
        arr[i] = abs(diff)
 
# Driver Code
 
N = 5
arr = [ 1, -1, 2, 3, -2 ]
replacedArray(N, arr)
printArray(N, arr)
 
N = 6
arr1 = [ -3, -4, -2, 5, 1, -2 ]
replacedArray(N, arr1)
printArray(N, arr1)
 
# This code is contributed by ihritik


C#
// C# program to implement above approach
using System;
 
class GFG
{
     
    // Function to print the array elements
    static void printArray(int N, int [] arr)
    {
        for (int i = 0; i < N; i++)
            Console.Write(arr[i] + " ");
     
        Console.WriteLine();
    }
     
    // Function to replace all elements with absolute
    // difference of absolute sums of positive
    // and negative elements
    static void replacedArray(int N, int [] arr)
    {
        int pos_sum, neg_sum, i, diff;
     
        pos_sum = 0;
        neg_sum = 0;
     
        for (i = N - 1; i >= 0; i--)
        {
     
            // calculate differenbce of both sums
            diff = Math.Abs(pos_sum) - Math.Abs(neg_sum);
     
            // if i-th element is positive,
            // add it to positive sum
            if (arr[i] > 0)
                pos_sum += arr[i];
     
            // if i-th element is negative,
            // add it to negative sum
            else
                neg_sum += arr[i];
     
            // replace i-th elements with
            // absolute difference
            arr[i] = Math.Abs(diff);
        }
    }
     
    // Driver Code
    public static void Main ()
    {
        int N = 5;
        int [] arr = { 1, -1, 2, 3, -2 };
        replacedArray(N, arr);
        printArray(N, arr);
     
        N = 6;
        int [] arr1 = { -3, -4, -2, 5, 1, -2 };
        replacedArray(N, arr1);
        printArray(N, arr1);
    }
}
 
// This code is contributed by ihritik


Javascript


输出:
2 3 1 2 0 
2 2 4 1 2 0

有效方法:将正和负和初始化为 0。现在从最后一个元素到第一个元素运行单个 for 循环并计算 diff = abs(pos_sum) – abs(neg_sum)。
现在,如果第 i 个元素为正数,则将其添加到 pos_sum 中,否则将其添加到 neg_sum 中。毕竟,用绝对差值替换第 i 个元素,即 abs(diff)。

下面是上述方法的实现:

C++

// C++ program to implement above approach
 
#include 
using namespace std;
 
// Function to print the array elements
void printArray(int N, int arr[])
{
    for (int i = 0; i < N; i++)
        cout << arr[i] << " ";
 
    cout << endl;
}
 
// Function to replace all elements with absolute
// difference of absolute sums of positive
// and negative elements
void replacedArray(int N, int arr[])
{
    int pos_sum, neg_sum, i, j, diff;
 
    pos_sum = 0;
    neg_sum = 0;
 
    for (i = N - 1; i >= 0; i--) {
 
        // calculate differenbce of both sums
        diff = abs(pos_sum) - abs(neg_sum);
 
        // if i-th element is positive,
        // add it to positive sum
        if (arr[i] > 0)
            pos_sum += arr[i];
 
        // if i-th element is negative,
        // add it to negative sum
        else
            neg_sum += arr[i];
 
        // replace i-th elements with
        // absolute difference
        arr[i] = abs(diff);
    }
}
 
// Driver Code
int main()
{
    int N = 5;
    int arr[] = { 1, -1, 2, 3, -2 };
    replacedArray(N, arr);
    printArray(N, arr);
 
    N = 6;
    int arr1[] = { -3, -4, -2, 5, 1, -2 };
    replacedArray(N, arr1);
    printArray(N, arr1);
 
    return 0;
}

Java

// Java program to implement above approach
class GFG
{
     
    // Function to print the array elements
    static void printArray(int N, int arr[])
    {
        for (int i = 0; i < N; i++)
            System.out.print(arr[i] + " ");
     
        System.out.println();
    }
     
    // Function to replace all elements with absolute
    // difference of absolute sums of positive
    // and negative elements
    static void replacedArray(int N, int arr[])
    {
        int pos_sum, neg_sum, i, j, diff;
     
        pos_sum = 0;
        neg_sum = 0;
     
        for (i = N - 1; i >= 0; i--)
        {
     
            // calculate differenbce of both sums
            diff = Math.abs(pos_sum) - Math.abs(neg_sum);
     
            // if i-th element is positive,
            // add it to positive sum
            if (arr[i] > 0)
                pos_sum += arr[i];
     
            // if i-th element is negative,
            // add it to negative sum
            else
                neg_sum += arr[i];
     
            // replace i-th elements with
            // absolute difference
            arr[i] = Math.abs(diff);
        }
    }
     
    // Driver Code
    public static void main (String[] args)
    {
        int N = 5;
        int arr[] = { 1, -1, 2, 3, -2 };
        replacedArray(N, arr);
        printArray(N, arr);
     
        N = 6;
        int arr1[] = { -3, -4, -2, 5, 1, -2 };
        replacedArray(N, arr1);
        printArray(N, arr1);
    }
}
 
// This code is contributed by ihritik

蟒蛇3

# Python program to implement above approach
 
# Function to print the array elements
def printArray(N, arr) :
 
    for i in range (0, N) :
        print(arr[i], end=" ")
 
    print()
 
 
# Function to replace all elements with absolute
# difference of absolute sums of positive
# and negative elements
def replacedArray(N, arr) :
 
     
    pos_sum = 0
    neg_sum = 0
 
    for i in range (N - 1,-1, -1) :
 
        # calculate differenbce of both sums
        diff = abs(pos_sum) - abs(neg_sum)
 
        # if i-th element is positive,
        # add it to positive sum
        if (arr[i] > 0) :
            pos_sum = pos_sum + arr[i]
 
        # if i-th element is negative,
        # add it to negative sum
        else :
            neg_sum = neg_sum + arr[i]
 
        # replace i-th elements with
        # absolute difference
        arr[i] = abs(diff)
 
# Driver Code
 
N = 5
arr = [ 1, -1, 2, 3, -2 ]
replacedArray(N, arr)
printArray(N, arr)
 
N = 6
arr1 = [ -3, -4, -2, 5, 1, -2 ]
replacedArray(N, arr1)
printArray(N, arr1)
 
# This code is contributed by ihritik

C#

// C# program to implement above approach
using System;
 
class GFG
{
     
    // Function to print the array elements
    static void printArray(int N, int [] arr)
    {
        for (int i = 0; i < N; i++)
            Console.Write(arr[i] + " ");
     
        Console.WriteLine();
    }
     
    // Function to replace all elements with absolute
    // difference of absolute sums of positive
    // and negative elements
    static void replacedArray(int N, int [] arr)
    {
        int pos_sum, neg_sum, i, diff;
     
        pos_sum = 0;
        neg_sum = 0;
     
        for (i = N - 1; i >= 0; i--)
        {
     
            // calculate differenbce of both sums
            diff = Math.Abs(pos_sum) - Math.Abs(neg_sum);
     
            // if i-th element is positive,
            // add it to positive sum
            if (arr[i] > 0)
                pos_sum += arr[i];
     
            // if i-th element is negative,
            // add it to negative sum
            else
                neg_sum += arr[i];
     
            // replace i-th elements with
            // absolute difference
            arr[i] = Math.Abs(diff);
        }
    }
     
    // Driver Code
    public static void Main ()
    {
        int N = 5;
        int [] arr = { 1, -1, 2, 3, -2 };
        replacedArray(N, arr);
        printArray(N, arr);
     
        N = 6;
        int [] arr1 = { -3, -4, -2, 5, 1, -2 };
        replacedArray(N, arr1);
        printArray(N, arr1);
    }
}
 
// This code is contributed by ihritik

Javascript


输出:
2 3 1 2 0 
2 2 4 1 2 0

时间复杂度: O(N),其中 N 是元素的数量。