Saturday, October 21, 2017

Codility - Lesson 12 Euclidean Algorithm - 1. ChocolatesByNumbers

Source Link:
https://codility.com/programmers/lessons/12-euclidean_algorithm/chocolates_by_numbers/

Question:
Two positive integers N and M are given. Integer N represents the number of chocolates arranged in a circle, numbered from 0 to N − 1.
You start to eat the chocolates. After eating a chocolate you leave only a wrapper.
You begin with eating chocolate number 0. Then you omit the next M − 1 chocolates or wrappers on the circle, and eat the following one.
More precisely, if you ate chocolate number X, then you will next eat the chocolate with number (X + M) modulo N (remainder of division).
You stop eating when you encounter an empty wrapper.

For example, given integers N = 10 and M = 4. You will eat the following chocolates: 0, 4, 8, 2, 6.
The goal is to count the number of chocolates that you will eat, following the above rules.

Write a function:
class Solution { public int solution(int N, int M); }
that, given two positive integers N and M, returns the number of chocolates that you will eat.

For example, given integers N = 10 and M = 4. the function should return 5, as explained above.

Assume that:
  • N and M are integers within the range [1..1,000,000,000].
Complexity:
  • expected worst-case time complexity is O(log(N+M));
  • expected worst-case space complexity is O(log(N+M)).
My Solution:
Notes:
1. main idea:
// using "gcd(M, N)"
// the number of eaten chocolates = N / gcd(M,N) 
2. using "Euclidean Algorithm" (important)
public static int gcd(int a,int b){
if(a % b == 0)
return b; // case 1
else
return gcd(b,a % b); // case 2 (key point)
  

Codility - Lesson 12 Euclidean Algorithm

Source Link:
https://codility.com/programmers/lessons/12-euclidean_algorithm/

Wikipedia:
https://en.wikipedia.org/wiki/Euclidean_algorithm

Some notes:
The Euclidean algorithm solves the problem of computing the greatest common divisor (gcd) of two positive integers.

12.2. Euclidean algorithm by division

For two given numbers a and b, such that a >= b:
• if b | a, then gcd(a, b) = b,
• otherwise gcd(a, b) = gcd(b, a mod b).


Notice that we can recursively call a function while a is not divisible by b.

12.2: Greatest common divisor by dividing.
1 def gcd(a, b):
2 if a % b == 0:
3 return b
4 else:
5 return gcd(b, a % b)


12.4. Least common multiple

The least common multiple (lcm) of two integers a and b is the smallest positive integer that
is divisible by both a and b. There is the following relation:



Knowing how to compute the gcd(a, b) in O(log(a+b)) time, we can also compute the lcm(a, b)
in the same time complexity.

Codility - Lesson 11 Sieve of Eratosthenes - 2. CountNonDivisible

Source Link:
https://codility.com/programmers/lessons/11-sieve_of_eratosthenes/count_non_divisible/

Question:
You are given a non-empty zero-indexed array A consisting of N integers.
For each number A[i] such that 0 ≤ i < N, we want to count the number of elements of the array that are not the divisors of A[i]. We say that these elements are non-divisors.

For example, consider integer N = 5 and array A such that:
A[0] = 3 A[1] = 1 A[2] = 2 A[3] = 3 A[4] = 6 
For the following elements:
  • A[0] = 3, the non-divisors are: 2, 6,
  • A[1] = 1, the non-divisors are: 3, 2, 3, 6,
  • A[2] = 2, the non-divisors are: 3, 3, 6,
  • A[3] = 3, the non-divisors are: 2, 6,
  • A[4] = 6, there aren't any non-divisors.
Write a function:
class Solution { public int[] solution(int[] A); }
that, given a non-empty zero-indexed array A consisting of N integers, returns a sequence of integers representing the amount of non-divisors.

The sequence should be returned as:
  • a structure Results (in C), or
  • a vector of integers (in C++), or
  • a record Results (in Pascal), or
  • an array of integers (in any other programming language).
For example, given:
A[0] = 3 A[1] = 1 A[2] = 2 A[3] = 3 A[4] = 6 
the function should return [2, 4, 3, 2, 0], as explained above.

Assume that:
  • N is an integer within the range [1..50,000];
  • each element of array A is an integer within the range [1..2 * N].
Complexity:
  • expected worst-case time complexity is O(N*log(N));
  • expected worst-case space complexity is O(N), beyond input storage (not counting the storage required for input arguments).
Elements of input arrays can be modified.

My Solution:
Notes:
1. main idea:
// using "HashMap" to count 
2. map1(key, value)
// key: the elements, 
//value: count of elements
3. map2(key, value)
// key: the elements, 
// value: count of "number of non-divisors" of elements

Codility - Lesson 11 Sieve of Eratosthenes - 1. CountSemiprimes

Source Link:
https://codility.com/programmers/lessons/11-sieve_of_eratosthenes/count_semiprimes/

Question:
A prime is a positive integer X that has exactly two distinct divisors: 1 and X. The first few prime integers are 2, 3, 5, 7, 11 and 13.
A semiprime is a natural number that is the product of two (not necessarily distinct) prime numbers. The first few semiprimes are 4, 6, 9, 10, 14, 15, 21, 22, 25, 26.
You are given two non-empty zero-indexed arrays P and Q, each consisting of M integers. These arrays represent queries about the number of semiprimes within specified ranges.
Query K requires you to find the number of semiprimes within the range (P[K], Q[K]), where 1 ≤ P[K] ≤ Q[K] ≤ N.

For example, consider an integer N = 26 and arrays P, Q such that:
P[0] = 1 Q[0] = 26 P[1] = 4 Q[1] = 10 P[2] = 16 Q[2] = 20  

The number of semiprimes within each of these ranges is as follows:
  • (1, 26) is 10,
  • (4, 10) is 4,
  • (16, 20) is 0.
Write a function:
class Solution { public int[] solution(int N, int[] P, int[] Q); }
that, given an integer N and two non-empty zero-indexed arrays P and Q consisting of M integers, returns an array consisting of M elements specifying the consecutive answers to all the queries.

For example, given an integer N = 26 and arrays P, Q such that:
P[0] = 1 Q[0] = 26 P[1] = 4 Q[1] = 10 P[2] = 16 Q[2] = 20
the function should return the values [10, 4, 0], as explained above.

Assume that:
  • N is an integer within the range [1..50,000];
  • M is an integer within the range [1..30,000];
  • each element of arrays P, Q is an integer within the range [1..N];
  • P[i] ≤ Q[i].
Complexity:
  • expected worst-case time complexity is O(N*log(log(N))+M);
  • expected worst-case space complexity is O(N+M), beyond input storage (not counting the storage required for input arguments).
Elements of input arrays can be modified.

My Solution:
Notes:
1. main idea: using "sieve of Eratosthenes"
https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes
2. initial settting (sieve of Eratosthenes)
Arrays.fill(primeArray, true); // initial setting: all primes
primeArray[0] = false; // not prime 
primeArray[1] = false; // not prime
int sqrtN = (int)Math.sqrt(N);
for(int i =1; i < sqrtN; i++){ 
if(primeArray[i] == true) // prime 
{
int j = i + i;
for(j=j; j<=N; j=j+i){
primeArray[j] = false; // not prime
}
}
    
3.  store all primes in "List"
List<Integer> primeList = new ArrayList<>();
4. find "semiprimes"
// for "primeList.size()"
for(int i=0; i< primeList.size(); i++){
for(int j=i; j< primeList.size(); j++){
semiprimeTemp = (long) primeList.get(i) * (long) primeList.get(j); // semiprimes
if(semiprimeTemp > N){
break;
}
else{
semiprimeArray[(int)semiprimeTemp] = true; // semiprimes
}
}
  
5. compute "cumulative Count of semiprimes"
int[] semiprimeCumulateCount = new int [N+1]; // note: plus one for "0"
for(int i=1; i<=N; i++){
semiprimeCumulateCount[i] = semiprimeCumulateCount[i-1]; // cumulative  
if(semiprimeArray[i] == true){
semiprimeCumulateCount[i]++; // semiprimes
}
 
6. compute "results" (for each query)
for(int i=0; i< numQuery; i++){
result[i] = semiprimeCumulateCount[Q[i]] - semiprimeCumulateCount[P[i]-1]; // note: "P[i]-1" (not included)
}
return result; 

Codility - Lesson 11 Sieve of Eratosthenes

Source Link:
https://codility.com/programmers/lessons/11-sieve_of_eratosthenes/

Wikipedia:
https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes



Some notes: 
The Sieve of Eratosthenes is a very simple and popular technique for finding all the prime
numbers in the range from 2 to a given number n.


It is sufficient to remove only multiples of prime numbers not exceeding (n)^(1/2).

 

The above illustration shows steps of sieving for n = 17.
First, we remove multiples of the smallest element in the set, which is 2. 
The next element remaining in the set is 3, and we also remove its multiples, and so on.

11.1: Sieve of Eratosthenes.
1 def sieve(n):
2 sieve = [True] * (n + 1)
3 sieve[0] = sieve[1] = False
4 i = 2
5 while (i * i <= n):
6 if (sieve[i]):

7 k = i * i
8 while (k <= n):
9 sieve[k] = False
10 k += i
11 i += 1
12 return sieve


So the overall time complexity of this algorithm is O(n log log n).

Codility - Lesson 16 Greedy algorithms - 2. MaxNonoverlappingSegments

Source Link: https://app.codility.com/programmers/lessons/16-greedy_algorithms/max_nonoverlapping_segments/ Question: Located on a line ...