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your time-consuming job */
#pragma omp parallel for private(temp)
for(j=0;j<1000;j++){
temp = arr[j];
for(i=0;i<temp;temp--)
result[j]=result[j]*temp;
}
end = clock();
time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
printf("\n\n%f",time_spent);
But every time I run the code I get a different output. I want to see how the performance of the code differs for openmp and serial code. What method I should use to achieve the same?
The time the code takes to run will change a little bit due to computer/server usage; however, if you run both the parallel and serial versions you should see a difference in the amount of run time between the two. Also, the size of your parallel operation is pretty small. But you should see and improvement.
int arr[1000] = {1, 6, 1, 3, 1, 9, 7, 3, 2, 0, 5, 0, 8, 9, 8, 4, 4, 4, 0, 9, 6, 5, 9, 5, 9, 2, 5, 8, 6, 1, 0, 7, 7, 3, 2, 8, 3, 2, 3, 7, 2, 0, 7, 2, 9, 5, 8, 6, 2, 8, 5, 8, 5, 6, 3, 5, 8, 1, 3, 7, 2, 6, 6, 2, 1, 9, 0, 6, 1, 6, 3, 5, 6, 3, 0, 8, 0, 8, 4, 2, 7, 1, 0, 2, 7, 6, 9, 7, 7, 5, 4, 9, 3, 1, 1, 4, 2, 4, 1, 5, 2, 6, 0, 8, 9, 2, 6, 0, 1, 0, 2, 0, 3, 3, 4, 0, 1, 4, 8, 8, 1, 4, 9, 4, 7, 3, 8, 9, 9, 1, 4, 1, 8, 7, 9, 9, 9, 8, 9, 0, 0, 4, 2, 4, 9, 7, 6, 0, 3, 4, 8, 6, 1, 9, 0, 8, 2, 0, 8, 1, 2, 4, 2, 2, 1, 4, 1, 1, 4, 3, 3, 4, 9, 8, 0, 8, 7, 7, 8, 0, 3, 8, 8, 4, 7, 8, 5, 2, 0, 3, 3, 4, 9, 8, 6, 1, 4, 0, 4, 8, 5, 9, 4, 4, 7, 5, 2, 4, 2, 2, 6, 5, 2, 4, 2, 1, 4, 7, 3, 5, 2, 7, 9, 1, 7, 8, 4, 3, 0, 8, 1, 5, 8, 7, 1, 7, 2, 5, 2, 6, 9, 8, 2, 1, 5, 4, 2, 9, 1, 6, 6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5, 4, 9, 0, 2, 7, 2, 9, 0, 9, 9, 2, 6, 4, 3, 6, 9, 7, 6, 1, 6, 0, 6, 4, 9, 9, 6, 6, 0, 2, 2, 6, 6, 3, 8, 8, 1, 0, 9, 3, 9, 8, 5, 6, 4, 8, 4, 3, 5, 0, 7, 2, 2, 3, 8, 3, 2, 5, 9, 2, 7, 1, 0, 5, 6, 0, 4};
clock_t begin, end;
double time_spent_omp;
double time_spent;
begin = omp_get_wtime();
/* here, do your time-consuming job */
#pragma omp parallel for private(temp)
for(j=0;j<1000;j++){
temp = arr[j];
for(i=0;i<temp;temp--)
result[j]=result[j]*temp;
}
end = omp_get_wtime();
time_spent_omp = (double)(end - begin) / CLOCKS_PER_SEC;
begin = omp_get_wtime();
/* here, do your time-consuming job */
for(j=0;j<1000;j++){
temp = arr[j];
for(i=0;i<temp;temp--)
result[j]=result[j]*temp;
}
end = omp_get_wtime();
time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
printf("\n\n Time to process: %f --- Time to process with OPENMP %f",time_spent, time_spent_omp);
This should give you a better idea about how it is working.
I have ArrayList<int> that contains all numbers that can be used in the combinations. I want to generate all possible combinations of these numbers of different length (number of integers), but all must have sum closest to N, but <= N (N is a input number). All numbers of the list have to be used (and only) once.
EXAMPLE
N = 10
list = {1, 5, 4, 3, 6, 9, 7, 4, 3, 8, 2, 1, 6, 3, 7}
One combination will be (1, 5, 4), (3, 6), (9), (7), (4, 3), (8, 2), (1, 6, 3), (7)
Can anyone help me with the solution?
I'm thinking of some recursive implementation. Am I on the right track?
EDIT
OK, because I can't explain this problem exactly as I want :) let's make it simpler. How to generate all sub-lists that have sum <= N, and can contain each number of the list only once. I'll do the rest of the work myself.
A simple k-combination of a finite set S is a subset of k distinct elements of S. Specifying a subset does not arrange them in a particular order.
You can use the CombinatoricsLib. CombinatoricsLib is a java library for generating combinatorial objects. https://code.google.com/p/combinatoricslib/
Using this:
public static void main(String[] args) {
// Create the initial vector
ICombinatoricsVector<Integer> initialVector = Factory.createVector(
new Integer[] {1, 5, 4, 3, 6, 9, 7, 4, 3, 8, 2, 1, 6, 3, 7} );
int subsetMaxSize = 5;
int upperLimit = 10;
int lowerLimit = 8;
for(int i = 1; i <= subsetMaxSize; i++)
{
Generator<Integer> gen = Factory.createSimpleCombinationGenerator(initialVector, i);
for (ICombinatoricsVector<Integer> combination : gen)
{
int sum = vectorSum(combination);
if(validateSum(sum, lowerLimit, upperLimit))
printVector(combination);
}
}
}
public static boolean validateSum(Integer value, Integer lowerLimit, Integer upperLimit)
{
if(value <= upperLimit && value > lowerLimit)
return true;
return false;
}
public static Integer vectorSum(ICombinatoricsVector<Integer> vect)
{
Integer sum = 0;
for(int i = 0; i < vect.getSize(); i++)
sum += vect.getValue(i);
return sum;
}
public static void printVector(ICombinatoricsVector<Integer> vect)
{
String output = "";
for(int i = 0; i < vect.getSize(); i++)
output += vect.getValue(i) + ", ";
System.out.println(output);
}
will return the output
9,
1, 9,
1, 8,
5, 4,
5, 4,
4, 6,
4, 6,
3, 6,
3, 7,
3, 6,
3, 7,
6, 4,
6, 3,
6, 3,
9, 1,
7, 3,
7, 2,
7, 3,
4, 6,
3, 6,
3, 7,
8, 2,
8, 1,
2, 7,
6, 3,
3, 7,
1, 5, 4,
1, 5, 3,
1, 5, 4,
1, 5, 3,
1, 5, 3,
1, 4, 4,
1, 3, 6,
1, 3, 6,
1, 6, 3,
1, 6, 2,
1, 6, 3,
1, 7, 2,
1, 7, 1,
1, 3, 6,
1, 8, 1,
1, 2, 6,
1, 2, 7,
1, 1, 7,
1, 6, 3,
5, 4, 1,
5, 3, 2,
5, 3, 1,
5, 4, 1,
5, 3, 2,
5, 3, 1,
5, 2, 3,
5, 1, 3,
4, 3, 3,
4, 3, 2,
4, 3, 3,
4, 4, 2,
4, 4, 1,
4, 3, 2,
4, 3, 3,
4, 2, 3,
3, 6, 1,
3, 4, 3,
3, 4, 2,
3, 4, 3,
3, 3, 3,
3, 1, 6,
6, 3, 1,
6, 2, 1,
6, 1, 3,
7, 2, 1,
4, 3, 2,
4, 3, 3,
4, 2, 3,
3, 1, 6,
2, 1, 6,
2, 1, 7,
1, 6, 3,
1, 5, 3, 1,
1, 5, 3, 1,
1, 5, 2, 1,
1, 5, 1, 3,
1, 4, 3, 2,
1, 4, 3, 1,
1, 4, 4, 1,
1, 4, 3, 2,
1, 4, 3, 1,
1, 4, 2, 3,
1, 4, 1, 3,
1, 3, 4, 2,
1, 3, 4, 1,
1, 3, 3, 2,
1, 3, 3, 3,
1, 3, 2, 3,
1, 6, 2, 1,
1, 4, 3, 2,
1, 4, 3, 1,
1, 4, 2, 3,
1, 4, 1, 3,
1, 3, 2, 3,
1, 2, 1, 6,
4, 3, 2, 1,
4, 3, 2, 1,
4, 2, 1, 3,
3, 4, 2, 1,
3, 3, 2, 1,
3, 3, 1, 3,
3, 2, 1, 3,
4, 3, 2, 1,
4, 2, 1, 3,
3, 2, 1, 3,
1, 3, 3, 2, 1,
1, 3, 2, 1, 3,
1, 3, 2, 1, 3,
You can use recursion, however if you will tell what is your original problem and the constraints, then there could be a better solution. For recursion it will be something like this:
list = {1, 5, 4, 3, 6, 9, 7, 4, 3, 8, 2, 1, 6, 3, 7};
result = {};
function rec(index, max_sum) {
if(index >= list.length) {
print result;
return;
}
for each list[i] where i >= index {
// Case 1 - we take current element and go further
if(list[i] <= max_sum) {
result.insert(list[i]);
rec(index + 1, max_sum - list[i]);
result.remove(list[i]);
}
// Case 2 - we skip current element
rec(index + 1, max_sum);
}
}
N = 10;
rec(0, N);
This will just generate all possible combinations of numbers sum of which doesn't exceed N.
Given an array of n elements, remove any adjacent pair of elements which are equal. Repeat this operation until there are no more adjacent pairs to remove; that will be the final array.
For e.g 1 2 2 3 4 should return the array 1 3 4.
please note array need not to be sorted.
check this test case also: 1,2,2,3,4,4,3,5 o/p should be 1,5.
(2,2) and (4,4) gets removed, then (3,3) which became adjacent after the removal of (4,4)
Any time you remove a pair of elements, you also need to see if you generated another pair that you want to remove.
The algorithm should follow naturally from that observation.
In Python:
>>> l=[1,2,2,3,4,4,3,5]
>>> [x for x in l if not l.count(x) > 1]
[1, 5]
This removes all integers that occur more than once in the list. This is a correct result for your example but I think that you are really trying to state something different. I think you are saying:
list:=(an unsorted list of integers)
while adjacent_pairs(list) is True:
remove_adjacent_pairs(list)
Once again, in Python:
#!/usr/bin/env python
def dedupe_adjacent(l):
for i in xrange(len(l) - 1, 0, -1):
if l[i] == l[i-1]:
del l[i-1:i+1]
return True
return False
def process_list(l):
print "input list: ",l
i=1
while(dedupe_adjacent(l)):
print " loop ",i,":",l
i+=1
print "processed list=",l
print
process_list([1,2,2,3,4,4,3,5])
process_list([1,2,2,3,4,4,6,3,5])
Output:
input list: [1, 2, 2, 3, 4, 4, 3, 5]
loop 1 : [1, 2, 2, 3, 3, 5]
loop 2 : [1, 2, 2, 5]
loop 3 : [1, 5]
processed list= [1, 5]
input list: [1, 2, 2, 3, 4, 4, 6, 3, 5]
loop 1 : [1, 2, 2, 3, 6, 3, 5]
loop 2 : [1, 3, 6, 3, 5]
processed list= [1, 3, 6, 3, 5]
The following:
function compress(arr) {
var prev, res = [];
for (var i in arr) {
if (i == 0 || (arr[i] != arr[i - 1]))
res.push(arr[i]);
}
return res;
}
compress([1, 2, 2, 3, 3, 3, 3, 4, 3, 3, 5, 6, 7, 8, 8]);
Returns:
[1, 2, 3, 4, 3, 5, 6, 7, 8]
Also (JavaScript 1.6 solution):
[1, 2, 2, 3, 3, 3, 3, 4, 3, 3, 5, 6, 7, 8, 8].filter(function(el, i, arr) {
return i == 0 || (el != arr[i - 1]);
})
Edit: Removing any item that appears in the array more than once requires a different solution:
function dedup(arr) {
var res = [], seen = {};
for (var i in arr)
seen[arr[i]] = seen[arr[i]] ? ++seen[arr[i]] : 1;
for (var j in arr) {
if (seen[arr[j]] == 1)
res.push(arr[j]);
}
return res;
}
The following:
dedup([1, 2, 2, 3, 4, 4, 3, 5]);
Produces:
[1, 5]
I have a solution to this in Java. You need to use replaceAll method in String class in Java. You can use regular expession to remove such adjacent redundant characters:
public class MyString {
public static void main(String[] args) {
String str = "12234435";
while(!str.replaceAll("(\\w)\\1+", "").equalsIgnoreCase(str))
str = str.replaceAll("(\\w)\\1+", "");
System.out.println(str);
}
}
You can find how to give a regular expression here
I would:
Sort the array.
From the start of the array, until you are at the last element of the array do:
`count` = count the number of array[i] elements.
remove the first `count` elements of the array if `count` > 1.
The following Python 3 code will remove duplicates from a list (array). It does this by scanning the array from start towards end and compares the target element with the element one larger. If they are the same they are removed. If the element pointer is not pointing at 0, then it is reduced by 1 in order to catch nested pairs. If the two compared elements are different then the pointer is incremented.
I'm sure there's a more pythonic way to remove two adjacent elements from a list, but I'm new to Python and haven't figured that out yet. Also, you'll want to get rid of the print(indx, SampleArray) statement--I left it in there to let you follow the progress in the output listing below.
# Algorithm to remove duplicates in a semi-sorted list
def CompressArray(SampleArray):
indx=0
while(indx < len(SampleArray)-1):
print(indx, SampleArray)
if(SampleArray[indx]==SampleArray[indx+1]):
del(SampleArray[indx])
del(SampleArray[indx])
if(indx>0):
indx-=1
else:
indx+=1
return SampleArray
Here are sample runs for:
[1, 2, 2, 3, 4]
[1, 2, 2, 3, 4, 4, 3, 5]
[1, 2, 2, 3, 3, 3, 3, 4, 3, 3, 5, 6, 7, 8, 8]
[1, 2, 2, 3, 4, 6, 7, 7, 6, 4, 3, 8, 8, 5, 9, 10, 10, 9, 11]
[1, 1, 2, 3, 3, 2, 4, 5, 6, 6, 5, 7, 8, 8, 7, 4, 9]
================================
0 [1, 2, 2, 3, 4]
1 [1, 2, 2, 3, 4]
0 [1, 3, 4]
1 [1, 3, 4]
[1, 3, 4]
================================
0 [1, 2, 2, 3, 4, 4, 3, 5]
1 [1, 2, 2, 3, 4, 4, 3, 5]
0 [1, 3, 4, 4, 3, 5]
1 [1, 3, 4, 4, 3, 5]
2 [1, 3, 4, 4, 3, 5]
1 [1, 3, 3, 5]
0 [1, 5]
[1, 5]
================================
0 [1, 2, 2, 3, 3, 3, 3, 4, 3, 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11]
0 [1, 5, 9, 10, 10, 9, 11]
1 [1, 5, 9, 10, 10, 9, 11]
2 [1, 5, 9, 10, 10, 9, 11]
3 [1, 5, 9, 10, 10, 9, 11]
2 [1, 5, 9, 9, 11]
1 [1, 5, 11]
[1, 5, 11]
================================
0 [1, 1, 2, 3, 3, 2, 4, 5, 6, 6, 5, 7, 8, 8, 7, 4, 9]
0 [2, 3, 3, 2, 4, 5, 6, 6, 5, 7, 8, 8, 7, 4, 9]
1 [2, 3, 3, 2, 4, 5, 6, 6, 5, 7, 8, 8, 7, 4, 9]
0 [2, 2, 4, 5, 6, 6, 5, 7, 8, 8, 7, 4, 9]
0 [4, 5, 6, 6, 5, 7, 8, 8, 7, 4, 9]
1 [4, 5, 6, 6, 5, 7, 8, 8, 7, 4, 9]
2 [4, 5, 6, 6, 5, 7, 8, 8, 7, 4, 9]
1 [4, 5, 5, 7, 8, 8, 7, 4, 9]
0 [4, 7, 8, 8, 7, 4, 9]
1 [4, 7, 8, 8, 7, 4, 9]
2 [4, 7, 8, 8, 7, 4, 9]
1 [4, 7, 7, 4, 9]
0 [4, 4, 9]
[9]
================================
I love Java, but functional solutions should get more time on this site.
In Haskell, doing things the way the question asks:
compress lst = if (length lst == length b) then lst else (compress b) where
b = helper lst
helper [] = []
helper [x] = [x]
helper (x:y:xs) = if (x == y) then (helper xs) else (x:helper (y:xs))
You can solve this problem in O(n) time, although it is a bit more complicated
compress' lst = reverse (helper [] lst) where
helper xs [] = xs
helper [] (x:xs) = helper [x] xs
helper (a:as) (x:xs)
| a == x = helper as xs
| otherwise = helper (x:a:as) xs
I think we could use a stack to check adjacent duplicated elements.
Scan the array. For each new element, if it is equal to the top element in the stack, drop it and pop the top element from the stack. Otherwise, push it into the stack.
Here is the stack based algorithm based upon the edited question.
// pseudo code, not tested
void RemoveDupp(vector<int> & vin, vector<int> & vout)
{
int i = 0, int j = -1;
vout.resize(vin.size());
while (i < vin.size())
{
if (j == -1 || vout[j] != vin[i])
vout[++j] = vin[i++]; //push
else
j--, i++; //pop
}
vout.resize(j + 1);
}