📌  相关文章
📜  检查是否可以重新分配阵列

📅  最后修改于: 2022-05-13 01:57:53.046000             🧑  作者: Mango

检查是否可以重新分配阵列

给定一个包含N个整数的数组arr[] ,任务是检查是否可以重新分配数组,使得对于每个1 ≤ i ≤ N (基于 1 的索引) arr[i] = i 。重新分配数组意味着数组的所有元素都可以更改为任何其他元素,但结果数组的总和必须等于原始数组总和。
例子:

处理方法:给定数组的和在修改后一定不能改变。因此,计算给定数组的总和,为了使数组的形式为1, 2, 3, ..., N ,数组元素的总和必须为(N * (N + 1)) / 2 。否则,这是不可能的。
下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
 
// Function that returns true if the
// array can be redistributed to
// the form 1, 2, 3, ..., N
bool canRedistribute(int* a, int n)
{
 
    // Calculate the sum of the array elements
    int sum = 0;
    for (int i = 0; i < n; i++)
        sum += a[i];
 
    // If can be redistributed
    if (sum == (n * (n + 1)) / 2)
        return true;
 
    return false;
}
 
// Driver code
int main()
{
    int a[] = { 7, 4, 1, 1, 2 };
    int n = sizeof(a) / sizeof(int);
 
    if (canRedistribute(a, n))
        cout << "Yes";
    else
        cout << "No";
 
    return 0;
}


Java
// Java implementation of the approach
import java.io.*;
 
class GFG
{
 
// Function that returns true if the
// array can be redistributed to
// the form 1, 2, 3, ..., N
static boolean canRedistribute(int []a, int n)
{
 
    // Calculate the sum of the array elements
    int sum = 0;
    for (int i = 0; i < n; i++)
        sum += a[i];
 
    // If can be redistributed
    if (sum == (n * (n + 1)) / 2)
        return true;
 
    return false;
}
 
// Driver code
public static void main (String[] args)
{
    int a[] = { 7, 4, 1, 1, 2 };
    int n = a.length;
 
    if (canRedistribute(a, n))
        System.out.print( "Yes");
    else
        System.out.print("No");
}
}
 
// This code is contributed by anuj_67..


Python3
# Python implementation of the approach
 
# Function that returns true if the
# array can be redistributed to
# the form 1, 2, 3, ..., N
def canRedistribute(a, n):
 
    # Calculate the sum of the array elements
    sum = 0;
    for i in range(n):
        sum += a[i];
 
    # If can be redistributed
    if (sum == (n * (n + 1)) / 2):
        return True;
 
    return False;
 
# Driver code
 
a = [7, 4, 1, 1, 2 ];
n = len(a);
 
if (canRedistribute(a, n)):
    print("Yes");
else:
    print("No");
 
# This code is contributed by 29AjayKumar


C#
// C# implementation of the approach
using System;
     
class GFG
{
 
// Function that returns true if the
// array can be redistributed to
// the form 1, 2, 3, ..., N
static Boolean canRedistribute(int []a, int n)
{
 
    // Calculate the sum of the array elements
    int sum = 0;
    for (int i = 0; i < n; i++)
        sum += a[i];
 
    // If can be redistributed
    if (sum == (n * (n + 1)) / 2)
        return true;
 
    return false;
}
 
// Driver code
public static void Main (String[] args)
{
    int []a = { 7, 4, 1, 1, 2 };
    int n = a.Length;
 
    if (canRedistribute(a, n))
        Console.WriteLine( "Yes");
    else
        Console.WriteLine("No");
}
}
 
/* This code is contributed by PrinciRaj1992 */


Javascript


输出:
Yes