用所有其他元素之和替换数组中的每个元素

给定一个大小为N的数组,任务是找到加密的数组。通过将原始数组的每个元素替换为剩余数组元素的总和,即。

null

每一次我, arr[i]=sumOfArrayElements–arr[i]

例如:

Input:  arr[] = {5, 1, 3, 2, 4} Output: 10 14 12 13 11Original array {5, 1, 3, 2, 4}Encrypted array is obtained as:= {1+3+2+4, 5+3+2+4, 5+1+2+4, 5+1+3+4, 5+1+3+2}= {10, 14, 12, 13, 11}Each element of original array is replaced by the sum of the remaining array elements.  Input: arr[] = {6, 8, 32, 12, 14, 10, 25 }Output: 101 99 75 95 93 97 82 

这个问题类似于 从加密数组(其他元素和的数组)中查找原始数组。 方法:

  1. 求数组中所有元素的和。
  2. 遍历数组并替换 arr[i] 总arr[i] .

C++

// C++ implementation to find encrypted array
// from the original array
#include <bits/stdc++.h>
using namespace std;
// Finds and prints the elements of the encrypted
// array
void findEncryptedArray( int arr[], int n)
{
// total sum of elements
// of original array
int sum = 0;
for ( int i = 0; i < n; i++)
sum += arr[i];
// calculating and displaying
// elements of encrypted array
for ( int i = 0; i < n; i++)
cout << (sum - arr[i]) << " " ;
}
// Driver program
int main()
{
int arr[] = { 5, 1, 3, 2, 4 };
int N = sizeof (arr) / sizeof (arr[0]);
findEncryptedArray(arr, N);
return 0;
}


JAVA

// Java implementation to find encrypted
// array from the original  array
class GFG {
// Finds and prints the elements
// of the encrypted array
static void findEncryptedArray( int arr[], int n)
{
// total sum of elements
// of original array
int sum = 0 ;
for ( int i = 0 ; i < n; i++)
sum += arr[i];
// calculating and displaying
// elements of encrypted array
for ( int i = 0 ; i < n; i++)
System.out.print(sum - arr[i] + " " );
}
// Driver program
public static void main(String[] args)
{
int arr[] = { 5 , 1 , 3 , 2 , 4 };
int N = arr.length;
findEncryptedArray(arr, N);
}
}


Python 3

# Python 3 implementation
# to find encrypted array
# from the original array
# Finds and prints the elements
# of the encrypted array
def findEncryptedArray(arr, n) :
sum = 0
# total sum of elements
# of original array
for i in range (n) :
sum + = arr[i]
# calculating and displaying
# elements of encrypted array
for i in range (n) :
print ( sum - arr[i], end = " " )
# Driver Code
if __name__ = = "__main__" :
arr = [ 5 , 1 , 3 , 2 , 4 ]
N = len (arr)
# function calling
findEncryptedArray(arr, N)
# This code is contributed by ANKITRAI1


C#

// C# implementation to find
// encrypted array from the
// original array
using System;
class GFG
{
// Finds and prints the elements
// of the encrypted array
static void findEncryptedArray( int []arr,
int n)
{
// total sum of elements
// of original array
int sum = 0;
for ( int i = 0; i < n; i++)
sum += arr[i];
// calculating and displaying
// elements of encrypted array
for ( int i = 0; i < n; i++)
Console.Write(sum - arr[i] + " " );
}
// Driver Code
public static void Main()
{
int []arr = { 5, 1, 3, 2, 4 };
int N = arr.Length;
findEncryptedArray(arr, N);
}
}
// This code is contributed
// by inder_verma.


PHP

<?php
// PHP implementation to
// find encrypted array
// from the original array
// Finds and prints the
// elements of the encrypted
// array
function findEncryptedArray(& $arr , $n )
{
// total sum of elements
// of original array
$sum = 0;
for ( $i = 0; $i < $n ; $i ++)
$sum += $arr [ $i ];
// calculating and displaying
// elements of encrypted array
for ( $i = 0; $i < $n ; $i ++)
echo ( $sum - $arr [ $i ]) . " " ;
}
// Driver Code
$arr = array (5, 1, 3, 2, 4 );
$N = sizeof( $arr );
findEncryptedArray( $arr , $N );
// This code is contributed
// by ChitraNayal
?>


Javascript

<script>
// JavaScript implementation to find encrypted
// array from the original  array
// Finds and prints the elements
// of the encrypted array
function findEncryptedArray(arr,n)
{
// total sum of elements
// of original array
let sum = 0;
for (let i = 0; i < n; i++)
sum += arr[i];
// calculating and displaying
// elements of encrypted array
for (let i = 0; i < n; i++)
document.write(sum - arr[i] + " " );
}
// Driver program
let arr=[5, 1, 3, 2, 4 ];
let N = arr.length;
findEncryptedArray(arr, N);
// This code is contributed by rag2127
</script>


输出:

10 14 12 13 11

时间复杂性: O(n)

© 版权声明
THE END
喜欢就支持一下吧
点赞14 分享