Posts

Showing posts with the label Sort

Java Code to Partition an Array and return the maximized Sum of Pairs

Image
  Given an integer array  nums  of  2n  integers, group these integers into  n  pairs  (a 1 , b 1 ), (a 2 , b 2 ), ..., (a n , b n )  such that the sum of  min(a i , b i )  for all  i  is  maximized . Return  the maximized sum This is a leetcode problem , which we will try to solve today.  On analyzing the problem we can understand that we need to split or partition the array into pairs of 2, and then take the minimum value from a pair, such that the sum of these minimum values is maximum.  Ideally, the value can be maximum only if we are taking bigger numbers, but we have to take minimum values from a pair of Integers. So, to work around this problem, we will sort the array first. This way, when we make pairs, we can ensure that the minimum value of a pair can come out to be a bigger number. We will then take the sum and return the same.  /**   * Group an array of 2n integers , into n...

Java Program to Sort an Integer Array in either Ascending or Descending Order using Java For Loops and a Function Parameter

Image
  When you're learning Java to prepare for an Interview, Arrays is a really important concept. Sorting of an array using for loops is basic concept which everyone should understand.  We've written an article earlier to teach you how to use inbuilt Java functions to sort the characters of a string . But, if you're asked to write an algorithm yourself that can achieve this, you're at the right place! We'll show how to do the sorting, and for an icing on the cake, we'll also show how to control the sorting order i.e. ascending or descending! This makes our function reusable for both purposes. So, let's get started! In order to sort an Array, we'll have to traverse through it twice. We'll do it using for loops.  The first loop will traverse the total length of the array for ( int i = 0 ; i < a . length ; i + + ) And, the second for loop will be incrementally traversing the array for the sorting for ( int j = i + 1 ; j < a . length ; j + + ) See ...

Java Program to Sort Characters of a String

Image
  Sorting a String's characters is one of the most asked Interview Questions in Java. Today let's see how this can be solved in a really easy manner.  We will be utilizing an inbuilt java function in Java.Utils library -> Arrays.sort() We need to import Java.utils in order to use it.  import java . util . * ; A string is nothing but an array of characters. The function that we are going to use also needs an char array as an input. So, we'll be converting the String into a character array.  //Load the string into a character array. char [ ] temp = a . toCharArray ( ) ; Now we'll use the Arrays.sort function to do the heavy work of sorting for us.  //Sort the Array Arrays . sort ( temp ) ;  Let's combine all this information together for a working code.  You can copy paste this code into a new .java file and see it working for yourself! package coding ; import java . util . * ; public class Practice { public static void main ( Stri...