You are given an array of n integers and a target sum T. The goal is to determine whether or not there are two numbers x,y in A with x+y=T.
Example : Suppose we have an int array = {5, 3, 7, 0, 1, 4, 2} and T = 5. The unique pairs that sum up to 5 are (5, 0) (3, 2) and (1, 4).
There are three approaches to solve this problem - 1) brute force, 2) sort the array and use binary and search, and 3) Using the hashtable.
[Read More]
Printing "Hello World" to screen without using a single semicolon in C/C++
yes.it works fine.bt would you plz explain how? Unknown - Feb 1, 2014yes.it works fine.bt would you plz explain how?
Its because printf() returns the characters it prints. So if we do printf (“Hello World\n”) … it returns 10 (for helloworld) + 1 (for space) + 1 (for \n)=12.
Now, coming to main part, if(12) , will be executed as in C language, if(0) is like if (false), but if(any other integer) is true, so if statement is executed, meanwhile printing Hello World.
[Read More]
Printing "Hello World" to screen without using a single semicolon in C/C++
When I first encountered this question, I thought it was impossible. However, there are ways to do so. I don’t know if the solutions are exploitation of c / c++. Anyway, here are the most common solutions:
int main() { if (printf("Hello World \\n")) { } } Or you can use the while loop like this:
int main() { while (printf("Hello World") > 20) { } } ```If you don't think these codes will work, try them out.
[Read More]
Construct a full binary tree from a pre-order list of marked nodes
Question: construct a full binary tree from a list of nodes in pre-order. The nodes are marked ‘i’ if they are inner nodes and are marked ‘l’ if they are leaf nodes.
Example given a list of nodes marked as (i, i, i, l, l, i, l, l, i, l, l), we need to construct a tree from those nodes so that the result looks like this:
Notice how all “l” nodes are leaf nodes and “i” nodes are inner nodes?
[Read More]
Converting integers less than 100 to words
This is a classic problem in which we’re given an integer less than 100 and we have to output the words corresponding to the integer’s value. For instance, if input is 90, then the output is “ninety” Moreover, if the input is -99, then the output is “incorrect input”
To solve this problem, we should have arrays of unique names such as “one” and “thirteen”. The reason is that other integers’ names are made up of just those unique names.
[Read More]
Find all subsets of a given set OR Find power set of a given set
What is the complexity of this code?
Shams - Mar 4, 2012
What is the complexity of this code?
Sorry for the late response. The time complexity of method 2 will be O(n.2^n). I know it may not help now as I am over an year late, but still replying.
Find all subsets of a given set OR Find power set of a given set
Problem
Write a method that returns all subsets of a set.
Example
If we’re given a set of integers such that S = {1, 2, 3}, how can we find all the subsets of that set? For example, given S, the set of all subsets i.e. P(S) we get are {}, {1}, {2}, {3}, {1, 2}, {1, 3}, {2, 3}, and {1, 2, 3}.
Here is {} is empty set denoted by Φ.
[Read More]
Turning a bit off without affecting other bits
Hello and welcome to the second part of “Bit Manipulation Tips & Tricks.” If you haven’t read the first part yet, here is the link of you. Read it! :)
1. Turning a bit off without affecting other bits
Turning a bit off means to change 1 bits (on) to 0 bits (off) and if the bits are already off then they’ll stay off. This can be accomplished by using the ~, « and & operators.
[Read More]
Bit Manipulation Tips and Tricks
In this series, we’ll try to compile a list of tips and tricks that are useful for performing bitwise operation.
Multiply a number by a power of two using bit left shift
Divide a number by a power of two using bit right shift
Toggle a specified bit in a bit string without effecting other bits
Checking if a bit is on or off without affecting other bits
Turning a bit off without affecting other bits
[Read More]
Removing a loop in a singly linked list
Problem:
Remove a loop from a singly linked list if the list has a loop.
Example
For example, 1->2->3->4->1 (4 points to the head node 1) should become 1->2->3->4 while 1->2->3->4 should stay the same 1->2->3->4.
Solution The problem has 3 steps :
Detect if there is a loop in the list (already solved here) Identify the start of the loop (already discussed here) Delete the loop, which we will discuss here.
[Read More]