Sunday, June 7, 2015

Panda, possible combinations to pick two subsets whose MAXOR value is same



Little Black Panda is mad about XOR operation. Presently he has gone mad and he won't stop performing XOR operation on various numbers.
Given an array of N numbers, for each subset of the array Little Panda performs MAXOR operation for the subset.MAXOR operation means that he finds XOR of all elements in that subset (if the subset is [1,2,3] then MAXOR is 1^2^3=0) . Little Panda is now exhausted and wants to do something else. Little Panda wants to pick any two subsets the MAXORof whose are same. Little Panda needs to find the number of selections that he can make. He is very very weak in programming so he wants the answer from you. Please help little panda in finding out his query.
Since the output can be very large output it modulo 1000000007.
Input Format
The first line contains N i.e. the length of the array.
N lines follow, each containing a non-negative integer.
Output Format
Output Little Panda's Query in a single line.
Constraints
1 <= N <= 100000
0 <= A[i] <= 100
(where A[i] denotes the value of array element)

Courtesy: HackerEarth


 # Finding the count of subsets, such that each subset MAXOR is x in   
 def FindSubsetCount(array,n):  
  maxOrCount = [0]*128 # array to store MAXOR Count frequencies  
  maxOrCount[0] = 1 # Assuming null subset sum = 1  
  for i in range(0,n):  
  element = array[i]  
  changeCount=[0]*128  
  for j in range(0,128):  
   changeCount[j^element] += maxOrCount[j]  
  for j in range(0,128):  
   maxOrCount[j] += changeCount[j]  
   maxOrCount[j] %= 1000000007  
  maxOrCount[0] -= 1 # remove as the panda does not know about the null subset  
  return TotalCombinations(maxOrCount)  
 def TotalCombinations(maxOrCount):  
  threshold = 1000000007  
  totalCombinations = 0  
  for i in range(0,128):  
  subsetCount = maxOrCount[i]  
  if subsetCount > 0:  
   totalCombinations += ((subsetCount*(subsetCount-1))/2)  
   if totalCombinations >= threshold:  
   totalCombinations %= threshold  
  return totalCombinations  
 def run():  
  try:  
  # collect number of input elements  
  n = int(raw_input())  
  if n < 1 or n > 100000:  
   return  
  # collect input elements  
  a = [0]*n  
  #a = map(int, raw_input().split())  
  for i in range(0,n):  
   a[i] = int(raw_input())  
   if a[i] < 0 or a[i] > 100:  
   return  
  print FindSubsetCount(a,n)  
  except:  
   return  
 if __name__ == "__main__":  
  run()  

Wednesday, January 5, 2011

[JAVA] - JNLP Communication

JNLP stands for Java Network Launch Protocol.
Lets understand this in bottom-up approach.

one can launch the java application using JNLP communication as below

1. From command line: javaws URL
URL: http(s)://://.jnlp
The above URL points to JNLPFile

(or)

2. From the browser you can type in URL

It will download the specified JNLP file and start processing it as per the contents in it.

what is a JNLP file? What are the contents of JNLP file?
JNLP file, is a XML file whose extension is .jnlp and filled using xml elements as specified by SUN
One certain question is, and how to fill it ?
refer to this link

Where does JNLP file resides?
Typically on server. (or any webserver which can give a URL for it in the above mentioned format need not be our application server!)

It will download all the specified jars from specified codebase. when i say specified i mean specified in jnlp file.
Depending on the application type we specify, it will trigger the starting point for our java application.

All the downloaded jars are stored in javaws cache.
You can view them using (On windows, open run-prompt, type in javaws -viewer)

will update this post over the period as i am learning it now. :)

Thursday, December 23, 2010

A problem on virtual tables

I used numbers to indicate my analysis please follow in that order. I am using the code snippets compiled using g++.


using namespace std;

class Base
{
public:
virtual void fun()
{
cout<<"Base";

}

};


class Derived:public Base {

public:

void fun();

};

int main() {

Base* p;

p=new Base();

/** 3. for p: virtual pointer to virtual table of Base

* since it contains only a pointer size is 4bytes

**/


Derived d;

/**4. compiled successfully, but linker issued an error

* 5. Creates a pointer to a virtual table of d

* 6. function is bound on some name in virtual table (lets say id02)

* 7. Entry of virtual table could be as below

* (ex: id02(binding name), fun(unresolved reference which has to be resolved during linking phase)

**/

/* Ignore below line as analysis not needed now */

p->fun();
return 0;
}

bash-3.00$ g++ vptr.cc
Undefined first referenced
symbol in file
vtable for Derived /var/tmp//ccmN6VG1.o


ld: fatal: Symbol referencing errors. No output written to a.out

1. What type of error we got?

A) The above line indicates it is a linker error. (Meaning lexical, syntax, semantic etc are through)

2. What is that symbol?

A) Vtable for derived (some convention used to indicate such unresolved references)

collect2: ld returned 1 exit status

8. During the linking phase, it searches for the unresolved reference (lets say in ST) and ends in an un successful attempt as there was no definition and issues an error.

ld went mad hence exited with status 1 resulting in such scary red lines