Posts

Showing posts from November, 2012

Map in C++ made easy

Hi everyone, lets move on to C++ for a while. This tutorial will help you in learning Map in C++. I have written in the simplest possible way. Before coming to C++, please refer my previous post about Map and Set in Java made easy. We won't be covering any implementation of Map in this post. MAP Similar to the maps in Java, map in C++ is an associative container that stores key and a mapped value. An associative containers refer to the group of class templates in the standard library of C++ programming language that implement ordered arrays. In map in C++, all the elements are ordered. The key values will be unique. Use Maps are mainly used, whenever we want to store particular occurrence of a variable. For e.g.: - Program to count the occurrence of all words in a file. Defining a Map Map in C++ is defined as follows: - map<string, int > map1; Instead of string and int, we can use other datatype as well.We should include the header file - #include<map>

Program to generate Excel Sheet Column Sequence

Excel Sheet Column sequence means a sequence like A,B,C..Z,AA,AB..ZZ,AAA...ZZZ..... . The program is very simple to code once we understood the logic. For converting a number to the corresponding string in this sequence, we need to know two things : - How many characters are there in the string. What is the corresponding value of each character. To find how many characters are there in the string, we should keep on diving the number/26 until it is greater than 0. To get the corresponding value of each character from last, we need to get number mod 26. One tricky thing about this algorithm is that, when we do the modulus of number corresponding to 'Z', we will get 26 mod 26 =0. We can add 26 to the modulus value if we get 0. Also in case of 'Z' , we need to subtract 1 from the result of number/26 so that our loop won't go for an extra iteration. For generating the Excel Sheet Column sequence to a given number, convert all the numbers from 1 to given numbe

Program to find whether a number is power of 2

This post will teach you how to find whether a number is power of 2. I am using 2 types of methods to solve this problem. Method 1 - checkPower If the number mod 2 not equal to 0, return false. Till number greater than 2, number = number/2 and check whether number mod 2 equal to 0 or not. If not equal to 0, return false. Return true at the end of loop. Time complexity of the program is O(log n). For better results, a new method is used. It is as follows. Method 2 - checkPower1 Find log(number)/log(2). If (2^ log(number)/log(2)) equals the given number, it is power of 2. Else not. Time complexity of the program is O(1). The program is as follows: - public class powerOf2 { public static void main(String args[]) { int num = 3; if (checkPower(num)) { System. out .println( " The number is a power of two" ); } else { System. out .println( " The number is not a power of two" ); }

Program to find the longest sequence in an array

This post will teach you to find the longest sequence of elements in an array. We need two sets in this program. One for adding the current elements and one which contains the longest sequence of elements.The logic is as follows:- Initialize current set and set containing longest sequence of values. For each current element, we will check the next elements. Clear the values in current set. Add the current element to a set. Assign previous element to current element If the next element is 1 greater than the previous element, update previous element as the next element and next element as next element of array. Else check whether the size of current set is greater than the set which contain longest sequence of elements. If it is greater, then remove all the elements form longest sequence set and add all elements of current sequence set to that. Since we need to loop all the elements twice, the time complexity of the program is O(n^2). The program is as follows: import

Map and Set in Java - made easy

This post will teach you how and where to use Map and Set in Java. I have written in the simplest possible way. Please note that this post won't deal with internal implementation of Maps and Sets. MAP Definition A Map is an Interface in which an object maps keys to values. Use A map is basically used when we need to store the particular occurrence of some variables. For e.g.: - Write a program to find the occurrence of all the words in a file. In this program we will put all the words in the map and whenever we find an occurrence will increment the value. Types The basic different types of Maps in Java are HashMap, TreeMap and LinkedHashMap. HashMap - HashMap is implemented using a Hash Table. The insertion and retrieval time in HashMap is O(1). When you traverse the elements, there won't be a specific order. TreeMap - Implemented Using Red-Black-Tree. The insertion and retrieval time in TreeMap is O(log n). Sorted order. For more about Red- Black - Tree, p

How to find the smallest 1000 or largest 1000 items from a billion of items

This is one of the most important concept in data structure and frequently asked in interviews. The one solution to this problem is sorting the elements and returning it. But when we see the complexity, it won't be advisable to sort all the billion items. For cracking this problem, we use a data structure called "min- heap" or "max-heap". For finding the largest 'n' elements, min- heap is used and for smallest 'n' elements, max-heap is used. In max-heap, keys of parent nodes are always greater than the children and the root node will be give the maximum value in that heap. Just the opposite in min-heap. The algorithm to find the smallest 1000 elements are as follows: - Create a max-heap of size 1000. Put the first 1000 elements to max-heap. For all the elements starting from 1001 to 'N', delete the maximum element from the max-heap and put the latter element. Return the heap. For finding the largest 1000 elements, use min-he

Java program to find whether 2 linked lists intersect.

The basic idea when we get by looking the question is that if 2 linked lists intersect at some place, then their tails should be the same. First we need to traverse both the linked list and find both its length. Then compare the last elements. If this is not equal, we can return there itself saying that both linked lists are not the same. If the length of one node is greater than second node, traverse those many positions of the first node to reach the second node. From that position, traverse both the nodes and check whether the nodes are same. If equal nodes are found, then break and return true. If no equal nodes are found, then return false. I am writing only the function which checks the intersection of the loop. Refer -  http://sujithforu.blogspot.com/2012/10/linked-list-implementation-in-java.html for implemention of linked list. public static boolean loopIntersection(ListElement node1, ListElement node2) { if(node1==null || node2==null) { return false; } ListElemen

Java problems without using normal methods

1) Program to check whether the number is odd or even without using modulo or division operators. The idea is to subtract 2 from the number until the number is less than 2. If the remaining value is 0, then number is odd, else even. public class oddeven { public static void main(String args[]) { int num =27; if (odd(num)) System. out .println( "ODD" ); else System. out .println( "EVEN" ); } public static boolean odd( int num) {   int temp=num; while (temp>=2) temp=temp-2; if (temp==1) return true ; else return false ; } } 2) Java program to return the maximum of two numbers without using if,while or for. If 'a' and 'b' are 2 numbers, then the maximum value of a,b is (a+b)/2 + (|a-b|)/2. For one part we need to use floor and for the other part we need to use ceiling. public class maxnum { public static void main(String args[])