Is it possible to find the original sequence of integers from its prefix sums and suffix sums?
By : Ahmed Abd El Wahed
Date : September 15 2020, 11:00 PM

it should still fix some issue Is there a way to find the initial sequence from its prefix sums and suffix sums? , Prefix Sum: code :
original array : {1, 2, 3}
prefix sum array : {1, 1+2, 1+2+3}
original array : {1, 2, 3}
suffix sum array : {3+2+1, 3+2, 3}
Let combined array be c[] = {1, 1+2, 3, 3+2, 1+2+3, 3+2+1} = {1, 3, 3, 5, 6, 6}
int length = combined_array.length/2;
int []prefix_breakup = new int[length];
int []original = new int[length];
for(int i=0; i<length ; i++){
if( i%2 == 0 ){
prefix_breakup[i] = combined_array[i];
}
}
original[0] = prefix_breakup[0];
for(int i=1; i<length ; i++){
original[i] = prefix_breakup[i]  prefix_breakup[i1];
}
Share :

Algorithm to find subset within two sets of integers whose sums match
By : Curtis
Date : March 29 2020, 07:55 AM
Any of those help I'm looking for an algorithm which can take two sets of integers (both positive and negative) and find subsets within each that have the same sum. , What others have said is true: code :
@a = qw(4 5 9 10 1);
@b = qw(21 7 4 180);
%a = sums( @a );
%b = sums( @b );
for $m ( keys %a ) {
next unless exists $b{$m};
next if $m == 0 and (@{$a{0}} == 0 or @{$b{0}} == 0);
print "sum(@{$a{$m}}) = sum(@{$b{$m}})\n";
}
sub sums {
my( @a ) = @_;
my( $a, %a, %b );
%a = ( 0 => [] );
while( @a ) {
%b = %a;
$a = shift @a;
for my $m ( keys %a ) {
$b{$m+$a} = [@{$a{$m}},$a];
}
%a = %b;
}
return %a;
}
sum(4 5 9 10) = sum(21 7)
sum(4 9 10 1) = sum(21 7 4)

Write a program that sums the sequence of integers, as well as the smallest in the sequence
By : brdweb
Date : March 29 2020, 07:55 AM
hop of those help? First thing, the 5 is not considered part of the list, it's the count for the list. Hence it shouldn't be included in the calculations. Since this is homework, here's the pseudocode. Your job is to understand the pseudocode first (run it through your head with sample inputs) then turn this into C code and try to get it compiling and running successfully (with those same sample inputs). code :
get a number into quantity
set sum to zero
loop varying index from 1 to quantity
get a number into value
add value to sum
if index is 1
set smallest to value
else
if value is less than smallest
set smallest to value
endif
endif
endloop
output "The sum of the sequence of integers is: ", sum
output "The smallest of the integers entered is: ", smallest
#include <stdio.h>
int main (int argCount, char *argVal[]) {
int i; // General purpose counter.
int smallNum; // Holds the smallest number.
int numSum; // Holds the sum of all numbers.
int currentNum; // Holds the current number.
int numCount; // Holds the count of numbers.
// Get count of numbers and make sure it's in range 1 through 50.
printf ("How many numbers will be entered (max 50)? ");
scanf ("%d", &numCount);
if ((numCount < 1)  (numCount > 50)) {
printf ("Invalid count of %d.\n", numCount);
return 1;
}
printf("\nEnter %d numbers then press enter after each entry:\n",
numCount);
// Set initial sum to zero, numbers will be added to this.
numSum = 0;
// Loop, getting and processing all numbers.
for (i = 0; i < numCount; i++) {
// Get the number.
printf("%2d> ", i+1);
scanf("%d", ¤tNum);
// Add the number to sum.
numSum += currentNum;
// First number entered is always lowest.
if (i == 0) {
smallNum = currentNum;
} else {
// Replace if current is smaller.
if (currentNum < smallNum) {
smallNum = currentNum;
}
}
}
// Output results.
printf ("The sum of the numbers is: %d\n", numSum);
printf ("The smallest number is: %d\n", smallNum);
return 0;
}
pax> ./qq
How many numbers will be entered (max 50)? 5
Enter 5 numbers then press enter after each entry:
1> 100
2> 350
3> 400
4> 550
5> 678
The sum of the numbers is: 2078
The smallest number is: 100
pax> ./qq
How many numbers will be entered (max 50)? 5
Enter 5 numbers then press enter after each entry:
1> 40
2> 67
3> 9
4> 13
5> 98
The sum of the numbers is: 227
The smallest number is: 9
pax> ./qq
How many numbers will be entered (max 50)? 0
Invalid count of 0.
[fury]$ ./qq
How many numbers will be entered (max 50)? 51
Invalid count of 51.

Efficient way to find if an integer is one of sums of integers
By : Viktors Garkavijs
Date : March 29 2020, 07:55 AM
With these it helps This is a school assignment, however, the requirement was that the program be "implemented with maximum performance"  which is vague to my taste, because I don't know would memory outweigh speed or not etc. But what I'm looking for is whether there is a "tricky" way to solve the problem by doing some smart manipulation on the input data. code :
public static int arrayContainsSum(int[] a, int[] b) {
final Set<Integer> sums = new HashSet<Integer>();
for (int i = 0; i < a.length  1; i++) sums.add(a[i] + a[i+1]);
for (int x : b) if (sums.contains(x)) return 1;
return 0;
}

Find a pair of integers from an array that sums up to a given number in Java 8 using functionals
By : SRI RAM
Date : March 29 2020, 07:55 AM
With these it helps I was looking at the problem of finding a pair of two integers from an array that sums up to a given number in Java in a different way. I wanted to use Java 8 functionals. I tried something like this: , You can make them final. If nothing else, you can write code :
for (int i = 0; i < arr.length; i++) {
final int[] arrFinal = arr;
final int iFinal = i;
List<Integer> res = IntStream.of(arr).boxed()
.filter(x > x + arrFinal[iFinal] == givenNumber)
.collect(Collectors.toList());
}
for (int i = 0; i < arr.length; i++) {
int target = givenNumber  arr[i];
List<Integer> res = IntStream.of(arr).filter(x > x == target)
.boxed().collect(Collectors.toList());
}

A recursive algorithm to find two integers in an array that sums to a given integer
By : dawei
Date : March 29 2020, 07:55 AM
Does that help You can convert any iterative algorithm into a recursive one by using (for instance) tail recursion. I'd be more expansive, if it weren't homework. I think you'll understand it from the other post.



Related Posts :
