Posts

LeetCode - Search in Rotated Sorted Array - The Coding Shala

Home >> Interview Questions >> search in rotated sorted array LeetCode - Search in Rotated Sorted Array In this post, you will learn how to search an element in a rotated sorted array and returns its index. We will use the binary search algorithm here. Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand  (i.e., [0,1,2,4,5,6,7] might become [4,5,6,7,0,1,2]).  You are given a target value to search. If found in the array return its index, otherwise return -1.  Example 1: Input: nums = [4,5,6,7,0,1,2], target = 0 Output: 4 Example 2: Input: nums = [4,5,6,7,0,1,2], target = 3 Output: -1 Search in Rotated Sorted Array Java We can search in the array using the loop and return if the target is found or not but this would take O(n) time. this is not a good idea here. Instead of using normal search we will use Binary Search Algorithm. Java Program:  class Solution { publ...

Implement Sqrt function using binary search - The Coding Shala

Home >> Interview Questions >> Implement sqrt() Implement Sqrt function using binary search In this post, you will learn how to find the square root of a given number using the binary search algorithm. In java, the sqrt() function is already available to find square root for a number but in this post, we are going to implement sqrt() function using binary search. Compute and return the square root of x, where x is guaranteed to be a non-negative integer.  Since the return type is an integer, the decimal digits are truncated and only the integer part of the result is returned. Example 1: Input: 4 Output: 2 Example 2: Input: 8 Output: 2 Explanation: The square root of 8 is 2.82842..., and since  the decimal part is truncated, 2 is returned. Sqrt function in Java We can do this using a binary search. If we check one by one until reaching the target we might get time limit error for large numbers. Java Program:  class ...

First Unique Character in a String Java - The Coding Shala

Home >> Interview Questions >> first unique character in a string First Unique Character in a String In this post, you will learn how to find the first unique character in a given string in Java. Given a string, find the first non-repeating character in it and return its index. If it doesn't exist, return -1. Examples: s = "leetcode" return 0. s = "loveleetcode", return 2. First Unique Character in a String Solution Approach 1: We can use HashMap. We Count every character's frequency and store it as value. Again check with string's char and if the value in the map is one return its index. Java Program:  class Solution { public int firstUniqChar ( String s ) { Map < Character , Integer > map = new HashMap < Character , Integer >(); for ( int i = 0 ; i < s . length (); i ++){ char ch = s . charAt ( i ); i...

Circular Queue Data Structure - The Coding Shala

Home >> Data Structures >> Circular Queue Circular Queue Data Structure In this post, you will learn what is Circular Queue in Data Structure and how to implement a circular queue in Java using an array. The circular queue is a linear data structure in which the operations are performed based on FIFO(first in First Out) principle and the last position is connected back to the first position to make a circle. It is also called the Ring Buffer. Why we need a Circular Queue? We know that in the simple queue if we remove an element from the head and we are not going to use that space again so it will be a complete waste. To prevent this we used a circular queue. For a circular queue, we may use a fixed-sized array and two pointers to indicate the starting position and the ending position. Circular Queue Java Program The following Java program explains the Circular queue. Here, i have implemented circular queue using an array. Java Program:  ...

LeetCode - Contains Duplicate Java - The Coding Shala

Home >> LeetCode >> Contains Duplicate LeetCode Contains Duplicate Problem In this post, you will learn how to check if an array contains duplicate values in Java. Given an array of integers, find if the array contains any duplicates.  Your function should return true if any value appears at least twice in the array, and it should return false if every element is distinct. Example 1: Input: [1,2,3,1] Output: true Example 2: Input: [1,2,3,4] Output: false Example 3: Input: [1,1,1,3,3,4,3,2,4,2] Output: true Practice on LeetCode - Click Here Contains Duplicate Java Solution Approach: We can sort the array and check with the next element but this approach will take O(n logn) time. We can do using HashSet within O(n) time.  Java Program:  class Solution { public boolean containsDuplicate ( int [] nums ) { Set < Integer > set = new HashSet ...

Fibonacci Series Java Program - The Coding Shala

Home >> Programming Questions >> Fibonacci Series Fibonacci Series Java Program In this post, you will learn how to write a Java program to print the Fibonacci Series or Fibonacci Sequence. The Fibonacci numbers, commonly denoted F(n) form a sequence, called the Fibonacci sequence, such that each number is the sum of the two preceding ones, starting from 0 and 1. That is,  F(0) = 0,   F(1) = 1 F(N) = F(N - 1) + F(N - 2), for N > 1. Given N, calculate F(N). The Fibonacci series starts with 0 and the second number is 1. In the Fibonacci series, the third number is the sum of the first and second numbers. We can print the nth number in the Fibonacci series by adding the previous two numbers of the Fibonacci series. Example 1: Input: 2 Output: 1 Explanation: F(2) = F(1) + F(0) = 1 + 0 = 1. Example 2: Input: 3 Output: 2 Explanation: F(3) = F(2) + F(1) = 1 + 1 = 2. Fibonacci Number Java Program A...

LeetCode - Climbing Stairs Java - The Coding Shala

Home >> LeetCode >> Climbing Stairs LeetCode Climbing Stairs Problem In this post, we will see how to solve the leetcode climbing stairs problem in Java. You are climbing a stair-case. It takes n steps to reach to the top.  Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?  Note: Given n will be a positive integer. Example 1: Input: 2 Output: 2 Explanation: There are two ways to climb to the top. 1. 1 step + 1 step 2. 2 steps Example 2: Input: 3 Output: 3 Explanation: There are three ways to climb to the top. 1. 1 step + 1 step + 1 step 2. 1 step + 2 steps 3. 2 steps + 1 step Practice on Leetcode Click Here Climbing Stairs Java Solution Approach 1: Using dynamic programming. Java Program:  class Solution { public int climbStairs ( int n ) { if ( n <= 2 ) return n ; int [] dp = new ...

LeetCode - Jewels and Stones Java Solution - The Coding Shala

Home >> LeetCode >> Jewels and Stones LeetCode - Jewels and Stones Java Solution In this post, we will see leetcode jewels and stones problem's solution in Java. You're given strings J representing the types of stones that are jewels, and S representing the stones you have.  Each character in S is a type of stone you have.  You want to know how many of the stones you have are also jewels.  The letters in J are guaranteed distinct, and all characters in J and S are letters. Letters are case sensitive, so "a" is considered a different type of stone from "A". Example 1: Input: J = "aA", S = "aAAbbbb" Output: 3 Example 2: Input: J = "z", S = "ZZ" Output: 0 Solve Problem Here: Click Here Jewels and Stones problem java program Approach 1: We can use Set to store character and then we match with stones. Java Program:  class Solution { ...