Category Archives: Algorithms

Program to find remainder without using modulo or % operator

Given two numbers ‘num’ and ‘divisor’, find remainder when ‘num’ is divided by ‘divisor’. The use of modulo or % operator is not allowed. Examples… Read More »

Mobile Numeric Keypad Problem

Given the mobile numeric keypad. You can only press buttons that are up, left, right or down to the current button. You are not allowed… Read More »

Analysis of Algorithm | Set 5 (Amortized Analysis Introduction)

Amortized Analysis is used for algorithms where an occasional operation is very slow, but most of the other operations are faster. In Amortized Analysis, we… Read More »

LinkedIn Interview Experience | Set 1 (for SDE Internship)

Below is my interview experience with linkedin India for software development intern. First there was an online round on hackerrank where they asked 4 simple… Read More »

Find common elements in three sorted arrays

Given three arrays sorted in non-decreasing order, print all common elements in these arrays. Examples: Input: ar1[] = {1, 5, 10, 20, 40, 80} ar2[]… Read More »

Print All Distinct Elements of a given integer array

Approach: 1. Put all input integers to hashmap’s key 2. Print keySet outside the loop Java filter_none edit close play_arrow link brightness_4 code import java.util.HashMap;… Read More »

Find the first repeating element in an array of integers

Given an array of integers, find the first repeating element in it. We need to find the element that occurs more than once and whose… Read More »

Efficient Program to Compute Sum of Series 1/1! + 1/2! + 1/3! + 1/4! + .. + 1/n!

Given a positive integer n, write a function to compute sum of the series 1/1! + 1/2! + .. + 1/n! A Simple Solution solution… Read More »

Print first k digits of 1/n where n is a positive integer

Given a positive integer n, print first k digits after point in value of 1/n. Your program should avoid overflow and floating point arithmetic. Examples… Read More »

Pattern Searching using a Trie of all Suffixes

Problem Statement: Given a text txt[0..n-1] and a pattern pat[0..m-1], write a function search(char pat[], char txt[]) that prints all occurrences of pat[] in txt[].… Read More »

Find the number of valid parentheses expressions of given length

Given a number n find the number of valid parentheses expressions of that length. Examples : Input: 2 Output: 1 There is only possible valid… Read More »

Print squares of first n natural numbers without using *, / and –

Given a natural number ‘n’, print squares of first n natural numbers without using *, / and -. Examples : Input: n = 5 Output:… Read More »

Shortest path with exactly k edges in a directed and weighted graph

Given a directed and two vertices ‘u’ and ‘v’ in it, find shortest path from ‘u’ to ‘v’ with exactly k edges on the path.… Read More »

Connect n ropes with minimum cost

There are given n ropes of different lengths, we need to connect these ropes into one rope. The cost to connect two ropes is equal… Read More »

Generate all binary permutations such that there are more or equal 1’s than 0’s before every point in all permutations

Generate all permutations of given length such that every permutation has more or equal 1’s than 0’s in all prefixes of the permutation. Examples: Input:… Read More »