top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Find all triplets in an array.

+8 votes
905 views

Given an array of numbers find all such triplets that satisfy the given condition.

Condition: a[i] < a[j] < a[k] where I < j < k.
Is it possible to do in linear time O(N) ?? if yes how??
if not so what would be optimized approach.

posted Feb 3, 2014 by Atiqur Rahman

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

+1 vote

take another array of structure{value,loc}, store initial array's elements in value part of the new array and index as location. Now sort this new array according to value. now scan this new array and check if
arr[i].value < arr[i+1].value < arr[i+2].value and arr[i].loc < arr[i+1].loc < arr[i+2].loc. Print arr[i].value, arr[i+1].value, arr[i+2].value if previous check is true.

answer Feb 4, 2014 by anonymous
Similar Questions
+5 votes

Given an array A of unsigned 32-bit ints, choose two in-bounds indices i, j so as to maximize the value of A[i] ^ A[j], where ^ is the bitwise XOR (exclusive OR) operator. If there are multiple possible answers, print any one.

Example Input:

4 2 0 13 49 

Output:

3 4 

Explanation: 13 ^ 49 is 60, 13 and 49 are positioned at indexes 3 and 4. Printing "4 3" would have been equally valid.
EDIT:- Let k be the bit width (maximum number of bits) of the primitive.
Constraint:- Only Sub-quadratic complexity. An O(k*arr.length) time.

+3 votes

In an "N" element integer sorted array, a particular elements is repeating "(N/2)+1" times. How much time it take to find the repeating element.

+3 votes

Input: arr[] = {5, 5, 10, -10, -20, 40, 50, 35, -20, -50}
Output: 125 (40, 50, 35)

+5 votes

Given an unsorted array, find the max length of subsequence in which the numbers are in incremental order.

For example: If the input array is {7, 2, 3, 1, 5, 8, 9, 6}, a subsequence with the most numbers in incremental order is {2, 3, 5, 8, 9} and the expected output is 5.

...