farhan.khwaja's blog

By farhan.khwaja, 13 years ago, In English

Map is an object that stores key/volume pairs. Given a key, you can find its value. Keys must be unique, but values may be duplicated.


The HashMap class uses a hash table to implementation of the map interface. This allows the execution time of basic operations, such as get() and put() to be constant.


An example is below:


Q. Find the numbers Divisible by an array of numbers ie div[] between a range ie from 1 to N?

Ans.  This can be solved using many ways, but we will be solving using HashMap which will help u understand the concept of HashMap in a better way..

HashMap<K,V> : K=key, V=value. K & V can be any thing.


CODE :


1:  import java.util.HashMap; 

2: public class Main {

3: public static int usingHashmap(int N,int div[]){

4: int count=0;

5: HashMap<Integer, Integer> key=new HashMap<Integer, Integer>();

6: for(int i=1;i<N;i++){

7: key.put(i, i);

8: }

9: for(int i=0;i<div.length;i++){

10: for(int k=1;k<N;k++){

11: if(key.get(k)==null){

12: key.put(k, -1);

13: }

14: if(key.get(k)%div[i]==0){

15: count++;

16: key.remove(k);

17: }

18: }

19: }

20: System.out.println(count);

21: return count;

22: }

23: public static void main(String[] args) {

24: new Main().usingHashmap(50, new int[]{3,2});

25: }

26: }

to know more about programming,html etc click here

Full text and comments »

  • Vote: I like it
  • -1
  • Vote: I do not like it

By farhan.khwaja, 13 years ago, In English
A small snippet of the code:

#include<stdio.h>
    #define maxx 101
    
    int Q[maxx];
    int first ,last;
    int node, edge;
    
    char linker[maxx][maxx];
    char var[maxx];
    int path[maxx];
    
    void Ini() {
    int i, j;
    for(i = 1; i<= node; i++) {
    for(j = 1; j<= node; j++) {
    linker[i][j] = 0;
    }
    path[i] = i;
    var[i] = 0;
    }
    }
    
    //Inserts element in the Q ie ENQUEUE
    void Insert(int n) {
    Q[last++] = n;
    last %= maxx;
    }


rest in here click:  http://codetolearn.blogspot.com/2010/12/bfs-algorithm-in-c.html

Full text and comments »

  • Vote: I like it
  • -2
  • Vote: I do not like it

By farhan.khwaja, 13 years ago, In English
www.codetolearn.blogspot.com

Full text and comments »

  • Vote: I like it
  • -3
  • Vote: I do not like it