Friday, 12 February 2016

Triplet Search

Given an array of unique numbers and a value, a valid triplet is a set of three numbers (not necessarily continuous in the array) that add up to the value. Write a program to count all valid triplets. Print the number of  valid triplets. Hint: Can you solve this in O(n^2) steps?

Input : First line contains N and Sum (Space separated). Second line contains space separated list of N numbers.
Output: Number of triplets such that  sum possible

Constraints:
  • 0 < N < 100
  • All numbers in the list are unique and between -10^4 to 10^4
  • -10^3 < Sum < 10^3


Solution -:

Evaluating polynomials

You are given a polynomial of degree n. The polynomial is of the form P(x) = anxn + an-1xn-1 + … + a0. For given values k and m, You are required to find P(k) at the end of the mth iteration of Horner’s rule. The steps involved in the Horner’s rule are given below,
Pn (x) = an
Pn-1 (x) = an-1 + x * Pn (x)                              1st iteration.
Pn-2 (x) = an-2 + x * Pn-1 (x)                           2nd iteration.
.
.
P0 (x) = a0 + x * P1 (x)                                  nth iteration.
In general, Pi (x) = ai + x * Pi + 1 (x) and P0(x) is the final result.
Input
n m k an an-1 …. a0
(Space separated)
Output: P(k) value at the end of the mth iteration.
Sample Input:
2 2 5
3 2 1
Sample Output:
86
Constraints:
1 <= n, k, m <= 10
0 <= ai <=10


Solution -:

Selection Sort

Selection sort is a sorting algorithm that sorts n elements in an array using n-1 calls of a procedure called Select() . Select(i) selects the smallest of the elements from positions i+1 to n and swaps with the element at i. Given a list of unsorted elements having N numbers, print the array of elements after K calls to Select() starting with Select(1) as the first call and Select(K) as the last call with increasing order of parameters to Select().

Input:
N K m1 m2 m3 .. mN (Space separated) (where mi's are elements of unsorted list)
Output:
Space separated list of N numbers after K calls to of Select().

Constraint:
2<= N <= 100
1<= K <= (N-1)
Numbers will be given in the range [-1000,1000].

Example 1:
5
1
9 8 7 6 5

Expected Output:
5 8 7 6 9

Thursday, 11 February 2016

Closest Numbers

You are given a sorted list of N numbers and a number Num. Write a program to find the five numbers that are closest (numerically) to Num. A number x in the array is closest to Num if |Num-x| is the smallest among all such x possible.

Note: If the Num is present in given list, then it should be in the output.

Constraints:
        5 < N <20
        All numbers in list are between -10000 to 10000
        -10000 <Num< 10000
        There could be numbers that are repeated
Input:      
    N Num
    m1 m2 m3 .. mN
where mi's are N numbers in sorted order.

Output:
    p1 p2 p3 p4 p5
where pi's are closest numbers in increasing order of absolute difference from Num. If there are two numbers with same difference, print the larger one first.


Solution-
#include<stdio.h>

Monday, 8 February 2016

Solve the Mystery

Solve the mystery.

Input :
First line contains T - No. of test cases.
For each test case there are two lines. 
first line contains N. 
Second line contains N space separated integers A[1] to A[N].

Output :
Print answer of the mystery in separate lines.

Constraints : 
1<=T<=10 
1<= N <= 10^5 
0<= A[i] <= 10^5