325 questions
2
votes
1
answer
107
views
Cyclic Sort Implemenation
I was just trying to implement cyclic sort after watching the theory part of a YouTube video (https://youtu.be/JfinxytTYFQ?list=PL9gnSGHSqcnr_DxHsP7AW9ftq0AtAyYqJ&t=889).
I came up with the below ...
7
votes
1
answer
371
views
Given an array find all the elements that appear thrice except one element appearing once: how does the optimal approach using bit-manipulation work
I came across LeetCode problem 137. Single Number II:
Given an integer array nums where every element appears three times except for one, which appears exactly once. Find the single element and ...
2
votes
1
answer
4k
views
Minimum numbers of operation required to make an array bitonic [closed]
I am trying to solve this code challenge:
Given an array arr of n integers, in a single operation, one can reduce any element of the array by 1. Find the minimum number of operations required to ...
-3
votes
1
answer
80
views
Where do I go out of bounds in this Reverse Words In a String problem?
I'm going out of bounds somewhere while iterating a very long string but I can't seem to find out where?
import java.util.*;
public class Solution {
//Helper function to reverse words in string
...
2
votes
1
answer
64
views
Efficient Algorithm for Finding the Longest Increasing Subsequence [duplicate]
I'm working on a project where I need to find the longest increasing subsequence (LIS) from a given array of integers. However, the array can be quite large, so I'm looking for an efficient algorithm ...
0
votes
0
answers
64
views
Constructing an algorithm to find the smallest log(log(n)) elements from an array of length n
I am a math student and I have been asked to help grade answer sheets for an Algorithms course as one of my professors is unwell. While I am quite familiar with combinatorial algorithms, I am not well ...
0
votes
3
answers
199
views
What is the exact time complexity of this algorithm?
I attempted to solve the problem at this HackerRank link (https://www.hackerrank.com/challenges/diagonal-difference/problem?isFullScreen=true) Given a square matrix, calculate the absolute difference ...
-1
votes
1
answer
188
views
online judge problems program c++ [closed]
#include <iostream>
using namespace std;
const int TMAX = 2000000;
int num_chupiguays(const int a[], int n) {
int suma = 0;
int resul = 0;
for (int i = 0; i < n; i++) {
...
0
votes
2
answers
81
views
Diff an array of objects so that it matches another without recreating any valid objects
Let's say I have 2 arrays each of which holds a few different objects
class Object1 {}
class Object2 {}
class Object3 {}
class Object4 {}
class Object5 {}
class Object6 {}
const arrayA = [
new ...
0
votes
1
answer
139
views
I wrote a function in C language that as I would think has O(n^3) time complexity, but as n grows, for some reason it behaves like O(n^2)
I have written a function that makes an array unique, but I don't quite understand how the time complexity works here.
Here's my whole program:
#include <stdio.h>
#include <stdlib.h>
#...
-1
votes
2
answers
83
views
Recursion method is invoked even after loop condition is met
public int removeMin(Integer[] arr, int count) {
Integer[] tempArr = new Integer[arr.length -1];
int index = 0;
for (int i = 1; i<arr.length; i++) {
tempArr[index] = arr[i] ...
1
vote
0
answers
64
views
Iteration failure in radixsort aka bucket sort algorithm,Radix sort inefficient because it assigns wrong size for bucket 0 according to the unit test
For one of my assignment, I am asked to implement a radix sort algorithm. Here is the code that was written so far:
class RadixSort:
def __init__(self):
self.base = 7
self....
0
votes
1
answer
317
views
Maximum sum of two elements
You are given two arrays each with n integers. Find the maximum sum of two elements. You have to choose one element from the first array, one element from the second array and their indexes have to be ...
0
votes
1
answer
191
views
Is the time complexity of the code snippet less than O(n^2)?
I am new to algorithms and data structures.
Thus, I joined LeetCode to improve my skills.
The first problem is to propose an algorithm whose time complexity is less than O(n^2).
I used the code ...
1
vote
1
answer
91
views
Inserting elements in a specific positions in the existing array
I have a List and Map as below,
val exampleList = List("A","B","C","D","E","A","L","M","N")
val exampleMap = ...
0
votes
5
answers
552
views
How to sort an array according to set bits
Sort an array by number of set bits. For example,
Input: arr = [0,1,2,3,4,5,6,7,8]
Output: [0,1,2,4,8,3,5,6,7]
example 2
Input: arr = [1024,512,256,128,64,32,16,8,4,2,1]
Output: [1,2,4,8,16,32,64,...
0
votes
2
answers
58
views
trying to adjust addition function to a subtraction function
I have this function:
static int[] AddArrays(int[] a, int[] b)
{
Array.Reverse(a);
Array.Reverse(b);
int[] result = new int[Math.Max(a.Length, b....
0
votes
1
answer
59
views
inaccurate results with function to add an array of digits together
so i have this function:
static int[] AddArrays(int[] a, int[] b)
{
int length1 = a.Length;
int length2 = b.Length;
int carry = 0;
int ...
2
votes
1
answer
233
views
Efficient way of approaching the Subset Sum Problem with very large input sets
The problem I am facing:
I need to find a way to deal with very large sets (3 to 10000000) of positive and negative ints, this seemed relatively impossible based off of previous experiments.
However, ...
1
vote
2
answers
165
views
Iterating and searching for element in n-dimensional nested array
here is an array presented
id: 0,
parent: 'p1',
children: [
{
id: 1,
parent: 'p2',
children: [
{
id: 3,
...
1
vote
1
answer
2k
views
Which time-complexity is better O(logM + logN) or O(logM.logN)?
I tried the binary search on the 2D matrix and I got the solution which gives the time compexity to be the O(logN.logM). There exist already a solution which performs it in log(MN)
time. I Only want ...
1
vote
2
answers
181
views
Quicksort implementation bug
After a while, I was trying to implement quickSort on my own.
To me the implementation looks fine, but some test cases are apparently failing.
class MyImplementation {
public int[] sortArray(...
2
votes
2
answers
2k
views
Is there any practical use case for Sleep Sort?
This is one of the most unique and fun sorting algorithms on planet earth, in which the time required to complete sorting depends on the size of each individual element rather than the number of ...
-1
votes
1
answer
115
views
Find the shortest subarray in an array, whose sum of indexes in the given array equals the sum of the elements of the subarray
I'm almost a beginner programmer that wants to learn how to write algorithms.I just wrote a program that finds the shortest subarray(subset) in an array, whose sum of indexes in the given array equals ...
-1
votes
1
answer
830
views
What is the Space complexity of initializing 2D array in a function that receive 2D array as input?
I was solving a leetcode problem ,
and wanted to find the space complexity[1] of a function that receives a 2D array of size nxn, and in the function I initialize a new 2D array of size (n-2)x(n-2)
...
0
votes
0
answers
46
views
Can Eller's Algorithm or Recursive Division be used to solve a maze
I am trying to solve a maze using Eller's Algorithm and Recursive Division, Although I know that these two Algorithms can be used to generate a maze but is it possible to solve one using either Eller'...
0
votes
1
answer
220
views
MovingSum of list of integers
I want to calculate the moving sum of a list of integers with a window of size 3. I have a class as such:
class MovingSum:
def __init__(self, window=3):
self.window = window
def ...
-1
votes
2
answers
104
views
Function for "Binary Search" does not work correctly
When I Run my code And take input of array in ascending order from user the function which i have made runs and if the i search the middle number from array to find its location the code runs ...
-2
votes
1
answer
766
views
Convert array of paths into a Tree in JavaScript [closed]
Given this input:
const paths = ["src", "src/components", "src/components/index.ts", "src/utils", "src/configuration/config.ts", "another/file.ts&...
2
votes
1
answer
149
views
Find the largest difference in every subarray [i, j]
I have an array p with integers .
I need to compute d[i, j] for all (i, j) : i<j , where :
d[i, j] = max (p[b] - p[a]) , i<=a<b<=j
I know how to solve this for d[1,n] in O(n) . But for ...
1
vote
2
answers
94
views
Algorithm for finding a set of N items based on percentages of how many times an item may appear
Let's say I have M boxes of different colors and sizes. I need to find a set of N boxes that contain different combinations of colors and sizes.
For example, the user could say they want the final set ...
0
votes
0
answers
123
views
Decrementing Array Round Robin Style
I have an array that can have a really large size holding values I need to decrement to 0. And I have a total amount I can decrement this array by. The problem is I want to decrement it round robin ...
0
votes
1
answer
168
views
I am having trouble visualizing sorting algorithms
I am trying to visualize a sorting algorithm with react, my algorithm works fine but i want to link it to divs on the DOM that change their height as the sorting algorithms sorts a given array, i have ...
0
votes
2
answers
304
views
new Array(n).map((_, index) => index) cannot initialize an array
new Array(n) will create an array of n * undefined, so why can't you use new Array(n).map((_, index) => index) function to get [0, 1, 2 ..., n-1 ] What about arrays like this?
I know that new Array(...
2
votes
2
answers
2k
views
Euclidean distance or cosine similarity between columns with vectors in PySpark
I have a Spark dataframe in the following form:
> df1
+---------------+----------------+
| vector1| vector2|
+---------------+----------------+
|[[0.9,0.5,0.2]]| [[0.1,0.3,0.2]]|
|[...
0
votes
1
answer
409
views
Shifting all elements in an array with one pass and no second array in Java
I'm trying to figure out how to do a value shift in an array by one value incrementally in Java, without using a second array to store values. Is there a way to just have a couple (1-3ish) of values ...
0
votes
1
answer
89
views
Comparing Two Lists, Updating if Different
I have a program written in JavaScript that runs CRUD operations depending if the items in one lists are different from the other. Basically comparing two lists, main list and mirror list. The ...
0
votes
1
answer
322
views
Merge overlapping intervals behavior
I'm attempting to solve the merge overlapping intervals problem and have the working solution below. However there's a piece of logic that im having problems understanding. Namely the part where the ...
0
votes
2
answers
61
views
Algorithm not functioning for sort squared array based off of input
I'm working on building an algorithm that sorts in place for an array of nondecreasing integers, and it's not passing some of my tests. I was wondering why? I've included a sample input and output as ...
1
vote
3
answers
798
views
looking for an std algorithm to replace a simple for-loop
Is there a standard algorithm in the library that does the job of the following for-loop?
#include <iostream>
#include <vector>
#include <iterator>
#include <algorithm>
int ...
1
vote
1
answer
246
views
How to convert 3-dimensional array of fixed size to a reference-style?
I work with 3-dimensional arrays of fixed size. For the first time I used passing by value.
fn some_func(matrix: [[[f64;Z];Y];X]) {// body}
fn main() {
let m = [[[0.0;Z];Y];X];
some_func(m);
}...
-1
votes
4
answers
1k
views
Sum and combine similar object elements in array if condition is met - Javascript
I have an array of this shape:
[
{
unique: 8,
views: 24,
name: "https://l.instagram.com/",
sec: 5.39,
},
{
unique: 2,
views: 20,
name: "",
sec:...
2
votes
0
answers
1k
views
Minimum adjacent swaps to group equal elements in an array
Given an array L containing N elements, from an alphabet of M distinct elements, is there an effective way to calculate the minimum number of swaps of two adjacent elements such that we group all the ...
0
votes
1
answer
185
views
CSES range query section question salary queries problem
I am trying to solve cses salary queries (https://cses.fi/problemset/task/1144/)
Question: I will make a frequency array of salaries and I will use coordinate compression but while update I have to ...
1
vote
1
answer
158
views
Why does the method not compute maximum greater than 65537?
public int longestConsecutive(int[] nums) {
Map<Integer, Boolean> numMap = new ConcurrentHashMap<>();
Map<Integer, Integer> maxMap = new ConcurrentHashMap<>();
...
0
votes
2
answers
341
views
How to generate row-wise permutations of a 2d Array?
I recently got this question (paraphrased below) in a practice interview test and it still stumps me:
Given a 2d array A, generate a list of row-wise 1d array permutations from it.
A = [
[1],
...
0
votes
1
answer
257
views
find maximum GCD of an array of triplets
you are given an array of triplets of size N, we have to choose one number from each triplet, forming a new array of N size, such that the GCD of the numbers in the new array is maximum.
example:
an ...
1
vote
2
answers
94
views
Return multidimensional array from given array
I have the following array: [1,2,3,4,5,6,7,8,9]
And I have to return based on a group and step parameters the following values
E.g.:
group = 3;
step = 3;
[
[1,2,3],
[4,5,6],
[7,8,9],
[1,2,3]
]
group =...
0
votes
2
answers
208
views
Replace all occurances of a number in a matrix python
The input is a multi-line string containing the matrix:
input = '''22 13 17 11 0
8 2 23 4 24
21 9 7 16 7
6 10 3 7 5
1 12 20 15 19'''
I have to replace all the occurrences of, let's say 7 ...
0
votes
1
answer
479
views
Why quick sort is always slower than bubble sort in my case?
They use same array:
QUICK SORT Time: 3159 miliseconds (array length 10K)
Bubble SORT Time: 1373 miliseconds (array length 10K)
I'm trying to compare time of the sorting using quick and bubble sort ...