The counting inversion can be done in O(n^2) with the naive algorithm.
But it will not suffice. So we need (nlogn). Counting the inversion in merge function of merge sort is good idea too do this. while merging if second array is having small element then the elements remaning in first array all have conflict with this element so add that and thats it.
or
create binary search tree ,step by step go on inserting the elememnts, and with every node keep track of nodes that are right side of current node and while inserting increment the current nodes right count if inserted element falls to right (that is it is greater than current node element).and else if falls to left(that means inserted element is smaller than current node and there are right_count_of_current_node no of element greater than inserted element that are before inserted element so add that count) add right count to answer.
The 2D range sum DP problem recurrence relation same as 836 only change is we have to consider submatrix wih max 1's and (not maxsum) have a look at condition on line no 106
This is problem of Complete search.Though problem statement is not clear it can be understood with the test case.It states to find no of winning combination including particular party.
Search all combinations
This is easy backtracking problem which can be solved in O(n*d^2) complexity.
But this could also solved in 1025^2 using dp using range sum.
take input and make entries in dp[1025][1025];
Preproccess the 1025x1025 array using :
dp[i][j]=dp[i-1][j]+dp[i][j-1]-dp[i-1][j-1]+dp[i][j];
then,
use recurrence relation to find max
dp[i+d][j+d]-dp[i+d][j-d-1]-dp[i-d-1][j+d]+dp[i-d-1][j-d-1]; for all (i,j) between d+1 to 1025-d
This is same as Knapsack 0/1 (maximize profit with using time < given amount of time)
This problem can be considered as little bit harder because reconstruction of solution is also required here for that purpose we are going to have 2-D(number of element as rows and time as column) boolean array which tells us that whether we have taken element with current row ndex.
This is easy problem but there is one catch multiple winning are allowed(as it is not mentioned in problem) , i tried both ways and got AC on commenting that condition (line : 121 in following code)
This is Dynamic Programming problem.
It is coin change problem , form table with dp.
and find i,j such that i*i+j*j=s*s using brute force
and among all i,j find minimum coins using table values