@aashi3007
here, refer my c++ code for better understanding of the logic
#include <bits/stdc++.h>
using namespace std;
int main() {
// your code goes here
int t;
cin>>t;
while(t--)
{
long long int n;
cin>>n;
long long int a[n];
vector<long long int> v;
map<int,int> mp;
for(int i=0;i<n;i++)
{
cin>>a[i];
}
sort(a,a+n);
long long int val=a[0];
v.push_back(a[0]);
mp[0]=1;
while(1)
{
int u=upper_bound(a,a+n,val)-a;
if(u!=n)
{
v.push_back(a[u]);
mp[u]=1;
val+=a[u];
}
else
break;
}
cout<<n-v.size()<<endl;
for(int i=0;i<v.size();i++)
{
cout<<v[i]<<" ";
}
for(int i=0;i<n;i++)
{
if(mp[i]==0)
cout<<a[i]<<" ";
}
cout<<endl;
}
return 0;
}
no…the approach is correct for trivial testcases…your solution fails for the testcases [1,2,3,5] as the count will be 2 but the correct rearrangement will be [1,2,5,3].u have discarded all the values which is less than the prefix sum values…and forgot to substract it from the prefix array .so the test cases are failing…a little bit of updation to ur code will make it work perfectly…my code :
/* Name of the class has to be “Main” only if the class is public. */
class Codechef
{
public static void main (String args) throws java.lang.Exception
{
// your code goes here
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
while(t>0){
int n =sc.nextInt();
int arr=new int[n];
for(int i=0;i<n;i++)
arr[i]=sc.nextInt();