Skip to main content

Posts

Showing posts from April, 2022

How to Calculate median in Java? | With Array and List | Programming Blog

Find median of Java Array and List values using 2 ways What is Median? Median is mid value of particular data set. This data set must be sorted.  In simple word, Median is mid value of sorted data. Median will be middle, if total number of elements is odd or average of middle elements when total number of elements is even. Example : When total number of elements are odd 1, 2, 3, 4, 5, 6, 7 Median will be : 4 2, 3, 5, 7, 8 Median will be : 5 When total number of elements are even 1, 2, 3, 4 Median will be : (2+3) / 2 : 2.5 2, 4, 6, 8, 10, 20, 25, 30 Median will be : (8+10) / 2 : 9 So the formula become for our code : For odd elements : Median = (Total length / 2) For even elements : Median = (Total length / 2) + (Total length / 2 - 1) / 2 So lets jump on code. We will take elements form user. Method 1 : Using code logic In this approach, we does not use any library function for find median. Example 1 : Find median from Java array import java.util.Arrays; import java.util.Scanner; public

Grid Challenge HackerRank Solution in Java with Explanation

Java solution for Grid Challenge HackerRank problem  Problem description : Given a square grid of characters in the range ascii[a-z], rearrange elements of each row alphabetically, ascending. Determine if the columns are also in ascending alphabetical order, top to bottom. Return YES if they are or NO if they are not. Example : grid = ['abc', 'ade', 'efg'] The grid is illustrated below. a b c a d e e f g The rows are already in alphabetical order. The columns a a e , b d f and c e g are also in alphabetical order, so the answer would be YES . Only elements within the same row can be rearranged. They cannot be moved to a different row. In this problem, we have to print YES if it is possible to rearrange the grid alphabetically ascending in both its rows and columns, or NO otherwise. Input : grid = ['ebacd', 'fghij', 'olmkn', 'trpqs', 'xywuv'] The grid is illustrated below. e b a c d f g h i j o l m k n t r p q s

Counting Sort 1 HackerRank solution in Java with code explanation

Find occurrence of all elements from Java list and store into another List  Problem description : Given a list of integers, count and return the number of times each value appears as an array of integers. Input = [1, 1, 3, 2, 1] Output = [0, 3, 1, 1] In input, we have three occurrence of 1, one occurrence of 2 and one occurrence of 3. So in output put 3 at first index, 1 at second index and 1 at third index. Input = [2, 2, 1, 6, 5] Output = [0, 1, 2, 0, 0, 1, 1] In this problem, we does not have to write entire code of counting sort. we need only first step of counting sort algorithm. So lets jump on code. import java.io.*; import java.util.*; import java.util.stream.*; import static java.util.stream.Collectors.joining; import static java.util.stream.Collectors.toList; class Result {     public static List<Integer> countingSort(List<Integer> arr) {     // Creating 100 list size and initialize with 0     List<Integer> output = new ArrayList<>(Collections.nCopies(

Lonely Integer Hackerrank solution in Java | Find unique element in Java Array

Find unique elements from given Array in Java with Explanation | Lonely Integer Hackerrank solution  Problem Description : Given an array of integers, where all elements but one occur twice, find the unique element.  Example : array = [1, 2, 3, 4, 3, 2, 1] The unique element is 4. So we have given array, in which all elements occurs twice but 1 elements only occurs once and we have to find that unique element. Solution 1 : Find unique element from Java array      import   java . io .*;      import   java . util .*;      import   java . util . stream .*;      import   static   java . util . stream . Collectors . toList ;      class   Result   {          public   static   int   lonelyinteger ( List < Integer >   a )   {                           Map < Integer ,   Integer >   map   =   new   HashMap <> ();              for   ( Integer   elem   :   a )   {                  if   (! map . containsKey ( elem ))   {                      map . put ( elem ,   1 );