Author: Java2blog
10 OctoberConvert sorted array to balanced binary search tree
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 convert sorted array to balanced binary search tree. Algorithm: You might know that inorder traversal of binary search tree results in sorted array. We will use this property […]
06 OctoberFind 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: arr[]={14, 12, 70, 15, 99, 65, 21, 90}; X =97. Sum found between index 1 to 3 Elements are 12, 17 and 15 Solution: Solution 1: Check all sub arrays and […]
06 OctoberSeparate 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: arr[] = {0,1,0,0,1,1,1,0,1} Array after separating odd and even numbers : {0,0,0,0,1,1,1,1,1} Solution : Solution 1: Count number of 0’s in the array. Lets say we get X 0’s Once […]
06 Octoberfind 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: int arr[]={16,19,21,25,3,5,8,10}; If you note that array is sorted […]
06 OctoberMaximum 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: int arr[]={14, 12, 70, 15, 95, 65, 22, 30}; Max Difference =95-12 = 83 Algorithm: Lets say […]
05 OctoberSeparate 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: arr[] = {12, 17, 70, 15, 22, 65, 21, 90} Array after separating odd and even numbers : {12, 90, 70, 22, 15, 65, 21, 17} […]
05 OctoberFind 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: arr[]={14, 12, 70, 15, 99, 65, 21, 90} Here 99 and 90 are leader elements Solution: Solution 1: Use two loops. Outer loop to iterate over array elements […]
03 OctoberStock Buy Sell to Maximize Profit
If you want to practice data structure and algorithm programs, you can go through 100+ java coding interview questions. [toc] Given an array of integers representing stock price on a single day, find max profit that can be earned by 1 transaction. So you need to find a pair (buyDay,sellDay) where buyDay < = sellDay and […]
03 OctoberSearch in a row wise and column wise sorted matrix
If you want to practice data structure and algorithm programs, you can go through data structure and algorithm interview questions. If you want to practice data structure and algorithm programs, you can go through data structure and algorithm interview questions. Problem : Given row wise and column wise sorted matrix ,we need to search element with minimum […]