Array
- 25 October
Find Smallest and Largest Element in an Array in Java
If you want to practice data structure and algorithm programs, you can go through 100+ java coding interview questions. In this post, we will see how to find smallest and largest element in an array. 1. Introduction to Problem Let’s say we have an array of numbers. [crayon-6729d2a2be7f2380322070/] Our goal is to find out smallest and […]
- 15 October
Largest sum contiguous subarray
If you want to practice data structure and algorithm programs, you can go through 100+ java coding interview questions. Problem: From Wikipedia : In computer science, the Largest sum contiguous subarray is the task of finding the contiguous subarray within a one-dimensional array of numbers which has the largest sum. For example, for the sequence of […]
- 15 October
Kadane ‘s Algorithm in java
If you want to practice data structure and algorithm programs, you can go through Java coding interview questions. Kadane algorithm is a famous algorithm to solve maximum subarray problem. Maximum subArray problem: From Wikipedia : In computer science, the maximum subarray problem is the task of finding the contiguous subarray within a one-dimensional array of numbers […]
- 06 October
Find the Contiguous Subarray with Sum to a Given Value in an array
Problem : Given an array of positive integer and given value X, find Contiguous sub array whose sum is equal to X. For example: [crayon-6729d2a2beeb5434308778/] Solution: Solution 1: Check all sub arrays and if current sum is equal to X, return. This will require two loops and if currentSum is greater than X tben try […]
- 06 October
Separate 0s and 1s in an array
Problem : Given an array of 0’s and 1’s in random order , you need to separate 0’s and 1’s in an array. For example: [crayon-6729d2a2bf0d5241060192/] Solution : Solution 1: Count number of 0’s in the array. Lets say we get X 0’s Once we get the count, put X 0’s in the array and […]
- 06 October
find minimum element in a sorted and rotated array
If you want to practice data structure and algorithm programs, you can go through data structure and algorithm interview questions. In this post, we will see how to find minimum element in sorted and rotated array. Problem: You are given an sorted and rotated array as below: [crayon-6729d2a2bf2c1906799366/] If you note that array is sorted and […]
- 06 October
Maximum difference between two elements such that larger element appears after the smaller number
If you want to practice data structure and algorithm programs, you can go through 100+ java coding interview questions. Given array of integers, find Maximum difference between two elements such that larger element appears after the smaller number For example: [crayon-6729d2a2bf3f2430694682/] Algorithm: Lets say we have array arr[] of stock prices. We will track two variable :minElementTillNow and maxDifference. minElementTillNow will be […]
- 05 October
Separate odd and even numbers in an array
Problem : Given an array of integers, you need to segregate odd and even numbers in an array. Please note : Order of elements can be changed. For example: [crayon-6729d2a2bf55f241898474/] Algorithm: Lets say array is arr[] Initialise two index variable , left=0 and right=arr.length-1 Increment left variable until you get odd number Decrement right variable […]
- 05 October
Find leaders in an array
Problem: We need to print all the leaders present in the array. Element is the leader if it is greater than right side of elements. For example: [crayon-6729d2a2bf717643672870/] Solution: Solution 1: Use two loops. Outer loop to iterate over array elements and inner loop to check for right elements of the array. If current element […]