Skip to main content

Posts

Showing posts from 2015

Find whose best friends got offered higher salary!

Problem Statement You are given three tables:  Students ,  Friends  and  Packages.   Students   contains two columns: ID  and   Name .   Friends   contains two columns:   ID   and   Friend_ID   ( ID   of the ONLY best friend). Packages  contains two columns:   ID   and   Salary   (offered salary in $ thousands per month). Write a query to output the names of those students whose best friends got offered a higher salary than them. Names must be ordered by the salary amount offered to the best friends. It is guaranteed that no two students got same salary offer. Sample Input Sample Output Samantha Julia Scarlet Explanation See the following table: Now, ·          Samantha's   best friend got offered a higher salary than her at 11.55 ·          Julia's   best friend got offered a higher salary than her at 12.12 ·          Scarlet's   best friend got offered a higher salary than her at 15.2 ·          Ashley's   best friend did

MinAbsSumOfTwo ::: Find the minimal absolute value of a sum of two elements.

Task description Let A be a non-empty zero-indexed array consisting of N integers. The  abs sum of two  for a pair of indices (P, Q) is the absolute value |A[P] + A[Q]|, for 0 ≤ P ≤ Q < N. For example, the following array A: A[0] = 1 A[1] = 4 A[2] = -3 has pairs of indices (0, 0), (0, 1), (0, 2), (1, 1), (1, 2), (2, 2). The abs sum of two for the pair (0, 0) is A[0] + A[0] = |1 + 1| = 2. The abs sum of two for the pair (0, 1) is A[0] + A[1] = |1 + 4| = 5. The abs sum of two for the pair (0, 2) is A[0] + A[2] = |1 + (−3)| = 2. The abs sum of two for the pair (1, 1) is A[1] + A[1] = |4 + 4| = 8. The abs sum of two for the pair (1, 2) is A[1] + A[2] = |4 + (−3)| = 1. The abs sum of two for the pair (2, 2) is A[2] + A[2] = |(−3) + (−3)| = 6. Write a function: class Solution { public int solution(int[] A); } that, given a non-empty zero-indexed array A consisting of N integers, returns the minimal abs sum of two for any pair of indices in this array. For example, g

COMPUTE number of inversion in an array - ArrayInversionCount

Task description A zero-indexed array A consisting of N integers is given. An  inversion is a pair of indexes (P, Q) such that P < Q and A[Q] < A[P]. Write a function: class Solution { public int solution(int[] A); } that COMPUTES  the number of inversions in A, or returns −1 if it exceeds 1,000,000,000. Assume that: N is an integer within the range [ 0 .. 100,000 ]; each element of array A is an integer within the range [ −2,147,483,648 .. 2,147,483,647 ]. For example, in the following array: A[0] = -1 A[1] = 6 A[2] = 3 A[3] = 4 A[4] = 7 A[5] = 4 there are four inversions: (1,2) (1,3) (1,5) (4,5) so the function should return 4. 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. // you can also use imports, for example: // import java.util.*; // you can write to

Calculate the number of elements of an array that are not divisors of each element.

Task description 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[6] = 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