Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8741799
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T11:22:51+00:00 2026-06-13T11:22:51+00:00

I am reading four files byte by bytes and comparing all four of those

  • 0

I am reading four files byte by bytes and comparing all four of those bytes to make one final byte from those four bytes. The way it works is:

  1. If all bytes are 0, then output 0
  2. If all but 1 byte are 0, then output the odd-byte-out (If the bytes were 0, 0, 1, 0, then I would output a 1)
  3. If 2 bytes are 0, then I randomly output one of the non-0 bytes
  4. If 1 byte is 0, then I output the most occurring byte from the other 3, or otherwise in case of a tie I output a random byte from that set
  5. If all bytes are non-0, then I output the most occurring byte, or otherwise in case of a tie I output a random byte from that set

One important thing to note is that “random” does not literally have to be random, I can choose the most convenient one.

So I have given this some thought, but I still cannot come up with the absolute fastest way of getting an output from these numbers. One thing I noticed those is if the first two bytes I read are the same and non-zero, then I can skip the next two bytes and just output one of the first two bytes. If the first three bytes are 0, then I can output the last byte. I can also check the third byte with the first byte and second byte to see if they are equal so I can avoid going onto the 4th byte, but I need this to be literally as efficient as possible. I need to run over this algorithm about 8 billion times, and so every little bit counts =)

So anyways, what is the most efficient way to go about doing this? (pseudocode? or otherwise)

This is what the program looks like (the shell of it at least):

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class reconstructerv2 {
   public static void main(String[] args) throws IOException {

      FileInputStream in1 = null;
      FileInputStream in2 = null;
      FileInputStream in3 = null;
      FileInputStream in4 = null;
      FileOutputStream out = null;  

      try {
         in1 = new FileInputStream("1.dd");
         in2 = new FileInputStream("2.dd");
         in3 = new FileInputStream("3.dd");
         in4 = new FileInputStream("4.dd");
         BufferedInputStream in1b = new BufferedInputStream(in1);
         BufferedInputStream in2b = new BufferedInputStream(in2);
         BufferedInputStream in3b = new BufferedInputStream(in3);
         BufferedInputStream in4b = new BufferedInputStream(in4);
         out = new FileOutputStream("final.dd");
         int a;
         int b;
         int c;
         int d;
         int o; 

         while ((a = in1.read()) != -1) {
            b = in2.read();
            if (a == b && a != 0)
               o = a;
            else {
               c = in3.read();
               d = in4.read();
            }
            System.out.println((byte) c);
            out.write((byte) o);
         }
      } finally {
         if (in1 != null) {
            in1.close();
            in2.close();
            in3.close();
            in4.close();
         }
         if (out != null) {
            out.close();
         }
      }
   }
}

EDIT: 8 billion, not 8 million

EDIT2: As pointed out in the comments, I can’t skip reading over characters due to synchronization.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T11:22:52+00:00Added an answer on June 13, 2026 at 11:22 am

    If all bytes are 0, then output 0

    Output a+b+c+d.

    If all but 1 byte are 0, then output the odd-byte-out (If the bytes were 0,0,1,0, then I would output a 1)

    Same as above. This already saves you a case. If any three bytes are zero, output the sum.

    If 2 bytes are 0, then I randomly output one of the non-0 bytes
    If 1 byte is 0, then I output the most occurring byte from the other 3, or otherwise in case of a tie I output a random byte from that set
    If all bytes are non-0, then I output the most occurring byte, or otherwise in case of a tie I output a random byte from that set

    These cases are really all the same. Output the most-occurring of the non-zero bytes, or a random one in the event of a tie.

    So there are really only two cases to consider: at least three zero bytes, and two or fewer.

    As a matter of fact all the cases are the same. You can apply the second solution above to all the cases. It may well be quicker not to separate them at all.

    But first added BufferedInputStreams around those FileInputStreams.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Reading the docs, I'd expect $(#wrap2).remove(.error) to remove all .error elements from #wrap2 .
I'm reading in an NES ROM file, where the first four bytes are \x4e\x45\x53\x1a,
I started reading mp3-files in c++. All went well until I read the specs
I'm reading from a .txt file and have four scores for each person, i
What i have is four comboboxes and two files. If the column matches the
I am reading a plain text from a file - by lines, with a
All, I'm reading a csv file and adding the data to a MySQL DB
I have two TextRanges from two different RichTextBoxes, and four strings from regular textboxes.
I face the challenge of reading/writing files (in Gigs) line by line. Reading many
I am reading the document of GNU Make. Here is an example %.d: %.c

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.