## Posts

Showing posts from September, 2019

### Find Number of Islands - The Coding Shala

Home >> Interview Questions >> Find number of Islands Find Number of Islands Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water. Example 1: Input: 11110 11010 11000 00000 Output: 1 Example 2: Input: 11000 11000 00100 00011 Output: 3 Number of Islands Java Program Method 1: Using dfs. An Island is started if there is 1 present and surrounded by water(0). we will count island if 1 available and make all the adjacent to zero(0) or sink adjacent island so we don't count them again. We can use dfs or bfs to check adjacent lands. The following code is using dfs. Java Code:  class Solution { public int numIslands ( char [][] grid ) { //check if

### Binary Search Algorithm - The Coding Shala Home >> Algorithms >> Binary Search Algorithm Binary Search Algorithm A binary search is an algorithm used to search a given element in a sequence. Binary search works on the sorted array. We used the left and right index in search called the search space. In binary search, we find mid of left and right and check if our given element is in the first half or second and according to that we set new left and right. By doing this every time we eliminate half of the sequence. let's take an example to implement the basic binary search. Example 1: Given a sorted (in ascending order) integer array nums of n elements and a target value, write a function to search target in nums. If target exists, then return its index, otherwise, return -1. Input: nums = [-1,0,3,5,9,12], target = 9 Output: 4 Explanation: 9 exists in nums and its index is 4 Input: nums = [-1,0,3,5,9,12], target = 2 Output: -1 Explanation: 2 does not exist in n

### Time Complexity, Space Complexity, Asymptotic Notations - The Coding Shala Home >> Algorithms >> Time and space complexity Time Complexity, Space Complexity, and Asymptotic Notations Time complexity describes the time taken by an algorithm and space complexity describes the memory used by an algorithm. Asymptotic Notations are languages that allow us to calculate time complexity and space complexity. Big O is most commonly used for time complexity or analysis of algorithms. Time Complexity In Computer Science, the time complexity of an algorithm quantifies the amount of time taken by an algorithm to run as a function of the length of the string representing the input. The time complexity of an algorithm is commonly expressed using big O notation, which excludes coefficients and lowers order terms.[From Wikipedia] In a simple way we can say how much time our code takes to execute.  So, How to calculate the running time of an algorithm? We can have three cases to analyze an algorithm: Worst Case Average Case Best Case

### Median of Two Sorted Arrays Java Program - The Coding Shala Home >> Interview Questions >> Median of two sorted arrays Median of Two Sorted Arrays Java Program There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)). You may assume nums1 and nums2 cannot be both empty. Example 1: nums1 = [1,2] nums2 = [3,4] The median is 2.5. Example 2 nums1 =  nums2 = [-1, -2] The median is -2. Median of Two Sorted Arrays Java Program Before solving this we'll see what is median and how to find it. So basically a median is the value present at the center of a sorted array. Now the length of the array can be odd or even.  let's solve the median of two sorted arrays. Method 1: We have two sorted arrays, now if we merge both arrays into one(still sorted) then we can easily find the center of the merged array, But this method takes

### Shell Script To Find Out Biggest Number Form Given Three Numbers - The Coding Shala

Home >> Scripting >> Biggest number Shell Script To Find Out Biggest Number Form Given Three Numbers Write a shell script to find out the biggest number form given three numbers. Numbers are supplied by the command line argument? Code:  #! / bin / bash if [ \$# - ne 3 ] then echo "enter three argument" exit 1 fi if [ \$ 1 - gt \$ 2 ] && [ \$ 1 - gt \$ 3 ] then echo "\$1 is biggest number" elif [ \$ 2 - gt \$ 1 ] && [ \$ 2 - gt \$ 3 ] then echo "\$2 is the biggest number" elif [ \$ 3 - gt \$ 1 ] && [ \$ 3 - gt \$ 2 ] then echo "\$3 is biggest number" else echo "all numbers are equal" fi Other Posts You May Like Shell script to print sum of all digits Shell script to add two numbers Shell script to find factorial Shell script to generate Fibonacci series Shell script to create a simple calculator Please leave a comment below if you like this post

### Shell Script to Print Numbers in Descending Order - The Coding Shala

Home >> Scripting >> Descending order of a number Shell Script to Print Numbers in Descending Order Write a shell script to print numbers in descending order using a while loop? Code:  #! / bin / bash i = 5 while test \$ i != 0 do echo "\$i" i =`expr \$ i - 1 ` done Other Posts You May Like Shell script to print sum of all digits Shell Script to add two numbers Shell Script to find a factorial Shell script to print a number in reverse order Shell script to create a simple calculator Please leave a comment below if you like this post or found some error, it will help me to improve my content.

### Shell Script to Create a Simple Calculator - The Coding Shala

Home >> Scripting >> simple calculator Shell Script to Create a Simple Calculator Write a shell script to create a simple calculator using a switch-case statement? Code:  #! / bin / bash echo "simple calculator" sum = 0 i = "y" echo "enter first number" read n1 echo "enter second number" read n2 while [ \$ i = "y" ] do echo "1.Addition" echo "2.Subtraction" echo "3.Multiplication" echo "4.Division" echo "Enter choice" read ch case \$ch in 1 ) sum =\$(echo " \$n1 + \$n2" | bc - l) echo "Addition is =" \$ sum ;; 2 ) sum =\$(echo "\$n1 - \$n2" | bc - l) echo "Sub is =" \$ sum ;; 3 ) sum =\$(echo "\$n1 * \$n2" | bc - l) echo "Mul is =" \$ sum ;; 4 ) sum =\$(echo "\$n1 / \$n2" | bc - l) echo "div is =" \$ sum ;; * )echo "invalid choice" esac echo "Do y