Tagged Questions
19
votes
6answers
1k views
How can I make this mess of Java for-loops faster?
I'm trying to find all the 3, 4, 5, and 6 letter words given 6 letters. I am finding them by comparing every combination of the 6 letters to an ArrayList of words ...
19
votes
6answers
5k views
Guessing a unique 4 random digits number
I've created a simple game, in which the user needs to guess 4 digits number between 0-9, generated randomly using Random() Each of the 4 digits are different from ...
16
votes
10answers
2k views
Find all the missing number(s)
Recently I've attended an interview, where I faced the below question:
Question: "There is an sorted array. You need to find all the missing numbers. Write the complete code, without using any ...
14
votes
6answers
1k views
Array whose values are the product of every other integer
I was asked the following interview question over the phone:
Given an array of integers, produce an array whose values are the
product of every other integer excluding the current index.
...
13
votes
3answers
2k views
13
votes
4answers
522 views
Finding the sub-array with the maximum sum - my approach
This is the code I ended up with that implements the approach I described in a recent answer to another question about the same problem
The basic idea here is to not loop through more things than ...
13
votes
8answers
19k views
Java function to read a CSV file
I have a java function that reads a csv and returns its content as a Vector<Vector<String>>.
The function seems to work as I need it, but my gut ...
11
votes
4answers
364 views
Finding subtuple in larger collection?
I've got a flat array of < 1000 items and I need to find the indices of a smaller tuple/array within the array. The tuple to find can be a varying length (generally between 2 and 5 items, order is ...
10
votes
4answers
503 views
Find byte[] in byte[] with Java
I made a search method because I want to use binary data like PNG files. The point of this is to be able to find strings like IEND in a PNG file. I think it works ...
10
votes
4answers
10k views
Removing duplicate values from an array
Here is my code for removing duplicated values from an array. I think I tested it with the most possible cases. Any suggestions or bugs?
...
10
votes
5answers
536 views
Yahtzee game (using arrays and object classes)
As part of an assignment for my intro Java class (please bear with my beginner skill set), I'm working on a program for which a Die object class is used to play a Yahtzee game. An overview of the ...
10
votes
2answers
2k views
Word count program in Java
I am trying to write clean code in Java for reading text of any size and printing the word count in ascending order without using Java collection framework.
For example, given the following input ...
8
votes
4answers
2k views
Finding all indices of largest value in an array
I have the following code that gives me the indices of an array's largest value:
...
8
votes
2answers
159 views
Min and Max initialization
Is it a good practice to initialize the max to -100000 and min to 100000? Is there any other way to initialize both min and max to 0?
...
8
votes
1answer
2k views
A boolean expression parser, written in Java
I was trying to write some of the Haskell list functions into Java, and I realized that one of the key strengths for many of the functions was the ability to pass in a boolean expression. I decided to ...
7
votes
4answers
519 views
Find the subarray with the max sum
In an interview I was asked to solve the following problem:
find a subarray with max sum
I have written a piece of code for the same. I need your help in reviewing this code.
...
7
votes
2answers
669 views
Fastest way to cut an array into two arrays at a particular index
Suppose I have the array:
double[] arrayToCut = new double[100];
for(int i = 0 ; i < 100 ; ++i) {
arrayToCut[i] = Math.random();
}
My objective is to end ...
7
votes
2answers
88 views
Output in one window
Can anyone give me feedback please? I used what I have learnt so far, mainly methods, loops and arrays. I would like you view based on these topics, however, comments on how to improve are welcome.
...
6
votes
4answers
3k views
Anagrams for a given input
This question is the first draft, my new revised code is here:
Anagrams for a given input 2.0
Here is my code that I wrote, which basically lists the anagrams for a given input.
I did this by ...
6
votes
2answers
4k views
Read an input text file and store the tokens in 2 different arrays
I am very new to Java so please ignore if there are obvious mistakes. If my question seems redundant then please guide me towards the correct link. However, I have surfed enough in order to find the ...
6
votes
4answers
3k views
Getting the largest element in an array using recursion
Any suggestions on how to make this code more efficient?
...
6
votes
2answers
1k views
Find all single elements in an array
I am interested in printing all numbers that do not have a match in an array.
Example: 1,3,4,5,3,4,5,5,6 result 1,5,6
Please ...
6
votes
2answers
435 views
Generating a list of alternatively positioned odd and even number out of an array of unsorted integers
I had an half an hour interview with Microsoft for an intern position. I was asked to generate a list of alternatively positioned odd and even number out of an array of unsorted integers. I guess it ...
6
votes
1answer
117 views
Is there a for-statement I can use to subtract elements in my 2-D in order to make my code more efficient and less-cumbersome?
This code works 100%, but I am trying to see if I can use a for-statement to make my second method less-cumbersome and more efficient instead of making so many ...
6
votes
1answer
5k views
Find all subsets of an int array whose sums equal a given target
I am trying to implement a function below:
Given a target sum, populate all subsets, whose sum is equal to the target sum, from an int array.
For example:
...
6
votes
1answer
62 views
Finding the sub-array with maximum sum - my slow approach
I decided to give the maximum sub-array sum problem an interesting go, however I will assure you that it doesn't perform well, and that I would never put this in production.
I'd like a review on ...
5
votes
4answers
875 views
Find the 2 distinct highest values in an array
I want to know if there is a way to improve my code. It finds the two highest values in an array, and these numbers need to be distinct.
I don't want to sort the values; I just want to find them.
...
5
votes
3answers
441 views
Missed lab, wondering how this bit of Java code could be improved anyway?
I'm not able to get feedback on this because I forgot about the lab due date, but I'd appreciate some critique anyway. It takes in a user specified number of points then finds the two points with the ...
5
votes
3answers
78 views
More efficient (or correct) way to make image grouping work?
It might be easier to read the explanation of what I was trying to do, and implement it a completely different way, rather than go through the code. But the code does what I want it to do, just ...
5
votes
3answers
1k views
Find elements occurring even number of times in an integer array
I came across an interview question, which is like this:
You have an array of integers, such that each integer is present an odd number of time, except 3 of them. Find the three numbers.
I tried ...
5
votes
3answers
2k views
A simple array-based Stack class in Java, what are my mistakes?
I have created a simple array-based Stack class with some methods like the actual Stack in Java.
I am testing this for mistakes, but since I am learning Java and my tests may not be as comprehensive ...
5
votes
2answers
109 views
Voting Algorithm
Find the element the occurs more than \$\dfrac{n}{2}\$ times in an input array, where \$n\$ is input array length.
I'm looking for code review, optimizations and best practices.
...
5
votes
3answers
797 views
A simple ArrayList class in Java
I have created a simple ArrayList with some methods from the actual ArrayList in Java.
I am testing this for mistakes, but I am ...
5
votes
2answers
212 views
Fastest way to multiply two byte arrays in Java
I have two byte arrays that represent unsigned 256-bit values and I want to multiply them resulting into a new value. What is the fastest ways to do this in Java? And are there other factors I should ...
5
votes
2answers
119 views
Keeping track of byte count in a binary protocol handler
I am putting together a fairly simple server that listens for a connection then creates this thread - textbook Java code - then accepts data on that connection.
I am following a protocol that the ...
5
votes
1answer
61 views
Working with list in Java
This code has a method that I call for in main.
I am given a list with temperatures. What I need to do is to write out the biggest absolute difference of 2 ...
5
votes
1answer
106 views
Optimising Funny Marbles
Problem statement is at http://www.codechef.com/DEC13/problems/MARBLEGF
Lira is given array A, which contains elements between 1000 and 2000.
Three types of queries can be performed on this ...
5
votes
3answers
125 views
Is my code dealing with the presented problem the right way?
I finished this program to print a 2D array, I got the right output, but I'm not sure if I did what was being asked in the problem below.
My code to deal with the presented problem:
...
5
votes
1answer
305 views
Merging two array of classes
I have this function that should merge two array of classes when id of array1 is equal to id of array2.
For simplicity I ...
5
votes
1answer
434 views
How is my quarterly sales statistics program?
Am I making good use of a 2-D array? Is this code inefficient? I am supposed to receive input from 6 departments for 4 quarterly periods in Java.
My assignment asks:
Quarterly Sales Statistics
...
5
votes
1answer
3k views
Java 2d array nested loops
Okay, So I have this method I made that searches a 2d array of user input column length and row length comprised of integers. The method is suppose to find 4 alike numbers in rows, columns, and both ...
5
votes
2answers
465 views
Finding greatest value in array smaller than x
Code review requested to make this code simpler, cleaner, and better. Input array is sorted.
This program finds the greatest number smaller than x. So, in an ...
5
votes
1answer
181 views
Performance of Large BigInteger square root and cube root functions, and excessively large array
This is a continuation of this:
Performance of BigInteger square root and cube root functions in Java
I've made most of the changes suggested, but since I'm loading literally the largest array ...
4
votes
2answers
176 views
Finding the value 'n' in an array that has 'n' or more larger values
Given an unsorted array of integers, you need to return maximum possible \$n\$
such that the array consists at least \$n\$ values greater than or equals
to \$n\$. Array can contain duplicate ...
4
votes
4answers
655 views
Find the value nearest to k in the sorted array
Given a sorted array returns the 'closest' element to the input 'x'.
Note, I do understand merits of unit testing in separate files. But deliberately added it to main method for personal convenience, ...
4
votes
2answers
131 views
Searching for duplicates without any collection or sorting
I have written code which is easy to understand, but may not be efficient but will solve the purpose.
...
4
votes
3answers
87 views
Sorting strings in a one dimensional array
What are the better solutions possible?
I have a character array with different strings. There is a max size for the strings. In case a string is smaller than max size remaining places are filled ...
4
votes
2answers
1k views
Calculating and displaying number statistics
I'm writing a program which takes individual int inputs from the user and then outputs some details on the numbers (average, min, max and how many).
Input.readInt() ...
4
votes
2answers
20k views
Reading a line from a text file and splitting its contents
I have this kind of file structure
MALE:FooBar:32
FEMALE:BarFoo:23
Where I would want to identify the gender and age of person, ...
4
votes
2answers
940 views
Binary search for inserting in array
I have written a method that uses binary search to insert a value into an array.
It is working, but i would like to get a second opinion to see if i didn't write too much code for it. aka doing same ...