# Category Archives: Algorithms

## Detect if two integers have opposite signs

Given two signed integers, write a function that returns true if the signs of given integers are different, otherwise false. For example, the function should… Read More »

## Maximum Length Chain of Pairs | DP-20

You are given n pairs of numbers. In every pair, the first number is always smaller than the second number. A pair (c, d) can… Read More »

## Word Wrap Problem | DP-19

Given a sequence of words, and a limit on the number of characters that can be put in one line (line width). Put line breaks… Read More »

## Generate integer from 1 to 7 with equal probability

Given a function foo() that returns integers from 1 to 5 with equal probability, write a function that returns integers from 1 to 7 with… Read More »

## DFA based division

Deterministic Finite Automaton (DFA) can be used to check whether a number “num” is divisible by “k” or not. If the number is not divisible,… Read More »

## Find day of the week for a given date

Write a function that calculates the day of the week for any particular date in the past or future. A typical application is to calculate… Read More »

## Sieve of Eratosthenes

Given a number n, print all primes smaller than or equal to n. It is also given that n is a small number. Example: Input… Read More »

## Find a pair with the given difference

Given an unsorted array and a number n, find if there exists a pair of elements in the array whose difference is n. Examples: Input:… Read More »

## Magic Square

A magic square of order n is an arrangement of n^2 numbers, usually distinct integers, in a square, such that the n numbers in all… Read More »

## Count numbers that don’t contain 3

Given a number n, write a function that returns count of numbers from 1 to n that don’t contain digit 3 in their decimal representation.… Read More »

## Check whether a given point lies inside a triangle or not

Given three corner points of a triangle, and one more point P. Write a function to check whether P lies within the triangle or not.… Read More »

## To find sum of two numbers without using any operator

Write a program to find sum of positive integers without using any operator. Only use of printf() is allowed. No other library function can be… Read More »

## Sudoku | Backtracking-7

Given a partially filled 9×9 2D array ‘grid’, the goal is to assign digits (from 1 to 9) to the empty cells so that every… Read More »

## Partition problem | DP-18

Partition problem is to determine whether a given set can be partitioned into two subsets such that the sum of elements in both subsets is… Read More »

## Count total set bits in all numbers from 1 to n

Given a positive integer n, count the total number of set bits in binary representation of all numbers from 1 to n. Examples: Input: n… Read More »