Skip to content
Programmingoneonone
Programmingoneonone
  • CS Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
    • Cybersecurity
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programmingoneonone
Programmingoneonone

HackerEarth Chandu and his Girlfriend Returns problem solution

YASH PAL, 31 July 2024
In this HackerEarth Chandu and his Girlfriend Returns problem solution In the previous problem Chandu bought some unsorted arrays and sorted them (in non-increasing order). Now, he has many sorted arrays to give to his girlfriend. But, the number of sorted arrays are very large so Chandu decided to merge two sorted arrays into one sorted array. But he is too lazy to do that. So, he asked your help to merge the two sorted arrays into one sorted array (in non-increasing order).
HackerEarth Chandu and his Girlfriend Returns problem solution

HackerEarth Chandu and his Girlfriend Returns problem solution.

#include <iostream>
#include <cassert>
#include <vector>
#include <cstdio>

using namespace std;
const int MAX = 1e5 + 5;
long long a[MAX], b[MAX];
vector <long long> c;

void merge1(int N, int M)
{
int x = 0, y = 0;
int k = N + M;
c.clear();
while(x < N and y < M)
{
if(a[x] > b[y])
c.push_back(a[x++]);
else
c.push_back(b[y++]);
}
while(x < N)
c.push_back(a[x++]);
while(y < M)
c.push_back(b[y++]);
}

int main()
{
int t, n, m;
scanf("%d", &t);
assert(1 <= t and t <= 100);
while(t--)
{
scanf("%d %d", &n, &m);
assert(1 <= n and n <= 50000);
assert(1 <= m and m <= 50000);
for(int i = 0;i < n;++i)
{
scanf("%lld", &a[i]);
assert(0 <= a[i] and a[i] <= 1000000000LL);
}
for(int i = 0;i < m;++i)
{
scanf("%lld", &b[i]);
assert(0 <= b[i] and b[i] <= 1000000000LL);
}
merge1(n, m);
for(int i = 0;i < c.size();++i)
if(i < c.size() - 1)
printf("%lld ", c[i]);
else
printf("%lldn", c[i]);
}
return 0;
}

Second solution

#include<bits/stdc++.h>


using namespace std;

#define rep(i,n) for(i=0;i<n;i++)
#define ll long long
#define elif else if
#define pii pair<int,int>
#define mp make_pair
#define pb push_back
#define CLEAR(array, value) memset(ptr, value, sizeof(array));
#define si(a) scanf("%d", &a)
#define sl(a) scanf("%lld", &a)
#define pi(a) printf("%d", a)
#define pl(a) printf("%lld", a)
#define pn printf("n")
#define SZ(x) (int)((x).size())



int main()
{
ios::sync_with_stdio(false);
int t;
//scanf("%d",&t);
cin>>t;
assert(1<=t && t<=100);
while(t--)
{
int i,j,k,n,m;
//scanf("%d",&n);
cin>>n>>m;
assert(1<=n<=50000);
assert(1<=m<=50000);
vector<int>va(n+m);
rep(i,n+m){cin>>va[i];
assert(0<=va[i]<=1000000000);
}
sort(va.rbegin(),va.rend());
rep(i,n+m)cout<<va[i]<<" ";
cout<<"n";

}
return 0;
}
coding problems solutions

Post navigation

Previous post
Next post

Are you a student and stuck with your career or worried about real-time things, and don't know how to manage your learning phase? Which profession to choose? and how to learn new things according to your goal, and land a dream job. Then this might help to you.

Hi My name is YASH PAL, founder of this Blog and a Senior Software engineer with 5+ years of Industry experience. I personally helped 40+ students to make a clear goal in their professional lives. Just book a one-on-one personal call with me for 30 minutes for 300 Rupees. Ask all your doubts and questions related to your career to set a clear roadmap for your professional life.

Book session - https://wa.me/qr/JQ2LAS7AASE2M1

Pages

  • About US
  • Contact US
  • Privacy Policy

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2026 Programmingoneonone | WordPress Theme by SuperbThemes