Skip to main content

Posts

Showing posts from April, 2021

Sort Array By Parity in Java | Programming Tutorial | LeetCode Problem

Sort Array as Odd numbers followed by Even numbers in Java | Sort array as store All even numbers first and all Odd numbers last. Given an array of non-negative integers, return an array consisting of all the even elements of array followed by all the odd elements of array. You may return any answer array that satisfies this condition. Example 1 :- Input: [3,1,2,4] Output: [2,4,3,1] The outputs [4,2,3,1], [2,4,1,3], and [4,2,1,3] would also be accepted. See description in Leetcode :- Sort Array By Parity   So lets see solution of above problem. Solution 1 : In place with For loop class Solution {     public int[] sortArrayByParity(int[] array) {         if (array.length > 1) {             int rightIndex = array.length - 1;             for (int leftIndex = 0; leftIndex < array.length;) {                                  if (array[leftIndex] % 2 != 0 && array[rightIndex] % 2 == 0) {                     int temp = array[leftIndex];                     array[leftIndex] = ar

Move Zeroes to end of Array in Java | LeetCode problem

Java code for Move All Zeroes to end of given Array without creating new Array with explanation Problem Description :- Given an integer array, move all 0's to the end of it while maintaining the relative order of the non-zero elements. Note that you must do this in-place without making a copy of the array. Example 1 :- Input: nums = [0, 1, 0, 3, 12] Output: [1, 3, 12, 0, 0] Example 2 :- Input: nums = [1, 0, 0, 5, 6, 9, 0] Output: [1, 5, 6, 9, 0, 0, 0] Solution 1 class Solution {          public void moveZeroes(int[] array) {                  // Traverse array from left to right         for (int i = 0; i < array.length-1; i++) {                         // Check if current element is 0 or not.             if (array[i] == 0) {                                 // If current element is 0 then again traverse array                 // From i+1 to last array element                 for (int j = i+1; j < array.length; j++) {                     // If next element is non 0,       

Replace Elements with Greatest Element on Right Side in Java

Replace Elements with Greatest Element on Right Side Leetcode Solution in Java Problem Description :- Given an array arr , replace every element in that array with the greatest element among the elements to its right, and replace the last element with -1 . After doing so, return the array. Example 1: Input: arr = [17,18,5,4,6,1] Output: [18,6,6,6,1,-1] Explanation: - index 0 --> the greatest element to the right of index 0 is index 1 (18). - index 1 --> the greatest element to the right of index 1 is index 4 (6). - index 2 --> the greatest element to the right of index 2 is index 4 (6). - index 3 --> the greatest element to the right of index 3 is index 4 (6). - index 4 --> the greatest element to the right of index 4 is index 5 (1). - index 5 --> there are no elements to the right of index 5, so we put -1. Example 2: Input: arr = [400] Output: [-1] Explanation: There are no elements to the right of index 0. So, lets see solution of above problem. Solution

Valid Mountain Array Leetcode solution in Java | Programming Tutorial

Valid Mountain Array Java Solution with Explanation Problem Description :- Given an array of integers arr , return true if and only if it is a valid mountain array . See full description on LeetCode website :- Valid Mountain Array   Lets see example of input and its output :- Example 1 :- Input : arr = [1, 2, 3, 4, 3]; Output : true Example 2 :- Input : arr = [1, 2, 3, 4, 5, 6]; Output : false Example 3 :- Input : arr = [0, 2, 3, 4, 5, 6, 5, 5, 4, 3]; Output : false Example 4 :- Input : arr = [0, 3, 2, 1]; Output : true Lest go for solution :- Solution 1 class Solution {          public boolean validMountainArray(int[] array) {         int up = 0;         int down = 0;         if (array.length >= 3) {                          for (int i=0; i<array.length-1; i++) {                 if (array[i] == array[i+1]){                     return false;                 }                 if (down == 0 && array[i+1] > array[i]) {                     up = 1;                 } else i

Check If N and Its Double Exist Leetcode solution in Java

Problem Description Given an array arr of integers, check if there exists two integers N and M such that N is the double of M ( i.e N = 2* M). Example 1 :- Input: arr = [10,2,5,3] Output: true Explanation: N = 10 is the double of M = 5,that is, 10 = 2 * 5. Example 2 :- Input: arr = [7,1,14,11] Output: true Explanation: N = 14 is the double of M = 7,that is, 14 = 2 * 7. Example 3 :- Input: arr = [3,1,7,11] Output: false Explanation: In this case does not exist N and M, such that N = 2 * M. See full problem description on Leetcode :- Check If N and Its Double Exist Solution 1 :- Using For loop class Solution {          public boolean checkIfExist(int[] arr) {                   // Loop through array         for (int i = 0; i < arr.length; i++) {             for (int j = 0; j < arr.length; j++) {                       // Check if i and j is not same and N * 2 = M                          if (i != j && arr[i] * 2 == arr[j]) {                     return true;                 }

Remove Duplicates from Sorted Array LeetCode | Java Solution

Remove Duplicates from Sorted Array LeetCode Solution in Java Given a sorted array nums , remove the duplicates in-place such that each element appears only once and returns 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. Read full description in LeetCode :- Problem Description So lets see solution. Solution 1 :- class Solution { public int removeDuplicates(int[] nums) { int temp = 0; for (int i = 1; i<nums.length; i++) { if (nums[i] != nums[temp]) { temp++; nums[temp] = nums[i];      } } return temp+1; } } Input and Output :- Input: nums = [1,1,2] Output: 2, nums = [1,2] Explanation: Your function should return length = 2, with the first two elements of nums being 1 and 2 respectively. It doesn't matter what you leave beyond the returned length. Input: nums = [0,0,1,1,1,2,2,3,3,4] Output: 5, nums = [0,1,2,3,4] Explanation: Your function should