Skip to content
Programmingoneonone
Programmingoneonone
  • Home
  • CS Subjects
    • Internet of Things (IoT)
    • 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
Programmingoneonone

HackerEarth K – Jump problem solution

YASH PAL, 31 July 2024
In this HackerEarth K – Jump problem solution we have given an array A of size N, you can jump from an index i to another index j if  A[j] – A[i] >= K, for j > i. Find the length of the longest sequence of jumps that can be possible in the array. You can start at any index.
HackerEarth K - Jump problem solution

HackerEarth K – Jump problem solution.

#include <bits/stdc++.h>

using namespace std;
#define pb push_back
#define MAX5 1000005
int arr[MAX5];
int n,k;
int dp[MAX5];
int n2(){
int ans = 0;
for(int i = 0; i < n; i++){
dp[i] = 1;
}
for(int i = 1; i < n; i++){
for(int j = 0; j < i; j++){
if(arr[i] >= arr[j] + k){
dp[i] = max(dp[i], 1 + dp[j]);
}
}
}
for(int i = 0; i < n; i++){
ans = max(ans, dp[i]);
}
return ans;
}

multiset <int> p;
multiset <int> :: iterator it;

int nlogn(){
p.clear();
for(int i=0;i<n;i++)
{
int tmp = arr[i];
it = p.upper_bound(tmp-k);
if(it == p.end())
{
p.insert(tmp);
continue;
}
if(*it > tmp)
{
p.erase(it);
p.insert(tmp);
}
}
return p.size();
}

int main()
{
cin >> k >> n;
for(int i = 0; i < n; i++){
cin >> arr[i];
}
cout << nlogn() << endl;
}
coding problems solutions

Post navigation

Previous post
Next post

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