Reducing Time complexity

Question link : [Problem - 1353B - Codeforces]

My solution:[Submission #116918048 - Codeforces]

I have two questions.

What is the time complexity of my solution? is it O(n^2)
How can i improve this?

In case Submission link not working,

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define fast ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
 
 
int main()
{
    fast
    int t;
    cin>>t;
    while(t--)
    {
       ll n,k;
       cin>>n>>k;
       int arr1[n],arr2[n];
       for(int i=0;i<n;i++){
           cin>>arr1[i];
       }
       for(int i=0;i<n;i++){
           cin>>arr2[i];
       }
       sort(arr1,arr1+n);
       sort(arr2,arr2+n,greater<int>());
        for(int i=0;i<k;i++){
            if(arr1[i]<arr2[i]){
                int a=arr2[i];
                arr2[i]=arr1[i];
                arr1[i]=a;
            }
        }
        int sum=0;
        for (int i = 0; i < n; i++)
        {
            sum+=arr1[i];
        }
        cout<<sum<<endl;
    } 
    return 0;
}

Complexity is- O(NlogN + K)
Improvement to- O(NlogN)Check here

1 Like

thanks @ashish_kaur buddy