Implement atoi which converts a string to an integer. The function first discards as many whitespace characters as necessary until the first non-whitespace character is found. Then, starting from this character takes an optional initial plus or minus sign followed by as many numerical digits as possible, and interprets them as a numerical value. The…
Category: Programs
This is classic computer science right here. Programming without knowledge of algorithms is like carpentry with just one kind of saw: you can get the job done, but it’s going to take a lot longer.
You can look at an algorithm as “discipline”. When you learn to write them, what you’re doing is solving a problem with discipline; using structure, patterns, and logical steps.
Remove Nth Node From End of List
Given a linked list, remove the n-th node from the end of list and return its head. Example: Given linked list: 1->2->3->4->5 and n=2. After removing the second node from the end, the linked list becomes 1->2->3->5. Note: Given n will always be valid. Follow up: could you do this in one pass? Solution for…
Search in Rotated Sorted array
Suppose an array sorted in ascending order is rotated at home 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. you may assume no duplicate exists in the array. your algorithm’s runtime complexity must be…
Find First And Last Position of Element in sorted Array
given an array of integers nums sorted in ascending order, find the starting and ending position of a given target value. your algorithm’s runtime complexity must be in the order of 0(log n). if the target is not found in the array, return [-1,-1]. Example 1: input: nums = [5,7,7,8,8,10], target = 8 output: [3,4]…
3sum in Java
Given an array nums of n integers, are there elements a, b, c in nums such that a + b + c = 0? find all unique triplets in the array which gives the sum of zero. note: the solution set must not contain duplicate triplets. Example: given array nums = [-1, 0, 1, 2,-1,…
Median of Two Sorted Arrays
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 0(log (m+n)). you may assume nums1 and nums2 cannot be both empty. Example 1: nums1 = [1, 3] nums2 = [2] the median is 2.0 Example…
Regular Expression Matching Program in Java
A regular expression is a type of entity that represents a character pattern. Pattern matching and “search-and-replace” operations on text are performed using regular expressions. Given an input string ( s )and a pattern ( p ), implement regular expression matching with support for ‘.’ and ‘*’. ‘.’ matches any single character. ‘*’ matches zero…
Longest Substring Without Repeating Characters
Given a String, find the length of the longest substring without repeating characters. Example 1: Input: “abcabcb” output: 3 Explanation: The answer is “abc”, with the length of 3. Example 2: Input: “bbbbb” Output: 1 Explanation: The answer is “b”, with the length of 1. Example 3: Input: “pwwkew” Output: 3 Explanation: The answer is…
Count the number of co-prime pairs in an array
Program: Count the number of co-prime pairs in an array. (co-prime numbers means Any two numbers whose GCD is 1) Input: The first line contains an integer T, total number of elements. Then follow T elements. Output: Count the number of co-prime pairs in an array. Sample Input and Output: Input 1: 3 1 2…
HOW TO GET DISTINCT ELEMENTS FROM AN ARRAY BY AVOIDING DUPLICATE ELEMENTS?
Here we explain how to find distinct elements from an array by avoiding duplicate elements. In the below program we use array of integers int num[], in this array we found distinct elements. For finding the distinct elements we use the following two ways. Program: Find distinct elements from an array Output: following is the…
PROGRAM TO ADD TWO NUMBERS WITHOUT USING PLUS (‘+’) SIGN
In our daily life we add some numbers and for this addition we use “+” sign. I have a one question, Can it be possible without using plus sign we add numbers? the answer is Yes! we can add two numbers without using the plus (“+”) sign. In the below program we use two different…
Java String Methods with Examples
Java string methods with examples: There are different string methods in java we will use this methods for perform various operations on the string in java.Following are some Java String Methods with Examples. Program: Get character from a string In this program we use charAt() method to get or access the single character from a…
Basic operations on String in Java- Strings Concatenation, Compare
Following programs are for performing the strings basic operations in java like string concatenation, string compare. To perform the string concatenation, string compare in java uses the inbuilt methods in java. String Length: Program: Find the length of string Here we first create the string named str and use the length() method to get the…
Reverse a String
How to reverse the string? We will use the following different solutions for reversing the string. First, we get input from the user using a command line. In the first solution of this problem we buffer the input string using the StringBuffer(String string) method. Then reverse this buffer and after reversing convert the buffer into…
Linear Search
Linear Search is a used for finding the key element in the multiple of elements in a given array. Now a days, Linear search is not that much used because Linear search algorithm is slow as compare to binary search or hashing. Algorithm: First step is traverse the array Next is match the search element…
Selection Sort
Selection sort is a simple sorting algorithm. This sorting algorithm is an in-place comparison-based algorithm in which the list is divided into two parts, the sorted part at the left end and the unsorted part at the right end. Initially, the sorted part is empty and the unsorted part is the entire list. The smallest…
Quick Sort
Sort algorithms order the elements of an array according to a predefined order.Quick sort is a divide and conquer algorithm. In a divide and conquer sortingalgorithm the original data is separated into two parts “divide” which areindividually sorted and “conquered” and then combined. Algorithm: If the array contains only one element or zero elements than…
Merge Sort
There are different sorting algorithms are available like selection sort, insertion sort etc. In this article we learn about one of them sorting algorithm, working of this sorting algorithm and example of this sorting algorithm. Merge sort algorithm is the simplest sorting algorithm as compare to other sorting algorithms. The Merge sorting algorithm can be…
Insertion Sort
There are different sorting algorithms are available like selection sort, insertion sort etc. In this article we learn about one of them sorting algorithm. Also working of this sorting algorithm and example of this sorting algorithm. Insertion sorting algorithm is the simplest sorting algorithm as compare to other sorting algorithms. Insertion sorting is a simple…
Bubble sort
There are different sorting algorithms are available like selection sort, insertion sort etc. In this article we learn about one of them sorting algorithm, working of this sorting algorithm and example of this sorting algorithm. Bubble sort algorithm is the simplest sorting algorithm as compare to other sorting algorithms. In the bubble sorting algorithm, array…
Binary Search Program
There are two types of searching are possible. First is linear search and second is binary search. In this article we explain about the binary search program, if you want to learn about what is linear search and how it works? see this article Linear Search. The linear search and binary search are used to…
Swapping of Two Numbers
Here we will see the swapping of two numbers using two different methods. In first method we exchange the values using the temporary variables and in second method Swapping of numbers without temporary variable. Swapping of numbers is the way of exchanging the values in the variables. Suppose there are two variables a and b,…
Addition of Two Numbers
In this program we will see the addition of two numbers using two different methods. In first program we specify the values of two numbers in the program itself. And in second program take the both numbers entered by user and add. In third program we use add () function. Adding the numbers using java…