Skip to main content

Posts

Showing posts from July, 2018

finding sum:

finding sum:  Problem Description You are given a set of N positive integers and another integer P, where P is a small prime. You need to write a program to count the number of subsets of size (cardinality) 3, the sum of whose elements is divisible by P. Since the number K of such sets can be huge, output K modulo 10007 1009 (the remainder when K is divided by 1009) Constraints N <= 500 P<50 All integers <=1000 Input Format First line two comma separated integers N, P The next line contains N comma separated integers Output One integer giving the number of subsets the sum of whose elements is divisible by P. Give result modulo 1009 Explanation Example 1 Input 4,5 5,10,15,20 Output 4 Explanation Every non empty subset of the given numbers has sum of its elements a multiple of 5. Since there are 4 subsets of size 3, the output is 4. Example 2 Input 5,5 3,7,12,13,15 Output 4 Explanation ...

Count Bits Problem

Count Bits Problem Description Given a sequence of distinct numbers a1, a2, ….. an, an inversion occurs if there are indices i<j such that ai > aj . For example, in the sequence 2 1 4 3 there are 2 inversions (2 1) and (4 3). The input will be a main sequence of N positive integers. From this sequence, a Derived derived sequence will be obtained using the following rule. The output is the number of inversions in the derived sequence. Rule for forming derived sequence The derived sequence is formed by counting the number of 1s bits in the binary representation of the corresponding number in the input sequence. Thus, if the input is 3,4,8, the binary representation of the numbers are 11,100 and 1000. The derived sequence is the number of 1sin the binary representation of the numbers in the input sequence, and is 2,1,1  Solution def getInvCount(arr, n):      inv_count = 0      for i in range (n):  ...

Array Product Problem:

Array Product Array Product Problem Description You are given a list of N integers and another positive integer K. Write a program to compute the number of ways in which the product P of the given N integers can be expressed as a product of K positive integers (not necessarily distinct). The order of the factors in the expression is not important. For example, 1 x 2 x 3 and 2 x 3 x 1 are not counted as different ways of expressing 6 as a product of three integers. Constraints The product of the N integers <= 10^9 Each of the N integers <=5000 Input Format Output One line containing the number of ways in which the product of the N integers can be expressed as a product of K positive integers Explanation Example 1  Input   2 4  2 3  Output   2  Explanation The product of the given integers is 6. This can be expressed as a product of 4 integers in 2 ways: 1x1x1x6, 1x1x2x3  Example 2  Input  ...