Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed last month.
The community reviewed whether to reopen this question last month and left it closed:
Original close reason(s) were not resolved
Improve this question
You are given π integers π1,π2,β¦,ππ. Find the maximum value of πππ₯(ππ,ππ+1,β¦,ππ)β
πππ(ππ,ππ+1,β¦,ππ) over all pairs (π,π) of integers for which 1β€π<πβ€π.
Input
The first line contains a single integer π‘t (1β€π‘β€10000) β the number of test cases.
The first line of each test case contains a single integer π (2β€πβ€105).
The second line of each test case contains π integers π1,π2,β¦,ππ (1β€ππβ€106).
It is guaranteed that the sum of π over all test cases doesn't exceed 3β
105.
Output
For each test case, print a single integer β the maximum possible value of the product from the statement.
Example
input
4
3
2 4 3
4
3 2 3 1
2
69 69
6
719313 273225 402638 473783 804745 323328
output
12
6
4761
381274500335
Let's say you have some input where the best answer is X. Among all ranges that achieve X, let R be the smallest. Then the min and max of the range must be at the endpoints, otherwise a smaller range would also yield X.
However, if R has more than 2 elements then it has some central elements which must be greater than the min of R. Shrinking R from the endpoint which has the min of the range would give us R' which has a larger min and the same max, so would yield X' > X, a contradiction.
Thus you only need to consider ranges of size 2.
tl;dr: Take the max product of adjacent members of the input.
2 [4 3]: 4*3 = 12
[3 2] 3 1: 3*2 = 6
[69 69]: 69*69 = 4761
719313 273225 402638 [473783 804745] 323328: 473783*804745 = 381274500335
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 months ago.
Improve this question
A MEX (Minimum Excluded) is the minimum non-negative integer that is excluded from the collection/list.
Eg :
MEX [] = 0
MEX [1,2,3,4,5,10,10000] = 0
MEX [0,1,2,3,4,5,6] = 7
MEX [0,1,3,4,1000] = 2
MEX [0,2,3,4,5,6] =1
Given a list of non negative integers, find the MEX of the list.
So, I tried sorting the array and then comparing the number at each position with its index to find the minimum number which is missing. The time complexity of this approach is O(nlogn + n). I am looking for a more optimised solution!
It can be done in linear time and linear space. You have to first realise that the result is bound by n.
So, create a lookup table (a bitset will do) with at most n+1 entries and then iterate over the input array. For each number < n flip the entry in your table to 1. The first value not 1 will by your result. By definition at least one bit has to be not 1.
Without sort-:
lis=set([0,0,2,3,1,8,4,34,12,99,100])
for i in range(0,max(lis)+2):
if i not in lis:
print(i)
break
Using Dictionary [Hashmap]-:
The (amortized) time complexity is constant (O(1)) in the size of the dictionary
lis=set([0,2,3,1,8,4,99,100])
dic={}
#for i in lis:
# dic[i]=dic.get(i,0)+1
dic=dict.fromkeys(lis, 1) # one-liner to initialize by #_aneroid
for i in range(max(lis)+1):
if i not in dic:
print(i)
break
Updated Solution:-
def solution(list_1):
if not list_1:
return 0
#Condition for all elements present
# Sum of natural numbers n*(n+1)//2 -: whole number (n-1)*n//2
n=len(list_1)-1
if sum(list_1)==(n*(n+1))//2:
return max(list_1)+1
dic=dict.fromkeys(list_1, 1)
for num in range(max(list_1)+1):
if num not in dic:
return num
break
print(solution([]))
print(solution([0,1,2,3]))
print(solution([1,2,54,32,21,46,32,0,29,88]))
print(solution(list(map(int,input().split())))) # If user want to input
Output:-
0
4
3
as user defined list..!
I am developing a probability analysis program for a board game. As part of an algorithm* I need to calculate the possible permutations of partitions of a number (plus some padding), such that all partition components cannot occupy any position that is lower than the total length of the permutation, in digits, minus the value of the component.
(It is extremely unlikely, however, that the number that will be partitioned will ever be higher than 8, and the length of the permutations will never be higher than 7.)
For instance, say I have the partition of 4, "211", and I want to find the permutations when there is a padding of 2, i.e. length of 5:
0 1 2 3 4 (array indexes)
5 4 3 2 1 (maximum value of partition component that can be allocated to each index)
2 1 1 _ _ (the partition plus 2 empty indexes)
This is represented as an array like so {2,1,1,0,0}
There are 6 permutations when 2 is in the 0 index (4! / 2! 2!), and there are 4 indexes that 2 can occupy (2 cannot be placed into the last index) so overall there are 24 permutations for this case (a 1 can occupy any index).
The output for input "21100":
21100, 21010, 21001, 20110, 20101, 20011
02110, 02101, 02011, 12100, 12010, 12001
00211, 10210, 11200, 10201, 01210, 01201
10021, 01021, 00121, 11020, 10120
01120
Note that this is simply the set of all permutations of "21100" minus those where 2 is in the 4th index. This is a relatively simple case.
The problem can be described as combining n different permutation groups, as the above case can be expressed as the combining of the permutations of x=1 n=4 and those of x=2 n=5, where x is the value count and n is the "space" count.
My difficulty is formulating a method that can obtain all possibilities computationally, and any advice would be greatly appreciated. -Please excuse any muddling of terminology in my question.
*The algorithm answers the following question:
There is a set of n units that are attacked k times. Each
attack has p chance to miss and q (1 - p) chance to damage
a random unit from the set. A unit that is damaged for a second time is destroyed
and is removed from the set.
What is the probability of there being
x undamaged units, y damaged units and z destroyed units after the attacks?
If anyone knows a more direct approach to this problem, please let me know.
I am going to use the algorithm to generate all permutations of the multiset as given in the answer to this question
How to generate all the permutations of a multiset?
and then filter to fit my criteria.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
A friend of mine has had a programming test yesterday. He passed the test anyway, but I'm curious about the answer.
Here is the test, supposed you are on point A and need to go to point B. You can only move up and right. How many possible movements do you have?
The options he remembered were 16, 64, 3125.
What is the answer and how to explain that?
Rotate your picture. Your problem consists in finding the number of paths from a particular vertex in the Pascal graph to the root vertex. Labelling the vertices at level n by the integers 0, 1, ..., n, then the number of paths from vertex k at level n is the binomial coefficient "n choose k". In your case this is vertex k=4 at level n=8, and "n choose k"=70.
I disagree with the commentators that this question has nothing to do with programming. It can be solved fairly easily with a simple recursive function:
def n_ways(x, y):
if x == 0 or y == 0:
# we are on the "edge", there is only one way to get there
return 1
else:
# the number of ways to get here equals the sum of the number of
# ways to get to the point directly below and the number of ways
# to get to the point directly left
return n_ways(x-1, y) + n_ways(x, y-1)
Let's try it out. Point B is x position 4, y position 4.
>>> n_ways(x=4, y=4)
70
We can also use our algorithm to generate the diagram below:
>>> for y in reversed(range(5)):
... for x in range(5):
... n = n_ways(x, y)
... print str(n).rjust(2),
... print
...
1 5 15 35 70
1 4 10 20 35
1 3 6 10 15
1 2 3 4 5
1 1 1 1 1
As StΓ©phane Laurent has already mentioned, you can easily find Pascal's Triangle hiding in this algorithm! :)
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
For any non-negative integer K, suppose we have exactly two coins of value 2^K (i.e., two to the power of K).
Now we are given a long N. We need to find the number of different ways we can represent the value N with coins that we have.
(Two representations are considered different if there is a value that occurs a different number of times in the representations.)
Example : Let N=6 then answer is 3 as the following three representations are possible in this case:
{1, 1, 2, 2}
{1, 1, 4} and
{2, 4}
How to do it if N can be upto 10^18?
We can start with the trivial solution where we have one 2 and the rest 1. This of course requires N to be at least 3. Otherwise, there is no solution:
coins1 = N - 2
coins2 = 1
nSolutions = 1
Let's first discuss the task of finding the number of possible merge operations of M equal coins where each and every coin must be merged (i.e. only a single coin type is used). This is the number of possible integer divisions of M by 2. This procedure can cache intermediate results to perform subsequent calls faster (see dynamic programming).
But how is this useful? In the current state we can now subsequently exchange two 1's for one 2. This can be done until there are no more two 1's, a total of floor(N/2 - 1) times. After each merge (actually after each second merge is sufficient), we have to check how often all 2's can be merged. There are still 1's in the state, so we can't use additional coin types:
while(coins1 >= 3)
{
coins1 -= 2;
coins2 += 1;
nSolutions += 1;
nSolutions += findNumberOfMerges(coins2); //this could be improved
}
If the while is left, there are either one or two 1's left. If there is one left, we are done because this one must always be there and we have checked all possible combinations to represent the 2's in a different way.
if(coins1 == 1)
return nSolutions;
In the other case, we can merge once more. However, this will not result in a valid state because there is no second coin type. But we can merge once more to get a valid state (one 4 (becomes coins2) and the rest 2's (becomes coins1)):
coins1 = coins2 + 1 - 2;
coins2 = 1;
nSolutions += 1;
Now we have a similar state as at the beginning, so we can run the whole procedure again. And again, and again until it returns.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 6 years ago.
Improve this question
I am at learning stage of cyclomatic complexity(CC). For practise, I am calculating cyclomatic complexity of 2 examples and want to confirm if my answers are correct or not...
Referring to wikipedia, CC is given by M = E β N + 2P where:
E = the number of edges of the graph
N = the number of nodes of the graph
P = the number of connected components
Please help.
Here, E = 8, N = 9 and P = 1. Hence M = 8 - 9 + (2x1) = 1.
Example 2:
Here E = 11, N = 10 and P = 1. Hence M = 10 - 11 + (2x1) = 1.
Hence for both the examples CC is 1. Please let me know if my calculation is correct or not.
You need to take more care to correctly insert the values into the formula.
In example 1, you say
Here, E = 8, N = 9 and P = 1
But actually, it's the other way round: 9 edges (=E), 8 nodes (=N), so you get a CC of 3.
In example 2, you have the values right: E=11, N=10, P=1. But you insert them in the wrong order in the formula; it actually should be 11 - 10 + (2x1) = 3.
Shortcut: If you have a picture of your graph, you can very easily determine the cyclomatic complexity. Just count the number of regions the background is divided into by the edges. In your first example, you have 2 inner regions (bordered by the edges) and one surrounding region, giving a CC of 3. Same goes with the second example. (This method requires that edges are not crossing each other, obviously.)
Also if this helps, it the number of conditional (If, while, for) statements +1. So in the above example, there are 2 conditional statements . so 2+1=3. Cyclomatic complexity in this case is 3
Just count the number of closed region and add 1 to it.
In your example above, number of closed region = 2, so the CC = 2+1 = 3
P = the number of connected components
IN OTHER WORDS
P = the number of nodes that have exit points
Source