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 Lily’s Homework 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 – Lily’s Homework – Hacker Rank Solution
Lily's Homework – Hacker Rank Solution
Problem:
Whenever George asks Lily to hang out, she’s busy doing homework. George wants to help her finish it faster, but he’s in over his head! Can you help George understand Lily’s homework so she can hang out with him?
Consider an array of distinct integers, . George can swap any two elements of the array any number of times. An array is beautiful if the sum of among is minimal.
Given the array , determine and return the minimum number of swaps that should be performed in order to make the array beautiful.
Example
One minimal array is . To get there, George performed the following swaps:
Swap Result [7, 15, 12, 3] 3 7 [3, 15, 12, 7] 7 15 [3, 7, 12, 15]
It took swaps to make the array beautiful. This is minimal among the choices of beautiful arrays possible.
Function Description
Complete the lilysHomework function in the editor below.
lilysHomework has the following parameter(s):
- int arr[n]: an integer array
Returns
- int: the minimum number of swaps required
Input Format
The first line contains a single integer, , the number of elements in . The second line contains space-separated integers, .
Constraints
Sample Input
STDIN Function ----- -------- 4 arr[]size n = 4 2 5 3 1 arr = [2, 5, 3, 1]
Sample Output
2
Explanation
Define to be the beautiful reordering of . The sum of the absolute values of differences between its adjacent elements is minimal among all permutations and only two swaps ( with and then with ) were performed.
Lily's Homework – Hacker Rank Solution
import java.io.*; import java.math.*; import java.security.*; import java.text.*; import java.util.*; import java.util.concurrent.*; import java.util.function.*; import java.util.regex.*; import java.util.stream.*; import static java.util.stream.Collectors.joining; import static java.util.stream.Collectors.toList; import java.util.stream.IntStream; public class LilysHomework { private static final Scanner scn = new Scanner(System.in); private static void swap(long[] array, int index1, int index2) { long temp = array[index1]; array[index1] = array[index2]; array[index2] = temp; } private static int swaps(long[] unsortedValues) { int swaps = 0; Map<Long, Integer> locations = new HashMap<>(); for (int i = 0; i < unsortedValues.length; i++) { locations.put(unsortedValues[i], i); } long [] sortedValue = unsortedValues.clone(); Arrays.sort(sortedValue); for (int i = 0; i < sortedValue.length; i++) { if (sortedValue[i] != unsortedValues[i]) { swaps++; int swapIndex = locations.get(sortedValue[i]); locations.put(unsortedValues[i], swapIndex); swap(unsortedValues, i, swapIndex); } } return swaps; } public static void main(String[] args) { int numberOfElements = scn.nextInt(); long[] values = new long[numberOfElements]; for (int i = 0; i < numberOfElements; i++) { int value = scn.nextInt(); values[i] = value; } // When all you have is a hammer, everything begins to look like a nail. long [] reverseValue = IntStream.rangeClosed(1, values.length).mapToLong( i -> values[values.length - i]).toArray(); System.out.println(Math.min(swaps(values), swaps(reverseValue))); } }
I am not positive the place you’re getting your information, but great topic.
I must spend some time learning much more or figuring out
more. Thank you for wonderful information I was on the lookout for this information for my mission.