给定一系列正负元素。任务是用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的所有正负元素之和的abs值。现在找出两者的绝对差求和并用第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 negatve 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 negatve 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 negatve 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 negatve 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 negatve 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 negatve 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 negatve 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 negatve 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。毕竟,用绝对差即abs(diff)替换第i个元素。
下面是上述方法的实现:
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 negatve 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 negatve 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 negatve 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 negatve 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
Java脚本
输出:
2 3 1 2 0
2 2 4 1 2 0
时间复杂度: O(N),其中N是元素数。
如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。