Posts

Showing posts with the label Arrays

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 code to find Count of Pairs in Integer Array with Absolute Difference K

Image
Let's solve a LeetCode problem using Java.  Count Number of Pairs With Absolute Difference K You can go through the problem and try to understand what needs to be done. Try to write your own code first. We have solved the problem and its here for your reference. /**  * Count Number of Pairs With Absolute Difference K  * @author computengine.com  * @param nums  * @param k  * @return  */ public int countKDifference ( int [ ] nums , int k ) { int count = 0 ; for ( int i = 0 ; i < nums . length ; i + + ) { for ( int j = i + 1 ; j < nums . length ; j + + ) { if ( Math . abs ( nums [ i ] - nums [ j ] ) = = k ) { count + + ; } } } return count ; } If you look closely, we have looped through the Integer Array, and compared two elements of the arrays and find the difference between them. Now the difference can be positive or negative but it has to be equal to the input K. So we'v...

Find the Longest Streak of 1's in a Binary Array using Java Code

Image
  You have been given an Integer Array which contains only 1s and 0s, and asked to find the longest running streak of 0 or 1 in the array. Sound familiar? Its a common interview question. Let's try to find a solution using Java.  We would required two counters here, 1 counter to keep track of the running count, and the second one to keep track of the maximum running streak found.  We'll loop through the array using a for loop.  Here is the code for your reference: /**  * Find the longest Streak of 1s  * @author computengine.com  * @param a  * @return the length of the longest streak  */ public int findLongestStreak ( int [ ] b ) { System . out . println ( " Array for processing: " + Arrays . toString ( b ) ) ; int count = 0 , max = 0 ; //loop through the array for ( int i = 0 ; i < b . length ; i + + ) { //increment count when 1 is found if ( b [ i ] = = 1 ) count + = 1 ; //if count is greater...

Using Java Hashmap to Find the Biggest Number in an Array which Occurs the Same times as the Number Itself

Image
  Today we'll touch-base on an Interview Question that involves multiple important Java concepts. You deal with Arrays, For loops, If statements, While Statement, Iterators, Java Collections and Function re-usability.  So, with one single question, your Interviewer can actually assess your expertise in all these areas! Let's check it out! Today we'll try to find the Biggest Number in an Array, which is occurring the Same number of times as the number itself.  So, just being the biggest number is not sufficient! If that was what is asked, you can find the max and/or min number of an array with this article. But this is not the question. In order to qualify for an answer, the number should occur the same number of times as the number itself and be the biggest such number. Quite complicated, we should say! If you follow our blog, we have already published one article on the usage of HashMaps to find if two strings are Anagrams . We'll reuse the Hashmap concept here to seg...

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 Find the Smallest and Largest number in an Array of Integers

Image
  Array operations on int arrays is a common Interview Question in Java. Finding the minimum number or the maximum number (smallest or largest number) in an array requires traversing the whole array and giving the output.  We'll try to solve both problems in this post. You can go through the logic and utilize it as per your requirement.  The logic is simple, we'll go through each element in a array, and compare it to a minimum or maximum number.  We've initialized the minimum/maximum number as the first element of the array. int min , max ; min = max = num [ 0 ] ; Now, if the next element in the array is lesser/greater than the stored minimum/maximum number then we'll change the mimimum/maximum number to match it.  Once we're done with the whole array, we get the minimum/maximum number of the array.  Below is the complete code for reference: /**   * Find the Largest and Smallest number in a Array of Integers   * @author computengine.com ...

Java Program to read Excel File and Load into Array

Image
Excel is one of the most important productivity tools used in offices. It is also a great organizational tool, and its really easy to create tables in excel to store different kinds of data.  When doing automation, excel is an important tool to store data for automation scripts, which makes reading Excel data using Java code an important Interview Question. So, today we'll learn how to read an excel sheet using Java, and return the contents as a String Array. The Array can then be utilized as per your requirement.  We are reading an xlsx file, the most recent and most popular excel format. We need to import the below classes: import java . io . File ; import java . io . FileInputStream ; import org . apache . poi . xssf . usermodel . XSSFCell ; import org . apache . poi . xssf . usermodel . XSSFRow ; import org . apache . poi . xssf . usermodel . XSSFSheet ; import org . apache . poi . xssf . usermodel . XSSFWorkbook ; java.io.File and java.io.FileInputStream will ...

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...