array-algorithms

Finding the sum of common elements between n number of arrays in java

杀马特。学长 韩版系。学妹 提交于 2019-12-10 15:09:29
问题 I have a program that sums the common elements of two arrays. For that I used two for loops and if I have three then I could use three for loops. But how to sum the common elements of n number of arrays where n is coming during run time. I don't know how to change the number of loops during run time or is there any other relevant concept for this ? Here is the code I've tried for summing twoarrays: import java.util.Scanner; public class Sample { public static void main(String... args) {

how can I make this faster. By removing one element from an array, check if array is increasing seuence

℡╲_俬逩灬. 提交于 2019-12-10 10:29:49
问题 Given a sequence of integers as an array, determine whether it is possible to obtain a strictly increasing sequence by removing no more than one element from the array. Example For sequence = [1, 3, 2, 1], the output should be almostIncreasingSequence(sequence) = false; There is no one element in this array that can be removed in order to get a strictly increasing sequence. For sequence = [1, 3, 2], the output should be almostIncreasingSequence(sequence) = true. You can remove 3 from the

array: convert index of one dimensional array to a vector index of a multidimensional array

▼魔方 西西 提交于 2019-12-06 07:58:40
问题 It will be a long question, please, take a deep breath before reading. I want to understand what would be the fastest algorithm to convert index of one dimensional array to a vector index of a multidimensional array. Let's proceed with an example to understand why do I need it: I have a 2 dimensional array: Array[i1][i2] i1 runs from i1_b=0 to i1_e=2 i2 runs from i2_b=0 to i2_e=1 So this array is outputted into the file line by line: Array[0][0] Array[0][1] Array[0][2] Array[1][0] Array[1][1]

calculating the sum of nodes in a single verticle line of a binary tree

為{幸葍}努か 提交于 2019-12-06 05:52:34
问题 For a binary tree i want to get the sum of all nodes that fall in a single verticle line. I want the sum of nodes in each verticle node A / \ B C / \ / \ D E F G / \ H I IF you look at above tee line 0 A E F so sum = A+E+F line -1 B I so sum = B +I line 1 C so sum = C line 2 G so sum = G I implemented following algorithm Map<Integer,Integere> mp = new HashMap<Integer,Integer>() calculate(root,0); void calculate(Node node, int pos){ if(node==null) return ; if(mp.containsKey(pos) ){ int val =

Strassen Vinograd Algorithm

我与影子孤独终老i 提交于 2019-12-05 20:23:38
I got a task to write a Strassen Vinograd algorithm in C++. I have written it twice, but first version of my code don't even works. The result is correct in the lower left corner of result matrix. And my second version is running slower than naive algorithm, even with N = 64+. So i need help, what am i doing wrong? Important note: i'm not allowed to use dinamic matrix in recursion and structures. In addition it is better to do the multiplication without copying, using the coordinates of the corner element of the submatrices. Incorrect: #include "pch.h" #include<iostream> #include<cstdio>

array: convert index of one dimensional array to a vector index of a multidimensional array

随声附和 提交于 2019-12-04 11:39:11
It will be a long question, please, take a deep breath before reading. I want to understand what would be the fastest algorithm to convert index of one dimensional array to a vector index of a multidimensional array. Let's proceed with an example to understand why do I need it: I have a 2 dimensional array: Array[i1][i2] i1 runs from i1_b=0 to i1_e=2 i2 runs from i2_b=0 to i2_e=1 So this array is outputted into the file line by line: Array[0][0] Array[0][1] Array[0][2] Array[1][0] Array[1][1] Array[1][2] Now I read the file line by line and index k is the number of the line being read last. I

Given an array [a1b2c3d4] convert to [abcd1234]

北城以北 提交于 2019-12-03 16:10:46
问题 Constraints : O(1) space O(n) Time It is not a homework question just a interesting question I came across. Here are some solutions I could think of but nothing that does it in given constraints. Method 1 *With O(n) memory * Divide the array in two parts recursively. ( keep dividing till the size <=2 for each sub problem ) Sort each sub problem with array first and numbers at end. Merge the sub problem arrays Method 2 In O(n log n) time Sort the array based Lexicographical order it becomes

Finding overlapping data in arrays

混江龙づ霸主 提交于 2019-12-03 12:15:12
We are writing a C# application that will help to remove unnecessary data repeaters. A repeater can only be removed in the case that all data it receives are received by other repeaters. What we need as a first step is explained bellow: I have collection of int arrays, for example a. {1, 2, 3, 4, 5} b. {2, 4, 6, 7} c. {1, 3, 5, 8, 11, 100} It may be thousands of such arrays. I need to find arrays that can be removed. An array can only be removed in the case that all its numbers are included in other arrays. In the example above, array a can be removed because its numbers 2 and 4 are in array b

Given an array [a1b2c3d4] convert to [abcd1234]

我是研究僧i 提交于 2019-12-03 05:25:22
Constraints : O(1) space O(n) Time It is not a homework question just a interesting question I came across. Here are some solutions I could think of but nothing that does it in given constraints. Method 1 *With O(n) memory * Divide the array in two parts recursively. ( keep dividing till the size <=2 for each sub problem ) Sort each sub problem with array first and numbers at end. Merge the sub problem arrays Method 2 In O(n log n) time Sort the array based Lexicographical order it becomes 1234abcd Reverse both halves of array 4321dcba Reverse the whole string abcd1234 Method 3 If range of

finding a pair of integers in a sorted array which sum to K

杀马特。学长 韩版系。学妹 提交于 2019-12-01 20:32:09
问题 Given a sorted array of integers, how can we find a pair of integers that sum to K? e.g. array = 1,3,5,6,0 , K = 6 , the answer is 1 and 5. Time complexity should be minimized. 回答1: You may want to look at this blog post: http://www.codingatwork.com/2011/07/array-sum/ My approach would be to do a binary search of the list for K/2 , then walk one variable a left and another variable b right trying to find a solution a+b=K . The idea would be to start a at the largest number less than or equal