Randomly assign 3 variables [closed] - random

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
I have 3 variables, a,b and c, and want to randomly assign them 3 possible numbers, 1,2 or 3. a may equal b but c may not equal either.
So if a = 2 and = 1, then c = 3.
If a = 1 and b = 1, then c must be either 2 or 3(randomly).
Currently I do:
a and b = random values. Then:
if (a == b)
{
do
{
c = (int)(Math.random()*3);
} while (c == a);
} else
{
do
{
c = (int)(Math.random()*3);
}while (c == a || c == b);
}
Is this the best way? How else would I do this?

(1) Making a call to generate a random number is the most expensive thing you are doing here, so you should do it as little as possible (two or three times).
(2) You should probably prefer java.util.Random, particularly if you want ints.
java.util.Random random = new java.util.Random();
int a = random.nextInt(3) + 1;
int b = random.nextInt(3) + 1;
int c = 0;
for(int n = a == b ? random.nextInt(2) : 0; n >= 0; n--) {
do {
c++;
} while(c == a || c == b);
}
This will be "uniformly" random.
Explanation:
We first choose a and b randomly from the three choices. We are left with either one or two choices for c, depending whether a equals b. We "roll the dice" for c and find the spot where c fits in.

You can assign a value to the c at first, and then based on that, randomly assign other variables:
c = random.nextInt(3) + 1;
do
{
a = random.nextInt(3) + 1;
} while(a == c);
do
{
b = random.nextInt(3) + 1;
} while(b == c);

Related

Number of moments where all turned on bulbs are shined [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
public int solution(int[] A) {
// A - array of bulbs. A[i] its position in row.
// return number of moments where all turned on bulbs are shined
// start from 0 to length-1, switch on bulbs ( A[i] represents a bulb's position)
// A[i] bulb shined if: 1) A[i] is switched 2) 1..A[i]-1 all are shined
// examples:
// input: {1,2,3,4,5} output: 5
// input: {1} output: 1
// input: {2,3,4,1,5} output: 2
// input: {2,1,3,5,4} output: 3
}
I suggested to iterate i: from 0 to a length-1, save every A[i] in a SortedSet. Check if there are i-1 elements in headSet < A[i]. If yes - we A[i] is shined.
It seems the performance of the solution above is low...
Can anybody suggest better?
You could do it in O(n):
public int solution(int[] a) {
Set<Integer> missing = new HashSet<>();
Set<Integer> store = new HashSet<>();
int count = 0;
for (int i = 0; i < a.length; i++) {
if (!store.contains(i + 1) && i + 1 != a[i])
missing.add(i + 1);
if (i + 1 < a[i])
store.add(a[i]);
else
missing.remove(a[i]);
if (missing.isEmpty())
count++;
}
return count;
}

Finding the maximum and minimum elements using 3n / 2 comparisons? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I am trying to design an algorithm that, given a list of n elements, finds the min and max in 3n / 2 comparisons. Any hints on how to do this?
As a hint, imagine that all the array elements are players in an elimination tournament. Pair off all the players and have the "winners" (bigger numbers) advance to one tournament and the "losers" (smaller numbers) fall into a loser's bracket. You will now have n / 2 winners to consider, and the maximum value must be one of them, and n / 2 losers to consider, and the minimum value must be one of them. In the process of doing this, you made n / 2 comparisons. Can you use n remaining comparisons to find the minimum of one group and the maximum of the other?
#templatetypedef 's hint is right:
public static void findMinimumAndMaximumOf(int[] numbers) {
assert numbers.length >= 2;
List<Integer> bigList = new ArrayList<>(numbers.length / 2);
List<Integer> smallerList = new ArrayList<>(numbers.length / 2);
int i = 1;
for (; i < numbers.length; i = i + 2) {
if (numbers[i] > numbers[i - 1]) {
bigList.add(numbers[i]);
smallerList.add(numbers[i - 1]);
} else {
bigList.add(numbers[i - 1]);
smallerList.add(numbers[i]);
}
}
if ((numbers.length & 1) == 1) {
if (numbers[numbers.length - 1] > numbers[numbers.length - 2]) {
bigList.add(numbers[numbers.length - 1]);
} else {
smallerList.add(numbers[numbers.length - 1]);
}
}
Iterator<Integer> iBig = bigList.iterator();
int biggest = iBig.next();
while (iBig.hasNext()) {
int current = iBig.next();
if (current > biggest) {
biggest = current;
}
}
Iterator<Integer> iSmall = smallerList.iterator();
int smallest = iSmall.next();
while (iSmall.hasNext()) {
int current = iSmall.next();
if (current < smallest) {
smallest = current;
}
}
System.out.println(String.format("Max:%d, Min:%d" ,biggest,smallest));
}

Project Euler 31: Why does this solution work?

I have been studying various solution to Project Euler question #31:
In England the currency is made up of pound, £, and pence, p, and
there are eight coins in general circulation: 1p, 2p, 5p, 10p, 20p,
50p, £1 (100p) and £2 (200p).
It is possible to make £2 in the following way: 1x£1 + 1x50p + 2x20p + 1x5p + 1x2p + 3x1p
How many different ways can £2 be made using any number of coins?
I am confused why this particular brute force solution works (source):
int target = 200;
int ways = 0;
for (int a = target; a >= 0; a -= 200) {
for (int b = a; b >= 0; b -= 100) {
for (int c = b; c >= 0; c -= 50) {
for (int d = c; d >= 0; d -= 20) {
for (int e = d; e >= 0; e -= 10) {
for (int f = e; f >= 0; f -= 5) {
for (int g = f; g >= 0; g -= 2) {
ways++;
}
}
}
}
}
}
}
Based on this solution I would expect there to be one more nested for-loop at the innermost level that is decrementing by 1, for coins of 1p value.
For example, when do we count the scenario where 200p is made up of only 1p coins?
As it stands currently, the code leads to the correct answer. But if an additional for-loop was added then it would seem the answer should be much bigger. What am I missing?
Because the for-loops count up ways to make a number <= 200, assuming the remainder is made up of 1p coins. For example, consider the first iteration, where a = b = c = d = e = f = g = 200, this is exactly the case you asked about explicitly, where 200p is made of all 1's.
For any given combination of 2p, 5p, 10p, 20p, 50p, £1 (100p) and £2 (200p) coins that sum up to £2 or less there's precisely one number of 1p coins that would make up an even £2. Hence, you don't need another loop to iterate the number of 1p coins.

What's an efficient algorithm to find one unique number in a set of 1 millions numbers? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have been asked this question in an interview. In 1 million numbers, all numbers have a duplicate except for one number. How to find that one number? Which algorithm should I use to get a good time and space complexity? I got an idea to use EXOR gate but I'm still lagging in deploying that.
Use xor for all numbers sequentially.
For following list of numbers:
1, 2, 3, 4, 3, 2, 1
Let ^ represents the exclusive disjunction (or xor)
Then,
1 ^ 2 ^ 3 ^ 4 ^ 3 ^ 2 ^ 1 = 4
Another simple solution: Two bitsets can be used, one for marking the existance of a number and another to mark duplication. We iterate through the array amd mark each element for existence and duplication. Then we iterate through the bitsets to find a number that is marked for existence and but not marked for duplication.
int[] numbers = new int[] { 1, 1, 2, 2, 3, 4, 4, 5, 5 };
BitSet bs1 = new BitSet();
BitSet bs2 = new BitSet();
int largestNumber = 0;
for (int i = 0; i < numbers.length; i++) {
int number = numbers[i];
if (bs1.get(number) == false) {
// Mark for existence
bs1.set(number);
} else {
// Mark for duplicating
bs2.set(number);
}
if (number > largestNumber) {
largestNumber = number;
}
}
for (int i = 0; i <= largestNumber; i++) {
if (bs1.get(i) && !bs2.get(i)) {
System.out.println("Non duplicating number is: " + i);
}
}
}
try this
int[] a = {1, 2, 1, 2, 3};
Arrays.sort(a);
for(int i = 0; i < a.length; i++) {
if (i == 0 && a[i] != a[i + 1] || i == a.length -1 || a[i] != a[i - 1] && a[i] != a[i + 1]) {
System.out.println(a[i]);
break;
}
}
Following may solve you problem:
Complexity: O(N)
// Assuming the duplicate are going by pair
// x ^x == 0 and x ^0 == x
int find_unique(const std::vector<int>& v)
{
assert(v.size() % 2 == 1);
int res = 0;
for (auto value : v) {
res ^= value;
}
return res;
}
Or
Complexity: O(N log N)
int find_unique(std::vector<int>& v)
{
if (v.empty()) { throw std::runtime_error("empty vector"); }
std::sort(v.begin(), v.end());
auto it = std::unique(v.begin(), v.end());
if (it == v.begin()) { throw std::runtime_error("no unique number"); }
if (it != v.begin() + 1) { throw std::runtime_error("several unique numbers"); }
return v[0];
}
or
Complexity: O(N log N)
int find_unique(std::vector<int>& v)
{
if (v.empty()) { throw std::runtime_error("empty vector"); }
if (v.size() == 1) { return v[0]; }
std::sort(v.begin(), v.end());
if (v[0] != v[1]) { return v[0]; }
for (int i = 1, size = v.size(); i + 1 < size; ++i) {
if (v[i] == v[i - 1]) { continue; }
if (v[i] == v[i + 1]) { ++i; continue; } // we may skip v[i + 1]
return v[i];
}
return v.back();
}
I believe that if we combine quicksort(search) technique and xor, we could get the fastest possible code. I am trying though, but correct me if this idea is wrong meanwhile.
BTW, this has good number of usecases though. Though the question is language agnostic, and requires clear algorithm, I am mentioning some use cases readers might find useful.
0's can be duplicate... or negative numbers...
System.out.println(33 ^ 33 ^ 7 ^ 0 ^ 0 ^ 5 ^ 7 ^ 5); is giving 0 (0 is a duplicate here)
Duplicates might be more than 2:
System.out.println(1 ^ 1 ^ 2 ^ 3 ^ 3 ^ 3); is giving 1, instead of 2...
And on and on, the question would possibly be complex than we might first think.
For a large number of input items (as opposed to the examples with a few numbers) it should take a considerable amount of time getting the input into some data structure. I was thinking about using this necessary time as part of the computation process by putting it into a map. The value of the map will only be one for the one single number. I assume the data is correct so I omit checking for that, but if there are multiple numbers which occur only one time it will just return the first one.
There should be room for further optimization, e.g. by accessing the map by value and just look for the one which has 1. I think this could be done easily with Boost.Bimap.
int getSingleNumber(){
map<int, int> numbers;
for (all input items)
{
numbers[currentInputItem]++;
}
for( map<int,int>::iterator ii=numbers.begin(); ii!=numbers.end(); ++ii)
{
if ( (*ii).second == 1 ) return (*ii).first;
}
}

Find all the quadruples [a, b, c, d] where a^3 + b^3 = c^3 + d^3 when 1 <= a, b, c or d <= 10000 [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
Looking for an algorithm or some coding hints to find the solutions for
a^3 + b^3 = c^3 + d^3, where a, b, c and d all are in the range [1 .. 10000]
It's an interview question.
I'm thinking priority queues to at least iterate for a and b values. Some hint will be great, will try to work through from there.
Using a hash map to store the (cube,(a,b)), you can iterate all possible pairs of integers, and output a solution once you have found that the required sum of cubes is already in the map.
pseudo code:
map <- empty hash_map<int,list<pair<int,int>>>
for each a in range(0,10^5):
for each b in range(a,10^5): //making sure each pair repeats only once
cube <- a^3 + b^3
if map.containsKey(cube):
for each element e in map.get(cube):
output e.first(), e.last(), a, b //one solution
else:
map.put(cube,new list<pair<int,int>>)
//for both cases, add the just found pair to the relevant list
map.get(cube).add(cube,new pair(a,b))
This solution is O(n^2) space(1) and O(n^2 + OUTPUT) time on average, where OUTPUT is the size of the output.
EDIT:
Required space is actually O(n^2 logn), where n is the range (10^5), because to represent 10^5 integers you need ceil(log_2(10^15)) = 50 bits. So, you actually need something like 500,000,000,000 bits (+ overhead for map and list) which is ~58.2 GB (+ overhead).
Since for most machines it is a bit too much - you might want to consider storing the data on disk, or if you have 64bits machine - just store in into "memory" and let the OS and virtual memory system do this as best as it can.
(1) As the edit clarifies, it is actually O(n^2log(n)) space, however if we take each integer storage as O(1) (which is usually the case) we get O(n^2) space. Same principle will apply for the time complexity, obviously.
Using a priority queue is almost certainly the simplest solution, and also the most practical one, since it's O(n) storage (with a log factor if you require bignums). Any solution which involves computing all possible sums and putting them in a map will require O(n^2) storage, which soon becomes impractical.
My naive, non-optimized implementation using a priority queue is O(n^2 log(n)) time. Even so, it took less than five seconds for n = 10000 and about 750 seconds for n = 100000, using a couple of megabytes of storage. It certainly could be improved.
The basic idea, as per your comment, is to initialize a priority queue with pairs (a, a+1) for a in the range [1, N), and then repeatedly increment the second value of the smallest (by sum of cubes) tuple until it reaches N. If at any time the smallest two elements in the queue are equal, you have a solution. (I could paste the code, but you only asked for a hint.)
Using Hashmap (O(n^2) solution):
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import static java.lang.Math.pow;
/**
* Created by Anup on 10-10-2016.
*/
class Pair {
int a;
int b;
Pair(int x, int y) {
a = x;
b = y;
}
}
public class FindCubePair {
public static void main(String[] args) {
HashMap<Long, ArrayList<Pair>> hashMap = new HashMap<>();
int n = 100000;
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= n; j++) {
long sum = (long) (pow(i, 3) + pow(j, 3));
if(hashMap.containsKey(sum)) {
List<Pair> list = hashMap.get(sum);
for(Pair p : list) {
System.out.println(i + " " + j + " " + p.a + " " + p.b);
}
} else {
ArrayList<Pair> list = new ArrayList<>();
hashMap.put(sum, list);
}
hashMap.get(sum).add(new Pair(i, j));
}
}
}
}
Unfortunately, the value of integers printed does not even reach 1000 on my computer due to resource limitation.
A quicker than trivial solution is as follows: You calculate all values that a^3 + b^3 can have, and store all possible values of a and b with it. This is done by looping through a and b, storing the results (a^3 + b^3) in a binary tree and having a list of values (a's and b's) associated to each result.
After this step, you need to traverse the list and for each value, choose every possible assignment for a,b,c,d.
I think this solution takes O(n^2 log n) time and O(n^2) space, but i might be missing something.
int Search(){
int MAX = 10000000;
for(int a = 0; a < MAX; a++){
int a3 = a * a * a;
if(a3 > MAX) break;
for(int b = a; b < MAX; b ++){
int b3 = b * b * b;
if(a3 + b3 > MAX)break;
for(int c = 0; c < a; c++){
int c3 = c*c*c;
int m = a3 - c3;
int d = b+1;
while(true){
int d3 = d * d * d;
if(d3-b3 <= m){
if((d3 - b3) == m){
count++;
PUSH_Modified(a3, b3, c3, b3, a, b, c, d);
}
d++;
continue;
}
else
break;
}
}
}
}
return 0;
}
Let's assume a solution:
a=A, b=B, c=C, and d=D.
Given any solution we can generate another 3 solutions
abcd
ABCD
ABDC
BACD
BADC
Actually, if A=B, or C=D, then we might only have 1 or 2 further solutions.
We can choose the solutions we look for first by ordering A <= B and C <= D. This will reduce the search space. We can generate the missed solutions from the found ones.
There will always be at least one solution, where A=C and B=D. What we're looking for is when A>C and B<D. This comes from the ordering: C can't be greater than A because, as we've chosen to only look at solutions where D>C, the cube sum would be too big.
We can calculate A^3 + B^3, put it in a map as the key, with a vector of pairs A,B as the value.
There will be (n^2)/2 values.
If there are already values in the vector they will all have lower A and they are the solutions we're looking for. We can output them immediately, along with their permutations.
I'm not sure about complexity.
One Solution - using concept of finding 2 sum in a sorted array. This is O(n3)
public static void pairSum() {
int SZ = 100;
long[] powArray = new long[SZ];
for(int i = 0; i< SZ; i++){
int v = i+1;
powArray[i] = v*v*v;
}
int countPairs = 0;
int N1 = 0, N2 = SZ-1, N3, N4;
while(N2 > 0) {
N1=0;
while(N2-N1 > 2) {
long ts = powArray[N1] + powArray[N2];
N3 = N1+1; N4 = N2-1;
while(N4 > N3) {
if(powArray[N4]+powArray[N3] < ts) {
N3++;
}else if(powArray[N4]+powArray[N3] > ts) {
N4--;
}else{
//System.out.println((N1+1)+" "+(N2+1)+" "+(N3+1)+" "+(N4+1)+" CUBE "+ts);
countPairs++;
break;
}
}
N1++;
}
N2--;
}
System.out.println("quadruplet pair count:"+countPairs);
}
Logic :
a^3 + b^3 = c^3 + d^3
Then, a^3+b^3-c*3-d^3 = 0
Try to solve this equation by putting all combination of values for a,b,c and d in range of [0 , 10^5].
If equation is solved print the values of a,b,c and d
public static void main(String[] args) {
//find all solutions of a^3 + b^3 = c^3 + d^3
double power = 3;
long counter = 0; // to count the number of solution sets obtained
int limit = 100000; // range from 0 to limit
//looping through every combination of a,b,c and d
for(int a = 0;a<=limit;a++)
{
for(int b = 0;b<=limit;b++)
{
for(int c = 0;c<=limit;c++)
{
for(int d = 0;d<=limit;d++)
{
// logic used : a^3 + b^3 = c^3 + d^3 can be written as a^3 + b^3 - c^3 - d^3 = 0
long result = (long)(Math.pow(a,power ) + Math.pow(b,power ) - Math.pow(c,power ) - Math.pow(d,power ));
if(result == 0 )
{
counter++; // to count the number of solutions
//printing the solution
System.out.println( "a = "+ a + " b = " + b + " c = " + c + " d = " + d);
}
}
}
}
}
//just to understand the change in number of solutions as limit and power changes
System.out.println("Number of Solutions =" + counter);
}
Starting with the brute force approach, its pretty obvious it will O(n^4) time to execute.
If space is not a constraint, we can go for combination of list and maps.
The code is self-explanatory, we are using a nested list to keep track of all entries for a particular sum (key in map).
The time complexity is thus reduced from O(n^4) to O(n^2)
public void printAllCubes() {
int n = 50;
Map<Integer, ArrayList<ArrayList>> resMap = new HashMap<Integer, ArrayList<ArrayList>>();
ArrayList pairs = new ArrayList<Integer>();
ArrayList allPairsList = new ArrayList<ArrayList>();
for (int c = 1; c < n; c++) {
for (int d = 1; d < n; d++) {
int res = (int) (Math.pow(c, 3) + Math.pow(d, 3));
pairs.add(c);
pairs.add(d);
if (resMap.get(res) == null) {
allPairsList = new ArrayList<ArrayList>();
} else {
allPairsList = resMap.get(res);
}
allPairsList.add(pairs);
resMap.put(res, allPairsList);
pairs = new ArrayList<Integer>();
}
}
for (int a = 1; a < n; a++) {
for (int b = 1; b < n; b++) {
int result = (int) (Math.pow(a, 3) + Math.pow(b, 3));
ArrayList<ArrayList> pairList = resMap.get(result);
for (List p : pairList) {
System.out.print(a + " " + b + " ");
for (Object num : p)
System.out.print(num + " ");
System.out.println();
}
}
}
}

Resources