Tag: Arrays

  • Find the next highest permutation of an array

    Problem: Given an integer array , find the next highest permutation of the numbers. If the next highest permutation is not found return the smallest permutation of the given input. Permutation of a number is all possible combinations of the given numbers. For example, Given the array: [1,2,3] The possible permutations are [1,3,2] , [2,1,3],…

  • Find first missing positive integer in an array.

    Problem: Given an array of integers find the first missing positive integer . The first positive integer is 1. So if 1 is itself missing you need to return 1 else whatever number after 1 is missing that should be returned. For example, Given the array: [1,2,4,5] The output is 3 Given the array: [3,4,-1,1,5]…

  • Sort three colors in an array

    Given an array containing three different colors red ,white and blue denoted by numbers 0,1 and 2 sort the array such that red comes first followed by white followed by blue. Constraints: For example, Given the input: [2,0,1,0,2,1] The output is [0,0,1,1,2,2] Try out the solution here: https://leetcode.com/problems/sort-colors/ Solution: Approach 1 – Count the number…

  • Valid Sudoku

    Given a board of size 9 * 9 representing a sudoku game , find out if it is valid. It is valid if : For example, The below sudoku: represented by the array: Input: board = [[“5″,”3″,”.”,”.”,”7″,”.”,”.”,”.”,”.”] ,[“6″,”.”,”.”,”1″,”9″,”5″,”.”,”.”,”.”] ,[“.”,”9″,”8″,”.”,”.”,”.”,”.”,”6″,”.”] ,[“8″,”.”,”.”,”.”,”6″,”.”,”.”,”.”,”3″] ,[“4″,”.”,”.”,”8″,”.”,”3″,”.”,”.”,”1″] ,[“7″,”.”,”.”,”.”,”2″,”.”,”.”,”.”,”6″] ,[“.”,”6″,”.”,”.”,”.”,”.”,”2″,”8″,”.”] ,[“.”,”.”,”.”,”4″,”1″,”9″,”.”,”.”,”5″] ,[“.”,”.”,”.”,”.”,”8″,”.”,”.”,”7″,”9″]] is valid since it obeys all the three conditions. Try…

  • Merge Sorted Array

    Given two sorted arrays and their sizes , merge the two into the first sorted array. Extra space equal to the size of the second array is provided at the end of the first array. For example, Given the arrays: num1 = [1,2,3,0,0,0] , size = 3 ( remaining space for the second array to…

  • Plus One on an array

    Given an array representing a integer with each index representing each digit starting from the most significant digit to the least, add one to the integer and return the output as an array. For example, For the input [1,6,7], The output is [1,6,8] For the input [9,9], The output is [1,0,0]. Try out the solution…

  • Remove Duplicates from Array

    Given a sorted array (in ascending order) remove the duplicates from them . you should not use any extra memory. So to remove in place , you need to move all the unique elements to the left of the array. So if there are k unique elements in an array of length n, the first…

  • Data Structures and Algorithms – Arrays – Container with Most Water

    Problem: Given an array of heights representing the ends of a container , find the container with the maximum area. For example: Consider the below input: Input: height = [1,8,6,2,5,4,8,3,7] The height can be represented pictorially as below: Each line represents each height in the array in the order of the indices. You can join…

  • Data Structures and Algorithms in Java – Arrays – Three sum

    Problem: Given an array of integers , find out triplets in the array such that their sum is 0. Constraints: There should not be any duplicates. Same number should not be added to itself. Assumptions: The array contains a minimum of 3 elements and maximum 3000 elements Numbers can vary from -100000 to 100000 Input:…

  • Data Structures and Algorithms in Java – Arrays – Search in Rotated Sorted Array

    Problem: Given an array of integers which is sorted and rotated , find out if the given element is in the array or not. A rotated array is an array whose elements are shifted towards the beginning from the end. For example , consider the array: input = [1,2,3,4,5] If this array is rotated once…