Given an array, count the number of pairs with a given sum.

Objective: Given an array of integers, write a program to count all the pairs with the given sum. Example: Given array: [1, 5, 7, 1, -1], Sum= 6 Total Pairs: 3 Note: pairs are (1, 5), (5, 1) and (7, -1) Given array: [4, 5, 1, 2, 9, -2, -4], Sum= 5 Total Pairs: 2 … Read more Given an array, count the number of pairs with a given sum.

Two Sum Problem

Objective: Given an array of integers, and k. Write a program to find indexes of two elements in an array which sum is equal to K. Example: Given array: [5, 4, 7, 3, 9, 2], Sum = 13 Output: Found indexes are: 4 and 1 Given array: [1, 2, 3, 4, 5], Sum = 9 … Read more Two Sum Problem

Given an array, find three-element sum closest to Zero

Objective: Given an array of integers, find the sum of any three elements which is closest to zero. The array may contain positive and negative elements.  Example:  Given Input: [-1, 4, -2, 5, 10, -5] Minimum Sum with three elements is: 1 Explanation:  -1, 4, -2 sums to -1  Given Input: [-1, 4, -2, 5, … Read more Given an array, find three-element sum closest to Zero

Find the sum of overlapping elements in two sets

Objective: Given two sets of integers, write an algorithm to print the sum of overlapping elements in two sets. Note: Elements in each set are unique or there are no duplicates within a set. Example: Set 1: [2, 3, 1, 6] Set 2: [4, 0, 6, 7] Sum of overlapping elements: 12 Explanation: Common element … Read more Find the sum of overlapping elements in two sets

Check if the given binary tree is Full or not.

Objective: Given a binary tree, write an algorithm to check if the tree is Full or not. Full binary tree:  A binary tree T is full if each node is either a leaf or possesses exactly two child nodes. See the example below Approach: Do postorder traversal. Check if the node is a leaf node … Read more Check if the given binary tree is Full or not.

Print Stack in reverse order.

Objective: Given a stack, write a program to print the stack elements in reverse order. Example: Approach: Use Temporary stack: Take temporary stack, and copy all the items from the given stack to a temporary stack. Elements will be stored in a temporary stack in reverse order. Now print elements from the temporary stack and … Read more Print Stack in reverse order.

Evaluation of Prefix Expressions (Polish Notation) | Set 1

Prefix notation is a notation for writing arithmetic expressions in which the operands appear after their operators. There are no precedence rules to learn. Let’s assume the below Operands are real numbers in real digits. (Later we will Enhance the solution for any number) Permitted operators: +,-, *, /, ^(exponentiation) Blanks are NOT permitted in … Read more Evaluation of Prefix Expressions (Polish Notation) | Set 1

Find the maximum number present in a String

Given a string with alphabets and numbers. Write an algorithm to find the maximum number in the string. If any number is greater than equal to Max_Value of integer then return the Max_Value. Example: Input: “abcd 45, 54 and 50 are the numbers” Output: Maximum Number- 54 Input: aabb123ccdefgh54319hij Output: 54319 Input: 213123123and544455523412423423424234234234 Output: 2147483647 … Read more Find the maximum number present in a String

Convert Prefix to Postfix Expression

Objective: Given a Prefix expression, write an algorithm to convert it into Postfix expression. Example: Input: Prefix expression:  + A B  Output: Postfix expression: A B + Input: Prefix expression:  *-A/BC-/AKL Output: Postfix expression: ABC/-AK/L-* Approach: Use Stacks Algorithm: Iterate the given expression from right to left, one character at a time If the character … Read more Convert Prefix to Postfix Expression

Convert Roman Number to Integer

Objective: Given a Roman number, write a program to convert it to Integer. Roman Number –   Letters used in Roman numerals and the corresponding numerical values are given in the table below. Rules:  Roman numerals are usually written in highest to lowest from left to right except for some special cases where left character … Read more Convert Roman Number to Integer

Convert Postfix to Prefix Expression

Objective: Given a Postfix expression, write an algorithm to convert it into prefix expression. Example: Input: Postfix expression:  A B +  Output: Prefix expression- + A B Input: Postfix expression:  ABC/-AK/L-* Output: Infix expression: *-A/BC-/AKL Approach: Use Stack Algorithm: Iterate the given expression from left to right, one character at a time If the character … Read more Convert Postfix to Prefix Expression

Convert Postfix to Infix Expression

Objective: Given a Postfix expression, write an algorithm to convert it into Infix expression. Example: Input: Postfix expression:  A B +  Output: Infix expression- (A + B) Input: Postfix expression:  ABC/-AK/L-* Output: Infix expression: ((A-(B/C))*((A/K)-L)) Approach: Use Stack Algorithm: Iterate the given expression from left to right, one character at a time If a character … Read more Convert Postfix to Infix Expression

String to Integer (AtoI – ASCII to Integer)

Objective: Implement atoi, which converts string to an integer. Rules for converting string to an integer. If the string is null or empty, Integer will be 0. The first character of string may represent the sign of the integer. ‘+’ or ‘-‘ Discard the white spaces if necessary. If any point, conversion of the string … Read more String to Integer (AtoI – ASCII to Integer)

Longest substring with at most two unique characters

Objective: Given a string, write an algorithm to find the longest substring with at most two characters. Example: Input: aabbccddc Output: ccddc, length: 5 Input: aabacbeeeebeaabb Output: beeeebe, length 7 Input: aaaaaa Output: Only one unique character is present in the input string Approach: Brute Force Identify all the substrings of the given input string … Read more Longest substring with at most two unique characters

Convert Prefix to Infix Expression

Objective: Given a Prefix expression, write an algorithm to convert it into Infix expression. Example: Input: Prefix expression: + A B Output: Infix expression- (A + B) Input: Prefix expression: *-A/BC-/AKL Output: Infix expression: ((A-(B/C))*((A/K)-L)) Approach: Use Stacks Algorithm: Iterate the given expression from right to left (in reverse order), one character at a time … Read more Convert Prefix to Infix Expression