Given an integer array arr[] of n elements, where arr[i] represents the number of goodies currently held by the (i+1)th student. The goodies can be redistributed among the students. Determine whether it is possible to redistribute them so that the student at 1-based index i receives exactly i goodies, with no goodies lost or created. Return true if such a redistribution is possible; otherwise, return false.
Examples:
Input: arr[] = [7, 4, 1, 1, 2]
Output: true
Explanation: The total number of goodies is 15, which is equal to 1 + 2 + 3 + 4 + 5. Therefore, the goodies can be redistributed so that the ith student receives exactly i goodies.Input: arr[] = [1, 1, 1, 1, 1]
Output: false
Explanation: The total number of goodies is 5, whereas 1 + 2 + 3 + 4 + 5 = 15. Hence, the required distribution is not possible.
Table of Content
[Naive Approach] Using Iterative Sum - O(n) Time and O(1) Space
The idea is to calculate the total number of goodies available and also compute the required number of goodies by adding numbers from 1 to n using a loop. If both totals are equal, the redistribution is possible; otherwise, it is not.
Working of Approach:
- Traverse the array and calculate the total number of goodies.
- Initialize the required sum as 0.
- Add numbers from 1 to n one by one.
- Compare the available and required totals.
- Return true if both are equal; otherwise return false.
#include <iostream>
#include <vector>
using namespace std;
bool isPossible(vector<int> &arr)
{
int sum = 0;
// Calculate the total number of goodies.
for (int x : arr)
sum += x;
int need = 0;
// Calculate the required goodies using a loop.
for (int i = 1; i <= arr.size(); i++)
need += i;
return sum == need;
}
int main()
{
vector<int> arr = {7, 4, 1, 1, 2};
if (isPossible(arr))
cout << "true";
else
cout << "false";
return 0;
}
class GFG {
public boolean isPossible(int[] arr)
{
int sum = 0;
// Calculate the total number of goodies.
for (int x : arr)
sum += x;
int need = 0;
// Calculate the required goodies using a loop.
for (int i = 1; i <= arr.length; i++)
need += i;
return sum == need;
}
public static void main(String[] args)
{
int[] arr = { 7, 4, 1, 1, 2 };
GFG obj = new GFG();
if (obj.isPossible(arr))
System.out.print("true");
else
System.out.print("false");
}
}
def isPossible(arr):
sum = 0
# Calculate the total number of goodies.
for x in arr:
sum += x
need = 0
# Calculate the required goodies using a loop.
for i in range(1, len(arr) + 1):
need += i
return sum == need
if __name__ == "__main__":
arr = [7, 4, 1, 1, 2]
if isPossible(arr):
print("true")
else:
print("false")
using System;
class GFG {
public bool isPossible(int[] arr)
{
int sum = 0;
// Calculate the total number of goodies.
foreach(int x in arr) sum += x;
int need = 0;
// Calculate the required goodies using a loop.
for (int i = 1; i <= arr.Length; i++)
need += i;
return sum == need;
}
static void Main()
{
int[] arr = { 7, 4, 1, 1, 2 };
GFG obj = new GFG();
if (obj.isPossible(arr))
Console.Write("true");
else
Console.Write("false");
}
}
function isPossible(arr)
{
let sum = 0;
// Calculate the total number of goodies.
for (let x of arr)
sum += x;
let need = 0;
// Calculate the required goodies using a loop.
for (let i = 1; i <= arr.length; i++)
need += i;
return sum === need;
}
// Driver Code
let arr = [ 7, 4, 1, 1, 2 ];
if (isPossible(arr))
console.log("true");
else
console.log("false");
Output
true
[Expected Approach] Sum Formula - O(n) Time and O(1) Space
The idea is to observe that goodies can be redistributed freely, so only the total number of goodies matters. Compute the required total using the formula n × (n + 1) / 2 and compare it with the sum of the array.
Working of Approach:
- Find the total number of goodies in the array.
- Compute the required total using n * (n + 1) / 2.
- Compare both totals.
- If they are equal, redistribution is possible.
- Otherwise, return false.
Let us understand with an example:
Input: arr[] = [7, 4, 1, 1, 2]
- Total goodies available = 7 + 4 + 1 + 1 + 2 = 15.
- Number of students = 5, so the required goodies are 1 + 2 + 3 + 4 + 5 = 15.
- Since the available total matches the required total, the goodies can be redistributed accordingly.
- Therefore, the answer is true.
#include <iostream>
#include <vector>
using namespace std;
bool isPossible(vector<int> &arr)
{
int sum = 0;
int need = arr.size() * (arr.size() + 1) / 2;
for (int x : arr)
{
// Calculate the total number of goodies.
sum += x;
}
return sum == need;
}
int main()
{
vector<int> arr = {7, 4, 1, 1, 2};
if (isPossible(arr))
cout << "true";
else
cout << "false";
return 0;
}
class GFG {
public boolean isPossible(int[] arr)
{
int sum = 0;
int need = arr.length * (arr.length + 1) / 2;
for (int x : arr) {
// Calculate the total number of goodies.
sum += x;
}
return sum == need;
}
public static void main(String[] args)
{
int[] arr = { 7, 4, 1, 1, 2 };
GFG obj = new GFG();
if (obj.isPossible(arr))
System.out.print("true");
else
System.out.print("false");
}
}
def isPossible(arr):
sum = 0
need = len(arr) * (len(arr) + 1) // 2
for x in arr:
# Calculate the total number of goodies.
sum += x
return sum == need
arr = [7, 4, 1, 1, 2]
if __name__ == "__main__":
arr = [7, 4, 1, 1, 2]
if isPossible(arr):
print("true")
else:
print("false")
using System;
class GFG {
public bool isPossible(int[] arr)
{
int sum = 0;
int need = arr.Length * (arr.Length + 1) / 2;
foreach(int x in arr)
{
// Calculate the total number of goodies.
sum += x;
}
return sum == need;
}
static void Main()
{
int[] arr = { 7, 4, 1, 1, 2 };
GFG obj = new GFG();
if (obj.isPossible(arr))
Console.Write("true");
else
Console.Write("false");
}
}
function isPossible(arr)
{
let sum = 0;
let need = arr.length * (arr.length + 1) / 2;
for (let x of arr) {
// Calculate the total number of goodies.
sum += x;
}
return sum === need;
}
// Driver Code
let arr = [ 7, 4, 1, 1, 2 ];
if (isPossible(arr))
console.log("true");
else
console.log("false");
Output
true