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 changed 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 = 15Input: 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++
// 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; } |
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
<script> Â //Javascript implementation of the approach Â
// Function that returns true if the // array can be redistributed to // the form 1, 2, 3, ..., N function canRedistribute(a, n) { Â
    // Calculate the sum of the array elements     var sum = 0;     for ( var i = 0; i < n; i++)         sum += a[i]; Â
    // If can be redistributed     if (sum == (n * (n + 1)) / 2)         return true ; Â
    return false ; } Â
var a = [ 7, 4, 1, 1, 2 ]; var n = a.length; Â
    if (canRedistribute(a, n))         document.write( "Yes" );     else        document.write( "No" ); Â
Â
// This code is contributed by SoumikMondal </script> |
Yes
Time Complexity: O(n), where n is the length of the given array.
Auxiliary Space: O(1)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!