WhatsJS.com

Simple way to create amazing documents for your project.

Monday, January 15, 2018

JavaScript - Linear Search

Problem Statement
Given an array of N elements and an integer K, return the position of the first occurrence of K in given array.  The position of the first element is considered as 1.

Output -1 if the number is not found in an array.
InputThe first line contains 'T' denoting the number of test cases. Then follows the description of test cases. Each case begins with a two space separated integer N and K denoting the size of the array and the value of K respectively. The next line contains the N-space separated integers denoting the elements of the array.

OutputFor each test case, print the index of the first occurrence of given number K. 
Print -1 if the number is not found in the array. 

Constraints:
1<=T<=100
1<=N<=1000
1<=K<=100000
1<=A[i]<=100000

Example:
Input :
2
5 16
9 7 2 16 4
98
1 22 57 47 34 18 66
Output
4
-1


























Friday, January 12, 2018

Codility - PtrListLen

A pointer is called a linked list if:
  • it is an empty pointer (it is then called a terminator or an empty list); or
  • it points to a structure (called a node or the head) that contains a value and a linked list (called the tail).
The length of a list is defined as the total number of nodes it contains. In particular, an empty list has length 0.
For example, consider the following linked list:
A -> B -> C -> D ->
This list contains four nodes: A, B, C and D. Node D is the last node and its tail is the terminator. The length of this list is 4.
Assume that the following declarations are given:
// IntList obj is an Object with attributes // obj.value - type: int // obj.next - type: IntList
Write a function:
function solution(L);
that, given a non-empty linked list L consisting of N nodes, returns its length.
For example, given list L shown in the example above, the function should return 4.
Assume that:
  • N is an integer within the range [1..5,000];
  • list L does not have a cycle (each non-empty pointer points to a different structure).
In your solution, focus on correctness. The performance of your solution will not be the focus of the assessment.

Codility MinAbsSliceSum

A non-empty zero-indexed array A of N integers is given. A pair of integers (P, Q), such that 0 ≤ P ≤ Q < N, is called a slice of array A. The sum of a slice (P, Q) is the total of A[P] + A[P+1] + ... + A[Q].
min abs slice is a slice whose absolute sum is minimal.
For example, array A such that:
A[0] = 2 A[1] = -4 A[2] = 6 A[3] = -3 A[4] = 9
contains the following slices, among others:
  • (0, 1), whose absolute sum = |2 + (−4)| = 2
  • (0, 2), whose absolute sum = |2 + (−4) + 6| = 4
  • (0, 3), whose absolute sum = |2 + (−4) + 6 + (−3)| = 1
  • (1, 3), whose absolute sum = |(−4) + 6 + (−3)| = 1
  • (1, 4), whose absolute sum = |(−4) + 6 + (−3) + 9| = 8
  • (4, 4), whose absolute sum = |9| = 9
Both slices (0, 3) and (1, 3) are min abs slices and their absolute sum equals 1.
Write a function:
int solution(int A[], int N);
that, given a non-empty zero-indexed array A consisting of N integers, returns the absolute sum of min abs slice.
For example, given:
A[0] = 2 A[1] = -4 A[2] = 6 A[3] = -3 A[4] = 9
the function should return 1, as explained above.
Assume that:
  • N is an integer within the range [1..100,000];
  • each element of array A is an integer within the range [−10,000..10,000].
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).

Codility DeepestPit

A non-empty zero-indexed array A consisting of N integers is given. A pit in this array is any triplet of integers (P, Q, R) such that:
  • 0 ≤ P < Q < R < N;
  • sequence [A[P], A[P+1], ..., A[Q]] is strictly decreasing, 
    i.e. A[P] > A[P+1] > ... > A[Q];
  • sequence A[Q], A[Q+1], ..., A[R] is strictly increasing, 
    i.e. A[Q] < A[Q+1] < ... < A[R].
The depth of a pit (P, Q, R) is the number min{A[P] − A[Q], A[R] − A[Q]}.
For example, consider array A consisting of 10 elements such that:
A[0] = 0 A[1] = 1 A[2] = 3 A[3] = -2 A[4] = 0 A[5] = 1 A[6] = 0 A[7] = -3 A[8] = 2 A[9] = 3
Triplet (2, 3, 4) is one of pits in this array, because sequence [A[2], A[3]] is strictly decreasing (3 > −2) and sequence [A[3], A[4]] is strictly increasing (−2 < 0). Its depth is min{A[2] − A[3], A[4] − A[3]} = 2. Triplet (2, 3, 5) is another pit with depth 3. Triplet (5, 7, 8) is yet another pit with depth 4. There is no pit in this array deeper (i.e. having depth greater) than 4.
Write a function:
function solution(A);
that, given a non-empty zero-indexed array A consisting of N integers, returns the depth of the deepest pit in array A. The function should return −1 if there are no pits in array A.
For example, consider array A consisting of 10 elements such that:
A[0] = 0 A[1] = 1 A[2] = 3 A[3] = -2 A[4] = 0 A[5] = 1 A[6] = 0 A[7] = -3 A[8] = 2 A[9] = 3
the function should return 4, as explained above.
Assume that:
  • N is an integer within the range [1..1,000,000];
  • each element of array A is an integer within the range [−100,000,000..100,000,000].
Complexity:
  • expected worst-case time complexity is O(N);
  • expected worst-case space complexity is O(N), beyond input storage (not counting the storage required for input arguments).

Codility - CountMultiplicativePairs

Zero-indexed arrays A and B consisting of N non-negative integers are given. Together, they represent N real numbers, denoted as C[0], ..., C[N−1]. Elements of A represent the integer parts and the corresponding elements of B (divided by 1,000,000) represent the fractional parts of the elements of C. More formally, A[I] and B[I] represent C[I] = A[I] + B[I] / 1,000,000.
For example, consider the following arrays A and B:
A[0] = 0 B[0] = 500,000 A[1] = 1 B[1] = 500,000 A[2] = 2 B[2] = 0 A[3] = 2 B[3] = 0 A[4] = 3 B[4] = 0 A[5] = 5 B[5] = 20,000
They represent the following real numbers:
C[0] = 0.5 C[1] = 1.5 C[2] = 2.0 C[3] = 2.0 C[4] = 3.0 C[5] = 5.02
A pair of indices (P, Q) is multiplicative if 0 ≤ P < Q < N and C[P] * C[Q] ≥ C[P] + C[Q].
The above arrays yield the following multiplicative pairs:
  • (1, 4), because 1.5 * 3.0 = 4.5 ≥ 4.5 = 1.5 + 3.0,
  • (1, 5), because 1.5 * 5.02 = 7.53 ≥ 6.52 = 1.5 + 5.02,
  • (2, 3), because 2.0 * 2.0 = 4.0 ≥ 4.0 = 2.0 + 2.0,
  • (2, 4) and (3, 4), because 2.0 * 3.0 = 6.0 ≥ 5.0 = 2.0 + 3.0.
  • (2, 5) and (3, 5), because 2.0 * 5.02 = 10.04 ≥ 7.02 = 2.0 + 5.02.
  • (4, 5), because 3.0 * 5.02 = 15.06 ≥ 8.02 = 3.0 + 5.02.
Write a function:
int solution(int A[], int B[], int N);
that, given zero-indexed arrays A and B, each containing N non-negative integers, returns the number of multiplicative pairs of indices.
If the number of multiplicative pairs is greater than 1,000,000,000, the function should return 1,000,000,000.
For example, given:
A[0] = 0 B[0] = 500,000 A[1] = 1 B[1] = 500,000 A[2] = 2 B[2] = 0 A[3] = 2 B[3] = 0 A[4] = 3 B[4] = 0 A[5] = 5 B[5] = 20,000
the function should return 8, as explained above.
Assume that:
  • N is an integer within the range [0..100,000];
  • each element of array A is an integer within the range [0..1,000];
  • each element of array B is an integer within the range [0..999,999];
  • real numbers created from arrays are sorted in non-decreasing order.
Complexity:
  • expected worst-case time complexity is O(N);
  • expected worst-case space complexity is O(1), beyond input storage (not counting the storage required for input arguments).

BugfixingLeaderSorted: JavaScript

A non-empty zero-indexed array A consisting of N integers and sorted in a non-decreasing order (i.e. A[0] ≤ A[1] ≤ ... ≤ A[N−1]) is given. The leader of this array is the value that occurs in more than half of the elements of A.
You are given an implementation of a function:
int solution(int A[], int N);
that, given a non-empty zero-indexed array A consisting of N integers, sorted in a non-decreasing order, returns the leader of array A. The function should return −1 if array A does not contain a leader.
For example, given array A consisting of ten elements such that:
A[0] = 2 A[1] = 2 A[2] = 2 A[3] = 2 A[4] = 2 A[5] = 3 A[6] = 4 A[7] = 4 A[8] = 4 A[9] = 6
the function should return −1, because the value that occurs most frequently in the array, 2, occurs five times, and 5 is not more than half of 10.
Given array A consisting of five elements such that:
A[0] = 1 A[1] = 1 A[2] = 1 A[3] = 1 A[4] = 50
the function should return 1.
The attached code is still incorrect on some inputs. Despite the error(s), the code may produce a correct answer for the example test cases. The goal of the exercise is to find and fix the bug(s) in the implementation. You can modify at most three lines.
Assume that:
  • N is an integer within the range [1..100,000];
  • each element of array A is an integer within the range [0..2,147,483,647];
  • array A is sorted in non-decreasing order.
Complexity:
  • expected worst-case time complexity is O(N);
  • expected worst-case space complexity is O(N), beyond input storage (not counting the storage required for input arguments).
Solution -


Tuesday, April 12, 2016

How to Upgrade NodeJS to Latest Version using NPM

If we want to upgrade NodeJS to latest version using NPM.

we have to run following commands in Terminal.

sudo npm cache clean -f

sudo npm install -g n

sudo n stable

Featured Documents

HTML5

View more

JavaScript

View more

Image Documents

View more

Tutorials

View more

Legal Documents

View more

Getting Started

View more