logo
Tags down

shadow

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[i-1];
}


Share : facebook icon twitter icon

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 pseudo-code. Your job is to understand the pseudo-code 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", &currentNum);

        // 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 Related Posts :
  • Intellij "Warning: The jjs tool is planned to be removed from a future JDK release jjs> what to do"
  • My class is Serializable and i have java.io.NotSerializableException why?
  • How to check if all squares of a chessboard got stepped at least once?
  • Netbeans Swing GUI Builder not working with java-module project and Maven
  • How can I convert a huge Resultset of 10 million records to a list in java?
  • What is the most efficient way in terms of memory and performance to print a formatted date+time every second in Java?
  • How can I achieve Null-safety when calling a method written in Java from Kotlin, which throws an exception?
  • Can I use a variable from java file in a scala file?
  • multiple errors while trying to insert into sqlitedatabase
  • Firebase DataSnapshot.getValue() is null
  • Is there anything like Java's AtomicStampedReference in C++?
  • Getting rid of punctuations in Strings
  • How can I execute a Junit test?
  • Alignment indent
  • Condition checking with .endsWith() statement
  • Java - How to find students with their highest marks writing a method in a Student class?
  • Revert minify enabled and proguard effect on decompiled apk
  • What is the difference between <>() and <DataType>() in Java
  • Java Spring Test Autowired Controller is null error
  • No qualifying bean found for dependency [CountryRepository]: expected at least 1 bean which qualifies as autowire candid
  • How to display all elements of LinkedList?
  • How do I check object null checking inside setter method in java?
  • Application accepts duplicated messages from google Pub/Sub even after acknowledge
  • How to Launch GoogleChrome from Selenium Remote Web Driver without ChromeDriver.exe
  • Get part of document into object from Firebase Firestore
  • Is it possible to put two variable types in List?
  • wiremock is throwing Nullpointer when trying from the mvn clean install but working fine with eclipse JunitTest
  • Java download file using a byte[]
  • How to execute shell command with JSON from java
  • Obtain Kubernetes API key with Java
  • Codenameone issue: What will happen to push notification service in ios app after expiration of Pro Account Subscription
  • Producer-consumer problem - both end up waiting
  • List of all z/OS DD statements in JCL job using JZOS toolkit
  • How to Subtract 30Days, 3 Months & 1 Year In Epoch time from now using java?
  • How to save an ArrayList, a custom adapter (and its data) in Android using JSON?
  • Difference between a global variable and an injected variable?
  • Code is crashing, it is meant to take in a full name e.g. John Cena and output it as JCena)
  • If we add a super() in a parent class constructor, which class will this super() invoke while creating an object of the
  • How to configure Jackson and Spring to render objects differently depending on the URL path and level of nesting
  • Java 8 opposite to filter in stream
  • Calling a `scala.Function1[_root_.scala.Predef.String, scala.Any]` from within Java
  • Memory usages high - Slow application response : Used memory value not decreasing + Free memory value not increasing
  • J-Unit 4 Checking return Type
  • JUnit: How to create a test class with dependency injection via the constructor
  • JSON parse error: Cannot deserialize instance of `` out of START_ARRAY token
  • Spring Boot findById is not working but findAllById works fine
  • Do Java class files know about white space?
  • ROOM Framework crashes an application
  • Lombok and changing generic method
  • How do I extract the title attribute and the text using xpath or css selector?
  • Wildfly in Docker container not starting
  • Spring @Component not able to Autowire
  • Validate to check uploaded file is pdf
  • Array not changing Value java
  • Can you link two entitys with OneToOne relationship without a foreign key?
  • In the given example, what is the logic behind the use of the Random class?
  • The simple way to get int array from System.in in Java
  • lombok annotation is not wokring in intellij19.3
  • Why variable may not be initialize inside an if
  • Spring load JSON file and return via requestMapping
  • shadow
    Privacy Policy - Terms - Contact Us © 35dp-dentalpractice.co.uk