A cleaner code using modular arithematic: void frequencyCount(vector& arr,int n, int p) { p++; for(int i=0; i= n) continue; int cnt = arr[ind] / p; int ele = arr[ind] % p; cnt ++; arr[ind] = cnt * p + ele; } for (int i=0; i
Your answer is right and easy but there is a little point I want to make They mentioned time complexity as O(1) that means we can not use extra space (int[] freq = new int[N + 1];) becasue this will make the time complexity as O(n).
Karan Mashru is the best teacher GfG has ever produced.Thanks man for wonderful explanation.
great explanation
for(int i=0;iN)
arr[i]=0;
int i=0;
while(i
A cleaner code using modular arithematic:
void frequencyCount(vector& arr,int n, int p)
{
p++;
for(int i=0; i= n)
continue;
int cnt = arr[ind] / p;
int ele = arr[ind] % p;
cnt ++;
arr[ind] = cnt * p + ele;
}
for (int i=0; i
Thanks
Solution giving Run time error.
Easiest Solution -> int[] freq = new int[N + 1];
// Counting the frequency of each element
for (int i = 0; i < N; i++) {
if (arr[i]
Your answer is right and easy but there is a little point I want to make
They mentioned time complexity as O(1) that means we can not use extra space (int[] freq = new int[N + 1];) becasue this will make the time complexity as O(n).
@@antimr2808 ya I didn't take that in account. As It was accepted on gfg. 🥲
wow you give new meaning to Inplace 😆
Thank you. Very well explained.
Tq💖