Skip to main content

Posts

Showing posts from March, 2021

Remove Element from Array LeetCode solution in Java | Programming blog

Given an array nums and a value val , remove all instances of that value in-place and return the new length. Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory. The order of elements can be changed. It doesn't matter what you leave beyond the new length. See full problem description on LeetCode :- Deleting items from an array   Lets see solutions :- Solution 1 :- class Solution { public int removeElement(int[] nums, int val) { int temp = 0; for (int i = 0; i < nums.length; i++) { if (nums[i] != val) { nums[temp] = nums[i]; temp++; } } return temp; } } Input and Output :- Input nums = [3,2,2,3], val = 3 Output 2, nums = [2,2] ___________________ Input nums = [0,1,2,2,3,0,4,2], val = 2 Output 5, nums = [0,1,4,0,3] Explanation :- We have to remove given value from array. Initialize any temporary variable with 0. Loop through given array n

Java Sort Hackerrank Solution | Comparator with Three Properties

Sort Java List using Comparator Interface for Three Object Values Problem Description :- You are given a list of student information: ID, FirstName, and CGPA. Your task is to rearrange them according to their CGPA in decreasing order. If two student have the same CGPA, then arrange them according to their first name in alphabetical order. If those two students also have the same first name, then order them according to their ID. No two students have the same ID. Hint : You can use comparators to sort a list of objects. See the oracle docs to learn about comparators. Input Format The first line of input contains an integer N, representing the total number of students. The next N lines contains a list of student information in the following structure:      ID  Name CGPA Read full description on HackerRank :- https://www.hackerrank.com/challenges/java-sort/problem Solution 1 :- Sort Java list using Inner class (Comparator) with Three Object properties In this problem we are using

Java Comparator Hackerrank Solution with Explanation | Programming Blog

Comparing Objects with Comparator in Java | Compare and Sort two Objects in Java Comparators are used to compare two objects. In this challenge, you'll create a comparator and use it to sort an array. The Player class is provided for you in your editor. It has 2 fields : a name string and score integer. Given an array of n Player objects, write a comparator that sorts them in order of decreasing score; if 2 or more players have the same score, sort those players alphabetically by name. To do this, you must create a Checker class that implements the Comparator interface, then write an int compare(Player a, Player b) method implementing the Comparator.compare(T o1, T o2) method. Input Format Input from stdin is handled by the locked stub code in the Solution class. The first line contains an integer, n, denoting the number of players. Each of the n subsequent lines contains a player's name and score, respectively. Read full description on Hackerrank Website :- https://www.

Modify property value of the objects in list using Java 8 streams

In this tutorial, we will see how to change object property one by one using stream api that is provided in Java 8. So for that first we need to create POJO class. So lets create one User class and add two properties Name and  Salary User.java package Java8; public class User {          private String name;     private Integer salary;          public User(String name, Integer salary) {         this.name = name;         this.salary = salary;     }     public String getName() {         return name;     }     public void setName(String name) {         this.name = name;     }     public Integer getSalary() {         return salary;     }     public void setSalary(Integer salary) {         this.salary = salary;     }     @Override     public String toString() {         return "User [name = " + name + ", salary = " + salary + "]";     }      } Add getter, setter and toString method in User class. Now we creating another java class where we can perform our modify

Java 1D Array (Part 2) HackerRank Soution with Explanation | Programming Tutorial

Problem Description :- Let's play a game on an array! You're standing at index 0 of an n-element array names game. From some index i (Where 0 <= i < n), you can perform one of the following moves: Move Backward: If cell i-1 exists and contains a 0, you can walk back to cell i-1. Move Forward:   If cell i + 1 contains a zero, you can walk to cell i + 1. If cell i + leap contains a zero, you can jump to cell i + leap. If you're standing in cell n - 1 or the value of i + leap >= n, you can walk or jump off the end of the array and win the game. In other words, you can move from index i to index i + 1, i - 1 or i + leap as long as the destination index is a cell containing a 0, If the destination index is greater than n - 1, you win the game. Given leap and game, complete the function in the editor below so that it returns true if you can win the game (or false if you cannot). Read Description on HackerRank :- https://www.hackerrank.com/challenges/java-1d-array/pro

Comparable Interface in Java with Examples | Programming Tutorial

How Comparable Interface works in Java? When to use Comparable Interface in Java with Examples?   In java, we often need to compare two values. We can easily compare primitive data type values like char, int, float, double using equals (= =), Less than (<) or Greater than (>) Operators.   But Comparing two objects, is bit different in Java. We can use Comparable or Comparator Interface for comparing two objects in Java. Based on our requirements we can choose Comparable or Comparator Interface.   Learn more about Comparator Interface and How to sort list using Comparator Interface with null values :- Comparator Interface in Java with Examples What is Comparable Interface in Java? Comparable interface in Java is used to compare two objects and sort them based on natural ordering. Comparable interface is known as natural ordering. In simple word, Comparable interface is used for sorting object according to the natural ordering. Lists and Arrays of objects that implement Comparable

Merge Two Sorted Arrays in Java with Explanation | LeetCode Solution

Merge Two Sorted Array LeetCode problem solution in Java with Explanation Problem Description :- Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. The number of elements initialized in nums1 and nums2 are m and n respectively. You may assume that nums1 has a size equal to m+n such that it has enough space to hold additional elements from nums2. Example 1 :- Input: nums1 = [1,2,3,0,0,0], m = 3, nums2 = [2,5,6], n = 3 Output: [1,2,2,3,5,6] Example 2 :- Input: nums1 = [1], m = 1, nums2 = [], n = 0 Output: [1] Lets see solution :- Solution 1 :- class Solution {     public void merge(int[] nums1, int m, int[] nums2, int n) {         while(m > 0 && n > 0){             if(nums1[m-1] > nums2[n-1]){                 nums1[m+n-1] = nums1[m-1];                 m--;             }else{                 nums1[m+n-1] = nums2[n-1];                 n--;             }         }         while(n > 0){             nums1[n+m-1] = nums2[n-1];  

Duplicate each occurrence of zeros in Array with Explanation | Java Solution

Duplicate each occurrence of zero, shifting the remaining elements to the right in Java | LeetCode Problem Problem Description :- Given a fixed length array of integers, duplicate each occurrence of zero, shifting the remaining elements to the right. Note that elements beyond the length of the original array are not written. Do the above modifications to the input array in place , do not return anything from your function. Example 1 :- Input: [1,0,2,3,0,4,5,0] Output: null Explanation: After calling your function, the input array is modified to:   [1,0,0,2,3,0,0,4] Example 2 :- Input: [1,2,3] Output: null Explanation: After calling your function, the input array is modified to: [1,2,3] Simply, if there is any 0 in array then we have to add another 0 in right of that array and shift all right value by 1. And array size remains same as old one. Solution 1 :- Using for loop class Solution {     public void duplicateZeros(int[] arr) {                  for (int i=0; i<arr.length; i