Hello Programmers/Coders, Today we are going to share solutions of Programming problems of HackerRank, Algorithm Solutions of Problem Solving Section in Java. At Each Problem with Successful submission with all Test Cases Passed, you will get an score or marks. And after solving maximum problems, you will be getting stars. This will highlight your profile to the recruiters.
In this post, you will find the solution for Picking Numbers in Java-HackerRank Problem. We are providing the correct and tested solutions of coding problems present on HackerRank. If you are not able to solve any problem, then you can take help from our Blog/website.
Use “Ctrl+F” To Find Any Questions Answer. & For Mobile User, You Just Need To Click On Three dots In Your Browser & You Will Get A “Find” Option There. Use These Option to Get Any Random Questions Answer.
Introduction To Algorithm
The word Algorithm means “a process or set of rules to be followed in calculations or other problem-solving operations”. Therefore Algorithm refers to a set of rules/instructions that step-by-step define how a work is to be executed upon in order to get the expected results.
Advantages of Algorithms:
- It is easy to understand.
- Algorithm is a step-wise representation of a solution to a given problem.
- In Algorithm the problem is broken down into smaller pieces or steps hence, it is easier for the programmer to convert it into an actual program.
Link for the Problem – Picking Numbers – Hacker Rank Solution
Picking Numbers – Hacker Rank Solution
Problem:
Given an array of integers, find the longest subarray where the absolute difference between any two elements is less than or equal to .
Example
![Picking Numbers in Algorithm | HackerRank Programming Solutions | HackerRank Problem Solving Solutions in Java [💯Correct] 2 image 70](https://technorj.com/wp-content/uploads/2021/12/image-70.png)
Function Description
Complete the pickingNumbers function in the editor below.
pickingNumbers has the following parameter(s):
- int a[n]: an array of integers
Returns
- int: the length of the longest subarray that meets the criterion
Input Format
The first line contains a single integer n, the size of the array a.
The second line contains n space-separated integers, each an a[i].
![Picking Numbers in Algorithm | HackerRank Programming Solutions | HackerRank Problem Solving Solutions in Java [💯Correct] 3 image 71](https://technorj.com/wp-content/uploads/2021/12/image-71.png)
Sample Input 0
6 4 6 5 3 3 1
Sample Output 0
3
Explanation 0
![Picking Numbers in Algorithm | HackerRank Programming Solutions | HackerRank Problem Solving Solutions in Java [💯Correct] 4 image 72](https://technorj.com/wp-content/uploads/2021/12/image-72.png)
Sample Input 1
6 1 2 2 3 1 2
Sample Output 1
5
Explanation 1
![Picking Numbers in Algorithm | HackerRank Programming Solutions | HackerRank Problem Solving Solutions in Java [💯Correct] 5 image 73](https://technorj.com/wp-content/uploads/2021/12/image-73.png)
Picking Numbers – Hacker Rank Solution
import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.Scanner; import java.util.Set; /** * @author Techno-RJ * */ public class PickingNumbers { public static int pickingNumbers(List<Integer> a) { int frequency[] = new int[101]; int result = Integer.MIN_VALUE; for (int i = 0; i < a.size(); i++) { int index=a.get(i); frequency[index]++; //frequency[index]=frequency[index]+1 } for (int i = 1; i <= 100; i++) { result = Math.max(result, frequency[i] + frequency[i - 1]); } return result; } public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); List<Integer> list = new ArrayList<>(); while (n-- > 0) { list.add(sc.nextInt()); } int result = pickingNumbers(list); System.out.println(result); sc.close(); } }