Skip to content
Programming101
Programmingoneonone

Learn everything about programming

  • Home
  • CS Subjects
    • IoT – Internet of Things
    • Digital Communication
    • Human Values
  • 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
Programming101
Programmingoneonone

Learn everything about programming

HackerEarth Smallest subarrays problem solution

YASH PAL, 31 July 2024
In this HackerEarth Smallest subarrays problem solution, You are given two arrays A and B of length N.
For every index i in array A, find the length of the smallest subarray starting from index i such that there exist at least B[i] elements with a value greater than or equal to A[i] in the subarray. If no such subarray exists, then print -1.
HackerEarth Smallest subarrays problem solution

HackerEarth Smallest subarrays problem solution.

#include<bits/stdc++.h>
#define ll long long int
using namespace std;

vector<int> merge(vector<int>& v1, vector<int>& v2)
{
int i = 0, j = 0;

vector<int> v;

while (i < v1.size() && j < v2.size()) {
if (v1[i] <= v2[j]) {
v.push_back(v1[i]);
i++;
}
else {
v.push_back(v2[j]);
j++;
}
}

for (int k = i; k < v1.size(); k++)
v.push_back(v1[k]);
for (int k = j; k < v2.size(); k++)
v.push_back(v2[k]);
return v;
}

void buildTree(vector<int>* tree, int* arr,
int index, int s, int e)
{

if (s == e) {
tree[index].push_back(arr[s]);
return;
}

int mid = (s + e) / 2;
buildTree(tree, arr, 2 * index, s, mid);
buildTree(tree, arr, 2 * index + 1, mid + 1, e);

tree[index] = merge(tree[2 * index], tree[2 * index + 1]);
}

int query(vector<int>* tree, int index, int s,
int e, int l, int r, int k)
{

if (r < s || l > e)
return 0;

if (s >= l && e <= r) {
return (tree[index].size()
- (lower_bound(tree[index].begin(),
tree[index].end(), k)
- tree[index].begin()));
}

int mid = (s + e) / 2;
return (query(tree, 2 * index, s,
mid, l, r, k)
+ query(tree, 2 * index + 1, mid + 1,
e, l, r, k));
}


void solve()
{
int n;
cin >> n;
assert(1 <= n and n <= 300000);

int a[n];
for(int i = 0 ; i < n ; i++)
{
cin >> a[i];
assert(1 <= a[i] and a[i] <= 1000000000);
}

int b[n];
for(int i = 0 ; i < n ; i++)
{
cin >> b[i];
assert(1 <= b[i] and b[i] <= n);
}

vector<int> tree[4*n+1];
buildTree(tree, a, 1 , 0 , n - 1);

for(int i = 0 ; i < n ; i++)
{
int value = a[i];
int s = i;
int e = n - 1;
int ans = -1;

while(s <= e){
int m = (s+e) >> 1;
int cnt = query(tree, 1, 0, n - 1, i, m, value);

if(cnt >= b[i])
{
ans = m;
e = m - 1;
}
else{
s = m + 1;
}
}

if(ans == -1) cout << ans << " ";
else cout << (ans - i + 1 ) << " ";
}
cout << endl;
}

int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;
t = 1;
while(t--)
{
solve();
}
}
coding problems solutions

Post navigation

Previous post
Next post
  • Automating Image Format Conversion with Python: A Complete Guide
  • HackerRank Separate the Numbers solution
  • How AI Is Revolutionizing Personalized Learning in Schools
  • GTA 5 is the Game of the Year for 2024 and 2025
  • Hackerrank Day 5 loops 30 days of code solution
How to download udemy paid courses for free

Pages

  • About US
  • Contact US
  • Privacy Policy

Programing Practice

  • C Programs
  • java Programs

HackerRank Solutions

  • C
  • C++
  • Java
  • Python
  • Algorithm

Other

  • Leetcode Solutions
  • Interview Preparation

Programming Tutorials

  • DSA
  • C

CS Subjects

  • Digital Communication
  • Human Values
  • Internet Of Things
  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2025 Programmingoneonone | WordPress Theme by SuperbThemes