Friday, 8 January 2016

Circular prime

Circular prime
A circular prime is a prime number with the property that the number generated at each intermediate step when cyclically permuting its (base 10) digits will be prime.

For example, 1193 is a circular prime, since 1931, 9311 and 3119 all are also prime.

Note:
A circular prime with at least two digits can only consist of combinations of the digits 1, 3, 7 or 9, because having 0, 2, 4, 6 or 8 as the last digit makes the number divisible by 2, and having 0 or 5 as the last digit makes it divisible by 5.

Reference:


Kaprekar number

Kaprekar number

Named after Dattaraya Ramchandra Kaprekar .
Kaprekar number for a given base is a non-negative integer, the representation of whose square in that base can be split into two parts that add up to the original number again.

Examples:
297 is a Kaprekar number for base 10, because 297² = 88209, which can be split into 88 and 209, and 88 + 209 = 297.

45 is a Kaprekar number, because 45² = 2025 and 20+25 = 45.

Let X be a non-negative integer.

X is a Kaprekar number for base b if there exist non-negative integers n, A, and positive number B satisfying:
X² = Abn + B, where 0 < B < bn
X = A + B

Note that X is also a Kaprekar number for base bn, for this specific choice of n. More narrowly, we can define the set K(N) for a given integer N as the set of integers X for which[1]

X² = AN + B, where 0 < B < N
X = A + B

Each Kaprekar number X for base b is then counted in one of the sets K(b), K(b²), K(b³),….

Note:

By convention, the second part may start with the digit 0, but must be nonzero.

For example, 999 is a Kaprekar number for base 10, because 999² = 998001, which can be split into 998 and 001, and 998 + 001 = 999. But 100 is not; although 100² = 10000 and 100 + 00 = 100, the second part here is zero.


Sunday, 3 January 2016

Longest sub array that has elements in increasing order?

Increasing longest sub array

We can solve it by using:

Brute force – Time Complexity O(n^2)
Dynamic programming - Time Complexity O(n).

Pseudo code:


def DP(a[]):
            dp[1] = 1
            for i = 2 to n:
                    if a[i] > a[i - 1]:
                            dp[i] = dp[i - 1] + 1
                    else:
                            dp[i] = 1

Monday, 21 December 2015

Design a data structure that supports insert, delete, search and get random in constant time

Perform Insert/Delete/Search/Get Random operations in O(1)

No predefined data structure that satisfies all these requirements.
Insertion supported by most of data structure but Deletion and Get Random supported by few of Data structure.
To perform all operation in constant time, we have to use Hybrid of 2 or more data structures.

1. O(1) Insert
Stacks/Queues/Linked lists and hash tables support this operation, here BST, heap, Skip list, TRIE etc. are eliminated.

2. O(1) delete
Question doesn't clearly specify delete what? First element, last element or any element?
If we have to delete first element than we go for queues, if we have to delete last element we select stack, if we have to delete any element then we opt for hash.
So contenders in the list till now are - stack/Queues and Hash table.

3. O(1) search
At this step both stacks and queues are ruled out as search is not possible in O(1) and only hash table remains in the list. So at this step we are clear that one of the data structure should be hash table.

4. O(1) Get Random
Hash fails to fulfill this requirement, hash requires key to fetch any element and we have no way of generating random keys.

Which data structure satisfies O(1) random access?
There is only one Arrays.
Just give index + starting address and boom array gives you the result in O(1).



import java.util.*;
class FastestDS {

      // A resizable array used to get Random element at runtime
      ArrayList<Integer> array;

      // A hash where keys are array elements and values are indexes in arr[]
      HashMap<Integer, Integer>  hashMap;

      // Constructor (creates arr[] and hash)
      public FastestDS() {
            array = new ArrayList<Integer>();
            hashMap = new HashMap<Integer, Integer>();
      }

      // A Theta(1) function to add an element to FastestDS data structure
      void add(int x) {
            // If element is already present, then noting to do
            if (hashMap.get(x) != null) {
                  return;
            }

            // Else put element at the end of arr[]
            int s = array.size();
            array.add(x);

            // And put in hash also
            hashMap.put(x, s);
      }

      // A Theta(1) function to remove an element from FastestDS data structure
      void remove(int x) {
            // Check if element is present
            Integer index = hashMap.get(x);
            if (index == null) {
                  return;
            }
            // If present, then remove element from hash
            hashMap.remove(x);

            // Swap element with last element so that remove from
            // arr[] can be done in O(1) time
            int size = array.size();
            Integer last = array.get(size-1);
            Collections.swap(array, index,  size-1);

            // Remove last element (This is O(1))
            array.remove(size-1);

            // Update hash table for new index of last element
            hashMap.put(last, index);
      }

      // Returns a random element from FastestDS
      int getRandom() {
            // Find a random index from 0 to size - 1
            Random rand = new Random();
            int index = rand.nextInt(array.size());

            // Return element at randomly picked index
            return array.get(index);
      }

      // Returns index of element if element is present, otherwise null
      boolean contains(int x) {
            return hashMap.get(x)!=null?true:false;
      }
}

Sunday, 13 December 2015

Generate serialVersionUID using Java Program

serialVersionUID can be generated by using getSerialVersionUID() method of the ObjectStreamClass class.

SerialiazedClass.java
package com.serial;
import java.io.Serializable;

class SerialiazedClass implements Serializable {
     String name;
     public void setName(String name) {
           this.name = name;
     }
}

GenerateSerialVerUID.java

import java.io.ObjectStreamClass;
public class GenerateSerialVerUID {
    
     public static void main(String[] args) {
          
           Class hashClass = SerialiazedClass.class;
          
           ObjectStreamClass osc = ObjectStreamClass.lookup(hashClass);
           long serialID = osc.getSerialVersionUID();

           System.out.println(serialID);
     }
}
Output:
1623809446810541828

Saturday, 12 December 2015

How to generate serialVersionUID in Java?

Generate serialVersionUID of Employee class

import java.io.Serializable;
public class Employee implements Serializable {
     private String name;
     public Employee(String name) {
           this.name = name;
     }
     public String getName() {
           return name;
     }
}

1. serialver command
JDK has a build in command called “serialver” to generate the serialVersionUID automatically.


C:\Users\awadh\Desktop>javac Employee.java
C:\Users\awadh\Desktop>serialver Employee
Employee:  private static final long serialVersionUID = -6607742892470200720L;




2. serialver tool


Run command: serialver –show
C:\Users\awadh\Desktop>serialver -show
Put the class description in the tool and click on the show button.
private static final long serialVersionUID = -6607742892470200720L;



3. Using Eclipse IDE
Related Posts Plugin for WordPress, Blogger...