Stuffs Division | Practice | GeeksforGeeks

Your are given N students with some goodies to be distrubuted among them such that student at ith index gets exactly i amount of goodies (considering no wastage). The goodies has already been distributed by some other. Your task is to check if it can be redistributed such that student at ith index gets i amount of goodies. 
 
Example 1:
Input:
N = 5
Arr = {7, 4, 1, 1, 2}
Output:
YES
Explanation:
Since, all the goods can be
redistributed as 1 2 3 4 5 
(ith students get i number of 
goodies).So, output is YES.
 
Example 2:
Input:
N = 5
Arr = {1, 1, 1, 1, 1}
Output:
NO
 
Your Task:  
You don't need to read input or print anything. Your task is to complete the function leftIndex() which takes the array Arr[] and its size N as inputs and returns  true if we can redistribute in the requried way, otherwise false .

Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)

Constraints:
1 <= N <= 107
0 <= Ai <= 1018
 
 
0
hasnainraza1998hr1 month ago
C++, 0.4
bool possible(long long arr[], long long n)   {       int sum = n*(n+1)/2;       int goodies = 0;       goodies = accumulate(arr,arr+n,goodies);       if(sum==goodies)           return true;       return false;   }
0
jraveen19972 months ago
Python:
Total Time Taken:0.5/6.5
The question just needs u to prove that sum of elements in the array is equal to sum of first n natural numbers
 
class Solution:
    def possible(self, arr, n):
        #code here.
        if sum(arr) == sum(list(range(1,n+1))):
            return True
        else:
            return False
+1
badgujarsachin837 months ago
0
D Sathwik1 year ago
D Sathwik
https://uploads.disquscdn.c...
0
Amar Prakash1 year ago
Amar Prakash
class Solution{  public:    bool possible(long long arr[], long long n)    {        int sum =0;        int sum1 = (n*(n+1))/2;        for(int i=0; i<n; i++)="" {="" sum="" +="arr[i];" }="" if(sum="=" sum1)="" {="" return="" true;="" }="" else="" {="" return="" false;="" }="" code="" here.="" }="" };="">
0
pooja upadhyay2 years ago
pooja upadhyay
i guess driver code of the question is wrong.Wrong Answer. !!!Wrong Answer
Possibly your code doesn't work correctly for multiple test-cases (TCs).
The first test case where your code failed:
Input:55 4 3 2 1
Its Correct output is:YES
And Your Code'your output is:Yes
0
sarvesha jaiswal2 years ago
sarvesha jaiswal
for i in range(int(input())):    n=int(input())    a=list(map(int,input().split()))    b=n*(n+1)//2    if sum(a)!=b:        print("NO")    else:        print("YES")
+1
ram2 years ago
ram
The question just needs u to prove that sum of elements in the array is equal to sum of first n natural numbers
0
Akhilesh kumar2 years ago
Akhilesh kumar
someone may explaine  this question??
0
TayfunYirdem2 years ago
TayfunYirdem
Single-line solution with LINQhttps://ide.geeksforgeeks.o...
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.