An algorithm is a sequence of well-defined steps that define an abstract solution to a problem. Use this tag when your issue is related to algorithm design.
1
vote
0answers
20 views
Naive Grouping for Factorization
I have a naive grouping method for factorization. I am curious as to its novelty and aspects of the code below that will increase its efficiency.
The method is best described with an example:
For n ...
4
votes
0answers
34 views
filter function implementations using copy_if Algorithm vs. For Loop
I have the following method of some class, which also defines the method isAllowed:
...
3
votes
1answer
66 views
Trial Multiplication for Factorization
I am trying a trial multiplication for factorization. This is a general example, but each sequence can be specified to pairs of sequences \${p,q}\$ for a given \$n\$. For example, if \$n\$ ended in 3, ...
4
votes
2answers
60 views
Find all pairs in an array that sum to a given number
Assume you have an array of random integers and a sum value. Find all
pairs of numbers from the array that sum up to the given sum in O(n)
time. Find all distinct pairs. (1,2) and (2,1) are not ...
5
votes
2answers
76 views
Kth to last element of a singly linked list
Find the kth to last element of a singly linked list
Any comments?
...
-2
votes
0answers
31 views
9
votes
1answer
213 views
Arranging 8 queens on an 8x8 board
Print all the ways of arranging eight queens on an 8x8 board. Rules:
Queens should not share the same row, column, of any diagonal.
Any comment on my solution?
I didn't write unit tests but I ...
1
vote
1answer
53 views
Checking whether one string is a rotation of another
Given a method isSubstring, two strings s1 and s2, check whether s1 is a rotation of s2 using only one call to isSubstring.
Any comments on the code below?
I would like to check the complexities ...
5
votes
3answers
90 views
Shuffle a sorted array so that left portions contains even indices and right portion contains odd indices numbers
I have been solving the following problem:
Input array: \$[7,4,2,5,1,9,6]\$
Output array: \$[1,4,6,9,7,5,2]\$
I tried to do it in the following way:
Sort the array. So, Input array becomes ...
0
votes
0answers
20 views
A context-based partition_copy
I recently posted a question on Stack Overflow asking how I could go about implementing a context-based partition copy. I actually came up with my own solution, but I would like to see if it could be ...
0
votes
0answers
35 views
Finding common ancestor in a binary tree
Following is a Python program I wrote to find the common ancestor in a binary tree (not BST) with no link to parent node. Please review.
...
2
votes
3answers
68 views
Sorting movie search results
I want to sort "movie search results" to get most probable movie that match "search".
Input example:
CARS.2.2011.720P.AC3.mkv
I have a NameMatcher class ...
3
votes
1answer
106 views
Counting the positive integer solutions of an equation
I have to solve this equation: \$ x + y + xy = n \$
...
3
votes
1answer
64 views
Python Longest Repeat
I am trying to find the longest repeated string in text with python, both quickly and space efficiently. I created an implementation of a suffix tree in order to make the processing fast, but the ...
1
vote
1answer
90 views
Find the highest frequency of messages inside a time slot of 24 hours
I have a very large number (millions) of messages, each labeled with the unixtime it was sent in a SQLite database. Each message has its own userid, for the user that have sent it. I want to know ...
1
vote
1answer
291 views
Number of interesting numbers between two given numbers
Please have a look into this problem:
Little Dipu is a small kid and like all the other kids, he likes to
play, but he plays with numbers (he is extraordinary you know).
Nowadays Dipu has some ...
3
votes
1answer
179 views
Labeling alternating max/mins based on a user defined delta
Given a set of values (points on a graph) find all the max/min points such that
there is no max is followed by a max (i.e. the max and min points alternate)
there is some minimum amount of change ...
4
votes
2answers
61 views
Implementation of Graham Scan algorithm in Haskell
Here is an implementation of Graham Scan algorithm for computing the convex hull of a finite set of points:
...
3
votes
1answer
55 views
Removing duplicates in a char[] O(n)
We're given a char[] containing characters A-Z. (For more characters this can easily be extended). Now we're asked to remove all ...
3
votes
2answers
38 views
Program to evaluate powers of complex numbers
I'm trying to develop a simple program to evaluate integral powers of a complex number \$z\$ , that is, \$z^n\$, where \$z\$ is in the algebrical form \$a+i b\$ and \$n \in \mathbb{Z} ^{*} _{+}\$.
...
3
votes
1answer
35 views
Spending a target sum, preferably split equally
I have a strange programming prompt. I managed to complete an algorithm to solve smaller test entries (which i'll post). One of my test cases has to complete a 200,000 int input and my algorithm isn't ...
3
votes
1answer
40 views
Acquiring indices for anagrams of an input string
I am working on an interview question from Amazon Software:
Design an algorithm to take a list of strings as well as a single input string, and return the indices of the list which are anagrams of ...
6
votes
2answers
201 views
Finding pairs of numbers within A that add up to N
I am working on an interview question from Amazon Software Interview:
Given an integer N and an array of unsorted integers A find all pairs of numbers within A which add up to N
I have a working ...
3
votes
2answers
40 views
Converting from base-10 to word
The following code is used to convert a base-10 number to a base-N number, where N is a length of a given alphabet that contains characters of which a base-N number ...
7
votes
3answers
227 views
Generating a histogram efficiently from the datasets in a Map
I have a map in which I store total bytes as the key and count of users as the value:
...
2
votes
1answer
29 views
Calculating top position of elements based on their heights
I have a data representing several elements (that could be rendered at some point as div), composed of y position (property top) and its height (property height).
...
2
votes
1answer
46 views
Path from the root to a given node in binary tree
Based on the solution provided in this question , I have written non-recursive Java code to find the path form to a given element in a binary tree. This code is working for all the elements except ...
2
votes
1answer
58 views
1
vote
0answers
29 views
Traversing game state space : more search leads to bad results [closed]
I am trying to solve a problem which i think is TSP on a 2-D grid. So, i am trying to get the best result that i can. However, looking ahead 1 step is producing ...
6
votes
2answers
284 views
Sum the exponents of the prime factors of a number
My goal is to find the sum of the exponents of the prime factors of an integer.
I wrote the code below with the complexities specified here (I am not 100% sure of the complexities though):
Runtime ...
4
votes
4answers
109 views
Extremely simple character manipulation
Given a string, this code
Multiplies odd numbers
Subtracts even numbers, do nothing if only one even number is found
Turns uppercase letters to lowercase and vice versa
I think this code is too ...
5
votes
3answers
75 views
Line passing the most number of points
Given points on a two dimensional graph, find a line that passes the most number of points.
Any comments please?
...
7
votes
3answers
168 views
Return list of common email ids from a list of lists
Input: ArrayList of ArrayLists with email ids.
Output: Return ArrayList of email ids which appears in each inner ArrayList.
Ex: Input: [[[email protected], ...
6
votes
0answers
243 views
Quicksort async vs serial
I am playing with async and I figured I'd write a parallel implementation of Quicksort while trying to look at various optimizations. I want to keep the generics ...
4
votes
3answers
104 views
Merge sort implementation and improvements
I would like your opinion on my Java implementation of the merge sort algorithm on an integer array, since I'm not really into algorithms I'd like to know from you if this is a correct implementation ...
4
votes
2answers
77 views
Averaging lists of values with duplicate keys
I have gene expression data that I represent as a list of genes and a list of lists of values. I average the expression data for any genes with the same name.
For example:
...
5
votes
1answer
133 views
Minimum number of coins - (dynamic programming solution - topdown approach)
This is a problem from topcoder tutorials
Given a list of N coins, their values (V1, V2, ... , VN), and the total sum S. Find the minimum number of coins the sum of which is S (we can use as many ...
6
votes
2answers
86 views
Find element in sorted matrix
Find an element in a MxN matrix where each row and column is sorted in ascending order.
Any comments on my code?
(I added some test cases on non square matrices which I did not copy here)
...
1
vote
0answers
47 views
Minimum amount of edges that needs to be traversed to visit all vertices… or something like that
So I have this problem, where I'm given m connections over n vertices. The vertices are labeled ...
1
vote
1answer
41 views
GCD calculation - A cross of the Euclidean and the Binary algorithm
I was reading Knuth's The Art of Computer Programming - Volume 2 (Seminumerical Algorithms) Third Edition. I saw the two algorithms (A and B). A method (due to V. C. Harris) was mentioned (in page ...
6
votes
5answers
562 views
Number of distinct elements in an array
This is my solution to find the Number of distinct elements in an array. Is there away fast way to do this? It run-time is O(N).
...
6
votes
2answers
97 views
Grouping An Array of Strings into Anagrams
Ok, so this code works perfectly, however, I've not coded in Java for a long time, so I'm worried about breaking Java conventions, but even more important, I would appreciate any comments on making ...
2
votes
1answer
53 views
HackerEarth Crazy kangaroo challenge
I want to find the lowest hop count from source to destination.
The challenge, in summary:
The first line contains the number of test cases T, 1 ≤ T ≤ 100000.
The next T lines each contains ...
2
votes
2answers
93 views
Project Euler - Smallest multiple
Here's Problem 5 - Smallest multiple
2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder.
What is the smallest positive number that is evenly ...
13
votes
2answers
274 views
“ACM ICPC Team” challenge on Hackerrank… Easy?
This challenge took me awhile to figure out, and it's characterized as "easy," so I'm sorta wondering what I missed.
Problem Statement
You are given a list of N people who are attending ...
3
votes
1answer
51 views
Generalized Suffix Tree implementation
I've started to write a Generalized Suffix Tree implementation. The overall code is still in an experimental state, though I think it's mature enough to ask for a little review.
I'd like to know how ...
5
votes
3answers
180 views
A partition algorithm for positive integers
I have encountered the following problem that I found very interesting to solve:
Given an array of positive integers {a1, a2, ..., an} you are required
to partition the array into 3 ...
4
votes
2answers
87 views
Removing duplicates for both values
My special case: home project, automatic downloader for podcasts.
Overall algorithm is:
Download a list of available podcasts
hash the podcasts
load metadata + hash of downloaded podcasts from ...
2
votes
0answers
31 views
Search/sort/create functionality, implements bubblesort and sequential search
I've moved my main() to the bottom of the code block in order to fit it all in one class for this submission. If there is a better way to do it?
...
3
votes
2answers
85 views
Radix sort with integer divisions
Here's a perhaps naive but simple implementation of Radix sort in Java.
It works with any radix, and both positive and negative numbers.
...