Tagged Questions
0
votes
0answers
13 views
Best algorithm to find a subarray whose sum is closer or equal to given number. [on hold]
Suppose we have sub array of size n and we have to find subarray of whose sum is equal to k or closest to k.
Solution: What would be best algo for this problem.
7
votes
4answers
83 views
Use a Java Array like a List
I'm saving string values in an array one at a time so I cannot initialize the array with the values all at once. After I'm done with the array I need to pass it to another class that is expected a ...
7
votes
1answer
76 views
Program to insert, search for and delete an element from an array
I'd like this code to be improved.
...
8
votes
2answers
105 views
Finding the longest Collatz sequence cycle
I wrote a program to calculate Collatz sequences for initial numbers between 1 and a given integer i, then find the one with the largest cycle length. My code is ...
7
votes
2answers
60 views
7
votes
3answers
265 views
Find two unique integers in sorted array
I have to write the following method:
I have sorted array of integers and in this array every integer repeats twice, except two elements. I have to find these two elements.
Input: 1, 1, 2, 3, 3, ...
10
votes
5answers
569 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 ...
4
votes
2answers
133 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.
...
8
votes
2answers
167 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?
...
4
votes
2answers
187 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 ...
1
vote
2answers
62 views
Given suffix array and a pattern, search if given pattern exists
Given a suffix array for a word, check if a pattern (consecutive chars) exists.
Example:
A suffix array constructed for "ameya" should return true for "ame" but false for "foo" or false ...
7
votes
2answers
90 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.
...
5
votes
2answers
234 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 ...
6
votes
1answer
66 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 ...
13
votes
4answers
545 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 ...
7
votes
4answers
600 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.
...
2
votes
2answers
137 views
Two solutions with min max values - which should be slower?
While practicing at codinbat.com on the assignment:
Given an array length 1 or more of ints, return the difference between the largest and smallest values in ...
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 ...
5
votes
2answers
126 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 ...
4
votes
4answers
881 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, ...
3
votes
2answers
91 views
Given an integer array of size n, find any three numbers
Return any three descending numbers that appear sequentially in the input such that A[i] > A[j] > A[k] and i < j < k. Since problem description contains 'any', I could not unit test using JUnit.
...
10
votes
4answers
535 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 ...
5
votes
2answers
110 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.
...
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.
...
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 ...
2
votes
1answer
527 views
Turning 1D array to 2D array in TicTacToe
To check a win in my TicTacToe game, I have created a 2D array that contains all the combination that a game can be won in, like so:
...
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 ...
13
votes
3answers
3k views
2
votes
1answer
85 views
Computing sum and counting matches in an array
The problem was pretty straightforward and used two simple algorithms from our examples; the one for computing the sum, and the one for counting matches. The array for the problem wasn't provided, but ...
3
votes
2answers
66 views
Returning all the LIS of given array of int
I have this assignment where I'm supposed to code an algorithm for finding the quantity of Longest Increasing Subsequences from an array of (different) integers and print them all.
I developed one ...
1
vote
2answers
541 views
Print the elements of an array into a table
I am trying to print the elements of an array into a table, but I'm having trouble formatting the array elements itself. Perhaps there is a better solution than what I'm currently using?
...
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 ...
7
votes
2answers
691 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 ...
-3
votes
1answer
289 views
Create arraylist on driver class [closed]
I don't quite understand what I'm doing and what I'm doing wrong.
Please help me to fix/finalize my code.
I am supposed to create at least 3 Student objects with input data of your choice to ...
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 ...
3
votes
4answers
609 views
Median of two sorted equal sized arrays on combination
The comprehensive description of the problem can be found here. I'm looking for code review, clever optimizations, adherence to best practices. etc.
This code is also a correction of code previously ...
0
votes
1answer
134 views
Find the mean of the medians of two equal sized sorted arrays [closed]
A corrected solution is found here
Looking for code review, clever optimizations, adherence to best practices. etc.
As an example:
a1[1, 3, 5, 7]
a2[2, 4, 6, 8]
The median for each array is the ...
5
votes
1answer
197 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 ...
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
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 ...
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 ...
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:
...
4
votes
3answers
95 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 ...
3
votes
1answer
320 views
Optimising Lucky Number Program
In this, we are given an array of numbers, say 1 2 3 4.
We start from a given position, let's say the 1st position.
We cancel that number and move forward that many non-cancelled numbers. The number ...
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 ...
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 ...
5
votes
2answers
1k 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 ...
8
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:
...
3
votes
1answer
1k views
Find missing numbers in a range, with duplicate numbers in a sorted array
Suggestions for cleanup and optimization request.
This question is a follow-up question on this post.
Lets assume the range of
1, 2, 3, 4.
This function takes in an array of repeated ...
3
votes
1answer
563 views
Find two missing elements from a sorted array in given range
Looking for reviewers to suggest optimization and improvements, and to evaluate coding practices.
...