Perfect Sum Problem with repetitions allowed - arrays

Given an array of integers and a sum, the task is to print all subsets of given array with sum equal to given sum with repetitions allowed.
Examples :
Input : arr = {1, 5, 6}, N = 7
Output :
1 1 1 1 1 1 1
1 1 5
1 5 1
5 1 1
1 6
6 1
I have already gone through related DP questions from https://www.geeksforgeeks.org/perfect-sum-problem-print-subsets-given-sum/ , https://www.geeksforgeeks.org/ways-sum-n-using-array-elements-repetition-allowed/ and find all subsets that sum to a particular value
I haven't found a way or any clues on how to solve this question with repetitions allowed. Any leads would be helpful.

Something like this?
function f(A, N, r=[], s=N){
if (s == 0)
return [r];
result = [];
for (let a of A)
if (a <= s)
result = result.concat(
f(A, N, r.slice().concat(a), s-a));
return result;
}
console.log(JSON.stringify(f([1,5,6], 7)));

Here is a simple solution in Java:
public class PrintAllSubsets {
public static void main(String[] args) {
int arr []= {1, 5, 6};
int N = 7;
allSubsets(arr,N,"");
}
private static void allSubsets(int[] arr, int n, String res) {
if(n == 0) {
System.out.println(res);
return;
}
for(int i = 0; i<arr.length;i++) {
if(n >= arr[i]) {
allSubsets(arr, n-arr[i], res + arr[i]);
}
}
}
}

Sort the input array (if you can) and then use https://en.wikipedia.org/wiki/Backtracking to get all possible solutions. Just go from the lowests number and if it cannot fit, just start returning and check if number one-item higher (in input array) can fit instead.

Related

Using only if-else statements to find specific element in an array with length n and elements from 0 to n-1

I'm stuck in solving an interview question. The goal is to find a specific element from an array with unknown length (cannot use .length) and return the number of steps, but for an array with a length of n, the elements are guaranteed to be from 0 to n-1, no duplicates. For example, if the array's length is 5, the elements are {0, 1, 2, 3, 4} but the order may be different. Additional requirements are no loops, no static/global variables, and no helper functions, and the only parameters passing in are the array int[] arr and the target value int x, no extra parameters allowed, the array remains the same after all the operations have done.
//So you can only write in the body of the following method, no outside variables nor methods could be used.
private int findElement (int[] arr, int x) {
}
What I have gotten so far is, since the elements are guaranteed to be 0 to n-1, I can use the target number as an index and go back to the array to see if the number arr[x] equals the number x I want. If not, I take that number arr[x] and make it my new index, repeating until I find the target value.
int[] arr = {4, 1, 0, 2, 3}
int target = 3;
arr[3] = 2; //take target as the initial index
arr[2] = 0;
arr[0] = 4;
arr[4] = 3; //we got the number we want
//steps total is 3 since the question says the first time doesn't count.
Question: I tried to solve this by recursion, but since I am always comparing the following values with the initial parameter value, in the above case I always wanted to find 3. So how to store that information without static variables or extra parameters is my bigges problem. Is there any other way I can store the initial parameter value and pass it through the whole process?
private int findElement(int [] arr, int x) {
int actualN = arr[x];
if (actualN == **???**) { //can't be x cuz x is changing but I always want 3
return 0;
} else {
return findElement(arr, arr[x]) + 1;
}
}
Preferably using Java
Any hints or help would be greatly appreciated.
Probably this should work:
private int findElement(int [] arr, int x) {
int currValue = arr[x], returnValue;
if(arr[x]>0)
arr[x] = 0;//setting the actual element of the array to 0
else
arr[x]--;// decrementing the search index so it goes from 0-> -1 -> -2 -> -3...
if(Math.abs(arr[-arr[x]]) == x)//We check if the number is at our search index...
returnValue = 0;
else
returnValue = findElement(arr, x)+1;
arr[x] = currValue;//We take the value of the index from when the function was called and then reassign it to the same index after our work with it is done.
return returnValue;
}
Since the array only has to be the same after execution and it doesn't matter it's state during execution, this may work.
Note: I haven't done elaborate test on this so please do test the code sometimes before submitting
You were almost there
// t is the target number, o is teh array offset
static int find(int [] arr, int t, int o) {
if (arr[o] == t)
return o;
return find(arr, t, o + 1);
}
and
static void Main(string[] args) {
int[] arr = { 4, 1, 0, 2, 3 };
int target = 3;
int x = find(arr, 3, 0);
}
if only 2 args allowed - I missed that
in c
static int* find(int* arr, int t) {
if (*arr == t)
return arr;
return find(arr + 1, t);
}
int main() {
int arr[] = {4, 1, 0, 2, 3};
int target = 2;
int x = find(arr, target) - arr;
}
in c#
static unsafe int* find(int * arr, int t) {
if (*arr == t)
return arr;
return find(arr + 1,t);
}
static void Main(string[] args) {
int[] arr = { 4, 1, 0, 2, 3 };
int target = 3;
unsafe {
fixed (int * p = &arr[0]) {
int x = (int)(find(p, target) - p);
}
}
}
I have assumed arr can be modified, provide it is unchanged after the answer has been obtained.
Since it is only "preferable" that the answer be in Java (and I don't know Java), I'll offer a solution in Ruby. With its pseudo-code appearance and added comments readers unfamiliar with Ruby should be able to follow the calculations.
Specifically, I append an element to the given array which equals the index of the current element of the array to be examined (initially zero). If that element equals the target value we return up the recursion chain, initially returning zero, then adding one at each subsequent point of the chain. Before returning the desired count in doit, the last element of the array is removed to restore the array to its initial value.
If the value of the array indexed by the last element of the array (the current index) does not equal the target value the last element of the array is incremented by one and the method is called recursively.
def doit(arr,target)
arr << 0 # append the index 0 to arr
n = recurse(arr, target)
arr.pop # remove the last element of arr
n
end
def recurse(arr, target)
return 0 if arr[arr[-1]] == target
arr[-1] += 1 # increment last value of arr by 1
1 + recurse(arr, target)
end
arr = [4, 1, 0, 2, 3]
doit(arr, 4) #=> 0
doit(arr, 1) #=> 1
doit(arr, 0) #=> 2
doit(arr, 2) #=> 3
doit(arr, 3) #=> 4

how to avoid a return statement in recursion

This program is for printing sum of all the numbers in the array given as input parameter. However this does not happen. Please let me know what is the mistake and provide me the solution with explanation.
namespace linkedLists
{
class Program
{
static void Main(string[] args)
{
int[] arr = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
int x = sumOfNum(arr, 0);
Console.WriteLine(x);
Console.ReadLine();
}
public static int sumOfNum(int[] arr, int x)
{
int[] arr_new = new int[arr.Length - 1];
if (arr.Length > 1)
{
x += arr[arr.Length - 1];
for (int i = 0; i < arr.Length - 1; i++)
{
arr_new[i] = arr[i];
}
}
if (arr.Length > 1)
{
sumOfNum(arr_new, x);
}
return x;
}
}
}
Your question's title is "how to avoid a return statement in recursion", which is exactly what you should be doing instead of avoiding in a recursion scenario.
But this is not the only problem with your code, because it does nothing like what you described it is supposed to do.
As already mentioned in the comments, this is not something that requires (or for which it is recommended) to use a recursive approach. It can be done, but is inefficient and could lead to a stack overflow if you have a large input array (it needs a new stack frame for each recursive method call in c#).
To solve this recursively, you need to try and state the problem as a recursive problem, before you start trying to code it. In pseudo code, for an input array x of size n:
array_sum(x):
if (x is empty)
return 0;
else
return x[0] + array_sum(x[1:n-1])
An implementation in C# would try to avoid allocating a new array instance (as opposed to what one of the non-functioning parts in the code of your question is doing), and instead keep track of the start index into the input array:
public static array_sum(int startIndex, int[] x)
{
// ...
}
If you want to know the answer without the recursion you just iterate over it while summing the numbers.
If you want to make a recursive solution the return is essential. Without it the recursion is just a different way of making a for loop.
public static int sumOfElements(int[] arr, int currentIndex, int accumulator)
{
if( finished-expression )
return accumulator;
else
return sumOfElements(...);
}
public static int sumOfElements(int[] arr)
{
return sumOfelements(arr, 0, 0);
}
Your mistake is that x is a different one at each iteration. You don't use the returned value from the recursion so you have the sum of one element as the result.
Agree with the previous comments about recursion, inefficiency, and complexity but as an exercise below is the minimal number changes to get the code to work.
Changes:
Added array size guard
Moved sum out of if-block
Added return statement
Code:
namespace linkedLists
{
class Program
{
static void Main(string[] args)
{
int[] arr = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
int x = sumOfNum(arr, 0);
Console.WriteLine(x);
Console.ReadLine();
}
public static int sumOfNum(int[] arr, int x)
{
if (arr.Length == 0) return x; // Change #1
int[] arr_new = new int[arr.Length - 1];
x += arr[arr.Length - 1]; // Change #2
if (arr.Length > 1)
{
for (int i = 0; i < arr.Length - 1; i++)
{
arr_new[i] = arr[i];
}
}
if (arr.Length > 1)
{
return sumOfNum(arr_new, x); // Change #3
}
return x;
}
}
}
Assuming that you are required to use recursion as an exercise I will comment no further on that.
The reason your programme does not work appears to be that when the array has only one element, your do not add that element to the subtotal (that you call x) before returning it.
While recursion is somewhat expensive, your program uses roughly ½arr.Length² ints, which would need a terabyte of memory in the unlikely event that you applied it to an array of length 500,000! So you would be far better not to copy the array.

What is wrong with my recursion in C?

I have written a recursive function for my homework to do the following calculation:
For the imput:
1 2 3 4
It should do this:
((1*3)+2) + ((1*4)+3) = 13, thats less than, ((2*4)+3) + ((1*4)+2) = 17, so it returns 13.
In letters it should do this calculation: ((A*C)+B) + ((A*D)+C) and compare it with the other options, in this case there are 2 options: ((B*D)+C) + ((A*D)+C).
In few words. The numbers indicate the number of "screws" on each end of a segment. The segment is always formed by 2 numbers. Segment A {1 2}, B {2 3}, C {3 4}.
The task is to join all the N segments. I must find the "cheapest" way to do it. Every time I join two segments, (A and B for example), I do this:
"bottom screws"of A (1 - the first number) * "top screws"of B (3 - the third number) + "joining screws" (2 - that is the number between).
I have to join them in order, it always must end in order ABCD. But I can choose where to start from. I can join A to B and then AB to C, or i can join B to C and then A to BC. Basically in one of the cases the "cost" will be the lowest and thats the value to return.
For now I have done this, but I got confused:
The *help is a intercalculation array which i use to store the new values gotten in the recursion.
int *help;
The *mezi is a dynamically alocated array defined as:
int *mezi;
And inside it looks like {0,4,1,2,3,4,-1}.
mezi[0] = here is stored the total prize in the recursion.
mezi[1] = here is stored the number of values in the array, 4 for 4 values (3 segments).
mezi[n+2] = the last number (-1), its just an identifier to find out the number of values.
Here's my code:
int findmin(int *mezi, int *pomocny)
{
int i,j,k;
int prize, prizemin, mini, minih;
for (i=3;i<mezi[1];i++) {
prize = mezi[i-1] * mezi[i+1] + mezi[i];
if (i==3) { mini = i; minih = prize; }
if (prize < minih) { mini = i; minih = prize; }
if (mezi[1] > 3){
k=2;
for (j=2;j<mezi[1];j++) {
if (j != mini) help[k] = mezi[j];
k++;
}
help[1] = (mezi[1]-1);
}
help[0] += prize;
findmin(help,help);
}
prizemin = help[0];
return prizemin;
}
Im kinda of a novice, I started using C not long ago and the recursive functions cofuse me a lot. I would reallz appretiate help. Thanks :)
There are quite a few issues with your program logic.
int findmin(int *mezi, int *pomocny)
{
int i,j,k;
int prize, prizemin, mini, minih;
for (i=3;i<mezi[1];i++)
{
prize = mezi[i-1] * mezi[i+1] + mezi[i];
if (i==3) { mini = i; minih = prize; } //This is a redundant test and
//initialization. i == 3 is true only in the first run of the loop. These
//initialization should be done in the loop itself
if (prize < minih) { mini = i; minih = prize; }
if (mezi[1] > 3){ //This is also redundant as mezi[3] is always greater than 3
// otherwise the loop wont run as you check for this in your test expression
k=2;
for (j=2;j<mezi[1];j++) {
if (j != mini) help[k] = mezi[j];
k++;
}
help[1] = (mezi[1]-1);
}
help[0] += prize;
//The only base case test you have is mezi[1]<3 which you should make sure is
// present in your data set
findmin(help,help);
}
prizemin = help[0];
return prizemin;
}

Given a set S, find all the maximal subsets whose sum <= k

This is a Facebook interview question I came across at an online portal.
Given a set S, find all the maximal subsets whose sum <= k. For example, if S = {1, 2, 3, 4, 5} and k = 7
Output is: {1, 2, 3} {1, 2, 4} {1, 5} {2, 5} {3, 4}
Hints:
Output doesn't contain any set which is a subset of other.
If X = {1, 2, 3} is one of the solution then all the subsets of X {1} {2} {3} {1, 2} {1, 3} {2, 3} are omitted.
Lexicographic ordering may be used to solve it.
Any ideas how could this be solved?
I have some idea - you need a tree.
If you have given input of {1, 2, 3, 4, 5}, and you're searching for maximal subsets - you should build a tree starting from the biggest numbers, and allways expand while sum <= k (so don't stop on 4-2, but go down to 1 to get 4-2-1).
So, nodes starting from 5 would be: 5-1 / 5-2 - only those 2 have sum <= 7
starting from 4: 4-3 / 4-2-1 / 4-1 (subset of previous)
starting from 3: 3-2-1 / 3-1 (subset of previous)
starting from 2: 2-1 (subset of 3-2-1)
starting from 1: 1 (subset of 2-1)
Then you can sort valid outputs and get {1, 2, 3} {1, 2, 4} {1, 5} {2, 5} {3, 4}
I know it's late to answer, but I think I've found a simple solution for this problem. We enumerate subsets of S in lexicographical order using backtracking and check the sum of subset generated so far.
When the sum exceeds k, the interesting part comes: we need to check if the generated subset is a proper subset of previously reported items.
One solution is to keep all the reported subsets and check for inclusion, but it's wasteful.
Instead, we calculate the difference between the k and the sum. If there is an element e in S such that e not in subset and e <= (k - sum), then the set we generated is a proper subset of a previously reported subset, and we can safely skip it.
Here is the complete working program in plain old C++, demonstrating the idea:
#include <iostream>
#include <vector>
#include <set>
#include <algorithm>
typedef std::set<int> Set;
typedef std::vector<int> SubSet;
bool seen_before(const Set &universe, const SubSet &subset, int diff) {
Set::const_iterator i = std::mismatch(universe.begin(), universe.end(),
subset.begin()).first;
return i != universe.end() && *i <= diff;
}
void process(const SubSet &subset) {
if (subset.empty()) {
std::cout << "{}\n";
return;
}
std::cout << "{" << subset.front();
for (SubSet::const_iterator i = subset.begin() + 1, e = subset.end();
i != e; ++i) {
std::cout << ", " << *i;
}
std::cout << "}\n";
}
void generate_max_subsets_rec(const Set &universe, SubSet &subset,
long sum, long k) {
Set::const_iterator i = subset.empty()
? universe.begin()
: universe.upper_bound(subset.back()),
e = universe.end();
if (i == e) {
if (!seen_before(universe, subset, k - sum))
process(subset);
return;
}
for (; i != e; ++i) {
long new_sum = sum + *i;
if (new_sum > k) {
if (!seen_before(universe, subset, int(k - sum)))
process(subset);
return;
} else {
subset.push_back(*i);
if (new_sum == k)
process(subset);
else
generate_max_subsets_rec(universe, subset, new_sum, k);
subset.pop_back();
}
}
}
void generate_max_subsets(const Set &universe, long k) {
SubSet subset;
subset.reserve(universe.size());
generate_max_subsets_rec(universe, subset, 0, k);
}
int main() {
int items[] = {1, 2, 3, 4, 5};
Set u(items, items + (sizeof items / sizeof items[0]));
generate_max_subsets(u, 7);
return 0;
}
The output is all maximum subsets in lexicographical order, one per line:
{1, 2, 3}
{1, 2, 4}
{1, 5}
{2, 5}
{3, 4}
This is a powerset problem. Recently I found this website about algorithms and it's been painting my imagination: hence the powerset/combinations solution following. You can simply copy, paste, and run the program.
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Solution {
public static void maximalSubset
(int sum, int[] set, int choose,List<Integer[]> exclusion) {
if(1>choose) return;
int combinationSize = combinationSize(set.length,choose);
int index[]=new int[choose];
Integer subSet[] = new Integer[choose];
for(int i=0; i<choose;i++)
index[i]=i;
for(int i=0; i<combinationSize; i++) {
if(i!=0)
nextCombination(index,set.length);
for(int x=0; x<choose; x++)
subSet[x]=set[index[x]];
if(summation(sum,subSet) && !excluded(subSet,exclusion)) {
System.out.println(Arrays.toString(subSet));
exclusion.add(Arrays.copyOf(subSet,subSet.length));
}
}
maximalSubset(sum,set,choose-1,exclusion);
}//
private static int combinationSize(int n, int r) {
int den,limit;
if(r>n-r) {
den=n-r;
limit=r;
}else {
den=r;
limit=n-r;
}
long result=1;
for(int i=n; i>limit;i--)
result*=i;
for(int i=2; i<=den;i++)
result/=i;
return (int)result;
}//
private static void nextCombination(int[] A, int n) {
int c=A.length;
int i=c-1;
while(n-c+i==A[i])
i--;
A[i]++;
for(int j=i; j<c; j++)
A[j]=A[i]+j-i;
}//
private static boolean summation(int sum, Integer[] S) {
for(int i:S)
sum-=i;
return sum>=0;
}//
private static boolean excluded(Integer[] needle,List<Integer[]> haystack) {
for(Integer[] H: haystack) {
int count=0;
for(int h: H)
for(int n:needle)
if(h==n) {
count++;
break;//it's a set
}
if(count==needle.length)
return true;
}
return false;
}//
public static void main(String[] args) {
int[] S = {1, 2, 3, 4, 5};
int k = 7;
List<Integer[]> exclusion = new ArrayList<Integer[]>();
maximalSubset(k,S,S.length,exclusion);
}
}
An old question but still an interesting one.
Here's a recursive Java 8 solution, with a "permutational" approach.
Optimized for cleaner and shorter code rather than performance -- for example the sorting and pruning would only need to take place once.
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Ordering;
import java.util.*;
import java.util.stream.Collectors;
public class SubsetFinder {
public List<List<Integer>> findSubsets(List<Integer> input, int k) {
List<List<Integer>> listOfLists = new ArrayList<>();
List<Integer> copy = Ordering.natural().sortedCopy(input);
while (!copy.isEmpty()) {
int v = copy.remove(copy.size() - 1);
if (v == k || (copy.isEmpty() && v <= k)) {
// No need to look for subsets if the element itself == k, or
// if it's the last remaining element and <= k.
listOfLists.add(new ArrayList<>(Arrays.asList(v)));
} else if (v < k) {
findSubsets(copy, k - v).forEach(subList -> {
subList.add(v);
listOfLists.add(subList);
});
}
}
// Prune sets which are duplicates or subsets of other sets
return listOfLists.stream().filter(
candidate -> listOfLists.stream().noneMatch(
lol -> candidate != lol && lol.containsAll(candidate)
)
).collect(Collectors.toList());
}
}
To test it:
public static void main(String[] args) {
new SubsetFinder()
.findSubsets(ImmutableList.of(1, 2, 3, 4, 5), 7)
.forEach(System.out::println);
}
Algorithm is the following:
Starting from empty subSet.
Cycle through original array from the beginning (assuming array is already sorted in ascending order) until currentSum is less or equal target sum.
If current element added to currentSum is less than target sum, adding to current subSet current element and running recursion starting from the next element.
Breaking current cycle if current sum exceed targetSum.
If we can't add more elements into current subSet, we checking if it is maximal and print it in this case.
To determine maximal subSets we can compare original array and current subSet element by element, searching for the first mismatch. If element at first mismatch index is greater than difference between currentSum and targetSum, subSet is maximal and should be printed.
Working solution on Java is below:
public class Test {
/**
* Assuming alphabet[] is already sorted in increasing order
*/
public static void printMaximalSubSetsToSum(int[] alphabet, int sum) {
if (alphabet == null || alphabet.length == 0) {
return;
}
if (alphabet[0] > sum) {
// no sense to search, since smallest element in array already bigger than sum
return;
} else if (alphabet[0] == sum) {
Set<Integer> subSet = new HashSet<>();
subSet.add(alphabet[0]);
printSubset(subSet);
}
Set<Integer> subSet = new HashSet<>();
processMaximalSubSetToSum(alphabet, sum, 0, 0, subSet);
}
private static void processMaximalSubSetToSum(int[] alphabet, int sum, int sumSoFar, int startFrom, Set<Integer> subSet) {
if (startFrom >= alphabet.length) {
if (isMaximalSubSet(alphabet, subSet, sum - sumSoFar)) {
printSubset(subSet);
}
return;
}
for (int i = startFrom; i < alphabet.length; i++) {
int newSum = sumSoFar + alphabet[i];
if (newSum > sum) {
if (isMaximalSubSet(alphabet, subSet, sum - sumSoFar)) {
printSubset(subSet);
}
return;
} else {
subSet.add(alphabet[i]);
if (newSum == sum) {
printSubset(subSet);
} else {
processMaximalSubSetToSum(alphabet, sum, newSum, i + 1, subSet);
}
subSet.remove(alphabet[i]);
}
}
}
private static boolean isMaximalSubSet(int[] alphabet, Set<Integer> subSet, int diff) {
// search first mismatch element between alphabet and current SubSet
Iterator<Integer> it = subSet.iterator();
int i = 0;
while (it.hasNext()) {
if (it.next() != alphabet[i]) {
break;
}
i++;
}
return i >= alphabet.length || alphabet[i] > diff;
}
private static void printSubset(Set<Integer> subset) {
System.out.println(subset);
}
public static void main(String[] args) throws java.lang.Exception {
//printMaximalSubSetsToSum(new int[]{1, 2, 3, 4, 5}, 7);
// Correct output is: {1, 2, 3}; {1, 2, 4}; {1, 5}; {2, 5}; {3, 4}
}
}
I am sorry for chipping in so late. But how about doing this?
1) Build a MIN-HEAP structure from the given array/set
2) traverse the structure from the root and keep subtracting the value at the node that you visit. Once you exceed the required sum (curr_sum > k), output this path, backtrack to the parent and take another path (this can be done recursively).
3) If backtracking takes you back to the original node that you started from, implement the entire algorithm recursively from root->left node.
4) Do the same two steps (2) and (3) above but with a MAX-HEAP now.
I am new to algorithms and data structures, and have only started reading Intro to Algos-Cormen. This might be a faulty solution, but I would be more than happy if anyone points out the fault to me :)

given an array, for each element, find out the total number of elements lesser than it, which appear to the right of it

I had previously posted a question, Given an array, find out the next smaller element for each element
now, i was trying to know , if there is any way to find out "given an array, for each element, find out the total number of elements lesser than it, which appear to the right of it"
for example, the array [4 2 1 5 3] should yield [3 1 0 1 0]??
[EDIT]
I have worked out a solution, please have a look at it, and let me know if there is any mistake.
1 Make a balanced BST inserting elements traversing the array from right to left
2 The BST is made in such a way that each element holds the size of the tree rooted at that element
3 Now while you search for the right position to insert any element, take account of the total size of the subtree rooted at left sibling + 1(for parent) if you move right
Now since, the count is being calculated at the time of insertion of an element, and that we are moving from right to left, we get the exact count of elements lesser than the given element appearing after it.
It can be solved in O(n log n).
If in a BST you store the number of elements of the subtree rooted at that node when you search the node (reaching that from the root) you can count number of elements larger/smaller than that in the path:
int count_larger(node *T, int key, int current_larger){
if (*T == nil)
return -1;
if (T->key == key)
return current_larger + (T->right_child->size);
if (T->key > key)
return count_larger(T->left_child, key, current_larger + (T->right_child->size) + 1);
return count_larger(T->right_child, key, current_larger)
}
** for example if this is our tree and we're searching for key 3, count_larger will be called for:
-> (node 2, 3, 0)
--> (node 4, 3, 0)
---> (node 3, 3, 2)
and the final answer would be 2 as expected.
Suppose the Array is 6,-1,5,10,12,4,1,3,7,50
Steps
1.We start building a BST from right end of the array.Since we are concerned with all the elements to right for any element.
2.Suppose we have formed the partial solution tree upto the 10.
3.Now when inserting 5 we do a tree traversal and insert to the right of 4.
Notice that each time we traverse to the right of any node we increment by 1 and add the no. of elements in left subtree of that node.
eg:
for 50 it is 0
for 7 it is 0
for 12 it is 1 right traversel + leftsubtree size of 7 = 1+3 =4
for 10 same as above.
for 4 it is 1+1 =2
While building bst we can easily maintain the left subtree size for each node by simply maintaining a variable corresponding to it and incrementing it by 1 each time a node traverses to the left by it.
Hence the Solution Average case O(nlogn).
We can use other optimizations such as predetermining whether array is sorted in decreasing order
find groups of element in decreasing order treat them as single.
I think is it possible to do it in O(nlog(n))with a modified version of quicksort. Basically each time you add an element to less, you check if this element rank in the original array was superior to the rank of the current pivot. It may look like
oldrank -> original positions
count -> what you want
function quicksort('array')
if length('array') ≤ 1
return 'array' // an array of zero or one elements is already sorted
select and remove a pivot value 'pivot' from 'array'
create empty lists 'less' and 'greater'
for each 'x' in 'array'
if 'x' ≤ 'pivot'
append 'x' to 'less'
if oldrank(x) > = oldrank(pivot) increment count(pivot)
else
append 'x' to 'greater'
if oldrank(x) < oldrank(pivot) increment count(x) //This was missing
return concatenate(quicksort('less'), 'pivot', quicksort('greater')) // two recursive calls
EDIT:
Actually it can be done using any comparison based sorting algorithm . Every time you compare two elements such that the relative ordering between the two will change, you increment the counter of the bigger element.
Original pseudo-code in wikipedia.
You can also use binary Index tree
int tree[1000005];
void update(int idx,int val)
{
while(idx<=1000000)
{
tree[idx]+=val;
idx+=(idx & -idx);
}
}
int sum(int idx)
{
int sm=0;
while(idx>0)
{
sm+=tree[idx];
idx-=(idx & -idx);
}
return sm;
}
int main()
{
int a[]={4,2,1,5,3};
int s=0,sz=6;
int b[10];
b[sz-1]=0;
for(int i=sz-2;i>=0;i--)
{
if(a[i]!=0)
{
update(a[i],1);
b[i]=sum(a[i]-1)+s;
}
else s++;
}
for(int i=0;i<sz-1;i++)
{
cout<<b[i]<<" ";
}
return 0;
}
//some array called newarray
for(int x=0; x <=array.length;x++)
{
for(int y=x;y<array.length;y++)
{
if(array[y] < array[x])
{
newarray[x] = newarray[x]+1;
}
}
}
something like this,where array is your input array and newarray your output array
make sure to initialize everything correctly(0 for the newarrays values)
Another approach without using the tree.
Construct another sorted array . For example for input array {12, 1, 2, 3, 0, 11, 4} it will be {0, 1, 2, 3, 4, 11, 12}
Now compare position of each element from input array with sorted array.For example 12 in first array is at 0 index while sorted array it’s as 6
Once comparison is done, remove element from both array
Other than using BST, we can also solve this problem optimally by doing some modification in merge sort algorithm (in O(n*logn) time).
If you observe this problem more carefully, you can say that in the problem we need to count the number of inversions required for each element to make the array sorted in ascending order, right?
So this problem can be solved using Divide and Conquer paradigm. Here you need to maintain an auxiliary array for storing the count of inversions required (i.e. elements smaller than it on the right side of it).
Below is a python program:
def mergeList(arr, pos, res, start, mid, end):
temp = [0]*len(arr)
for i in range(start, end+1):
temp[i] = pos[i]
cur = start
leftcur = start
rightcur = mid + 1
while leftcur <= mid and rightcur <= end:
if arr[temp[leftcur]] <= arr[temp[rightcur]]:
pos[cur] = temp[leftcur]
res[pos[cur]] += rightcur - mid - 1
leftcur += 1
cur += 1
else:
pos[cur] = temp[rightcur]
cur += 1
rightcur += 1
while leftcur <= mid:
pos[cur] = temp[leftcur]
res[pos[cur]] += end - mid
cur += 1
leftcur += 1
while rightcur <= end:
pos[cur] = temp[rightcur]
cur += 1
rightcur += 1
def mergeSort(arr, pos, res, start, end):
if start < end:
mid = (start + end)/2
mergeSort(arr, pos, res, start, mid)
mergeSort(arr, pos, res, mid+1, end)
mergeList(arr, pos, res, start, mid, end)
def printResult(arr, res):
print
for i in range(0, len(arr)):
print arr[i], '->', res[i]
if __name__ == '__main__':
inp = input('enter elements separated by ,\n')
inp = list(inp)
res = [0]*len(inp)
pos = [ind for ind, v in enumerate(inp)]
mergeSort(inp, pos, res, 0, len(inp)-1)
printResult(inp, res)
Time : O(n*logn)
Space: O(n)
You can also use an array instead of a binary search tree.
def count_next_smaller_elements(xs):
# prepare list "ys" containing item's numeric order
ys = sorted((x,i) for i,x in enumerate(xs))
zs = [0] * len(ys)
for i in range(1, len(ys)):
zs[ys[i][1]] = zs[ys[i-1][1]]
if ys[i][0] != ys[i-1][0]: zs[ys[i][1]] += 1
# use list "ts" as binary search tree, every element keeps count of
# number of children with value less than the current element's value
ts = [0] * (zs[ys[-1][1]]+1)
us = [0] * len(xs)
for i in range(len(xs)-1,-1,-1):
x = zs[i]+1
while True:
us[i] += ts[x-1]
x -= (x & (-x))
if x <= 0: break
x = zs[i]+1
while True:
x += (x & (-x))
if x > len(ts): break
ts[x-1] += 1
return us
print count_next_smaller_elements([40, 20, 10, 50, 20, 40, 30])
# outputs: [4, 1, 0, 2, 0, 1, 0]
Instead of BST, you can use stl map.
Start inserting from right.
After inserting an element, find its iterator:
auto i = m.find(element);
Then subtract it from m.end(). That gives you the number of elements in map which are greater than current element.
map<int, bool> m;
for (int i = array.size() - 1; i >= 0; --i) {
m[array[i]] = true;
auto iter = m.find(array[i])
greaterThan[i] = m.end() - iter;
}
Hope it helped.
Modified Merge sort: (Already tested code)
Takes O(nlogn) time.
public class MergeSort {
static HashMap<Integer, Integer> valueToLowerCount = new HashMap<Integer, Integer>();
public static void main(String[] args) {
int [] arr = new int[] {50, 33, 37, 26, 58, 36, 59};
int [] lowerValuesOnRight = new int[] {4, 1, 2, 0, 1, 0, 0};
HashMap<Integer, Integer> expectedLowerCounts = new HashMap<Integer, Integer>();
idx = 0;
for (int x: arr) {
expectedLowerCounts.put(x, lowerValuesOnRight[idx++]);
}
for (int x : arr) valueToLowerCount.put(x, 0);
mergeSort(arr, 0, arr.length-1);
//Testing
Assert.assertEquals("Count lower values on right side", expectedLowerCounts, valueToLowerCount);
}
public static void mergeSort(int []arr, int l, int r) {
if (r <= l) return;
int mid = (l+r)/2;
mergeSort(arr, l, mid);
mergeSort(arr, mid+1, r);
mergeDecreasingOrder(arr, l, mid, r);
}
public static void mergeDecreasingOrder(int []arr, int l, int lr, int r) {
int []leftArr = Arrays.copyOfRange(arr, l, lr+1);
int []rightArr = Arrays.copyOfRange(arr, lr+1, r+1);
int indexArr = l;
int i = 0, j = 0;
while (i < leftArr.length && j < rightArr.length) {
if (leftArr[i] > rightArr[j]) {
valueToLowerCount.put(leftArr[i], valueToLowerCount.get(leftArr[i]) + rightArr.length - j);
arr[indexArr++] = leftArr[i++];
}else {
arr[indexArr++] = rightArr[j++];
}
}
while (i < leftArr.length) {
arr[indexArr++] = leftArr[i++];
}
while (j < rightArr.length) {
arr[indexArr++] = rightArr[j++];
}
}
}
To find the total number of values on right-side which are greater than an array element, simply change single line of code:
if (leftArr[i] > rightArr[j])
to
if (leftArr[i] < rightArr[j])

Resources