Check if it is possible to redistribute the Array

Given an array arr[] of N integers, the task is to check if it is possible to redistribute the array such that for every 1 ≤ i ≤ N (1-based indexing) arr[i] = i. Redistributing the array means all the elements of the array can be chanaged to any other element but the sum of the resultant array must be equal to the original array sum.

Examples:

Input: arr[] = {7, 4, 1, 1, 2}
Output: Yes
7 + 4 + 1 + 1 + 2 = 15
1 + 2 + 3 + 4 + 5 = 15



Input: arr[] = {1, 1, 1, 1}
Output: No
1 + 1 + 1 + 1 = 4
1 + 2 + 3 + 4 = 10

Approach: It is given that the sum of the array must not change after the modification. So, calculate the sum of the given array and in order for the array to be of the form 1, 2, 3, …, N, the sum of the array elements must be (N * (N + 1)) / 2. Else, it is impossible.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the approach
#include <bits/stdc++.h>
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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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..

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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 */

chevron_right


Output:

Yes


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.





Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.