array-algorithms

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

北城余情 提交于 2019-12-01 19:59:49
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. 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 to K/2 and start b at the smallest number greater than a . Again, use a binary search to find a and let b be

Sum is to be calculated for the numbers in between the array's max and min value. For eg: [1,4] should return 1+2+3+4=10

一世执手 提交于 2019-12-01 13:47:34
It seems that the compiler is not going into the for loop.The sum of the array is to calculated.SumAll([1,4]) should return 10(1+2+3+4) as output. function sumAll(arr) { //return Math.max.apply(Math,arr); //return Math.min.apply(Math,arr); // return "0"; var sum=arr.reduce(function(a,b){ for(var i=Math.min.apply(Math,arr);i<=Math.max.apply(Math,arr);i++){ return a+b; } },0); //return sum; } sumAll([1, 4]); You could use directly the values from the array, without reduce. function sumAll(arr) { var i, sum = 0; for (i = Math.min.apply(null, arr); i <= Math.max.apply(null, arr); i++) { sum += i;

Sum is to be calculated for the numbers in between the array's max and min value. For eg: [1,4] should return 1+2+3+4=10

核能气质少年 提交于 2019-12-01 12:40:30
问题 It seems that the compiler is not going into the for loop.The sum of the array is to calculated.SumAll([1,4]) should return 10(1+2+3+4) as output. function sumAll(arr) { //return Math.max.apply(Math,arr); //return Math.min.apply(Math,arr); // return "0"; var sum=arr.reduce(function(a,b){ for(var i=Math.min.apply(Math,arr);i<=Math.max.apply(Math,arr);i++){ return a+b; } },0); //return sum; } sumAll([1, 4]); 回答1: You could use directly the values from the array, without reduce. function sumAll

Finding a maximum sum contiguous sub array

风流意气都作罢 提交于 2019-12-01 06:22:32
I am writing a code to find the maximum sum contiguous sub array in C. The logic seems fine according to me, but still the output is not correct. Please look into the code. The algorithm divides a bigger array into 2 sub-arrays. It then checks for maximum sum sub-array by examining the left array , right array and also the array containing the midpoint (It will check right and left from the midpoint and then return the maximum sum sub-array containing the midpoint). int* cross_max(int arr[], int low, int mid, int high) { int left_max, left_sum = -2000; int sum = 0; int i; for(i=mid; i>=low;i--

How to find the permutation of a sort in Java

眉间皱痕 提交于 2019-12-01 03:36:35
I want to sort an array and find the index of each element in the sorted order. So for instance if I run this on the array: [3,2,4] I'd get: [1,0,2] Is there an easy way to do this in Java? Louis Wasserman Let's assume your elements are stored in an array. final int[] arr = // elements you want List<Integer> indices = new ArrayList<Integer>(arr.length); for (int i = 0; i < arr.length; i++) { indices.add(i); } Comparator<Integer> comparator = new Comparator<Integer>() { public int compare(Integer i, Integer j) { return Integer.compare(arr[i], arr[j]); } } Collections.sort(indices, comparator);

How to find the permutation of a sort in Java

。_饼干妹妹 提交于 2019-12-01 01:03:16
问题 I want to sort an array and find the index of each element in the sorted order. So for instance if I run this on the array: [3,2,4] I'd get: [1,0,2] Is there an easy way to do this in Java? 回答1: Let's assume your elements are stored in an array. final int[] arr = // elements you want List<Integer> indices = new ArrayList<Integer>(arr.length); for (int i = 0; i < arr.length; i++) { indices.add(i); } Comparator<Integer> comparator = new Comparator<Integer>() { public int compare(Integer i,

Reversing an array without 'reverse' or duplicating an array

限于喜欢 提交于 2019-11-29 08:13:57
I'm trying to solve the following exercise: Reverse an array without using the reverse method, without using a second array, and without duplicating any of the values. I've thought about making the array an object and then updating the array from the end to the beginning but I figured you can just update it as well. Tried something simple like: function reverseArray(array) { for (var i = 0; i < array.length; i++) { // var elem = array.shift(); var elem = array.shift() array.push(elem) } return array } array = ['a', 'b','c','d','e']; reverseArray(array); But that doesn't really change it. Any

Reversing an array without 'reverse' or duplicating an array

丶灬走出姿态 提交于 2019-11-28 01:50:03
问题 I'm trying to solve the following exercise: Reverse an array without using the reverse method, without using a second array, and without duplicating any of the values. I've thought about making the array an object and then updating the array from the end to the beginning but I figured you can just update it as well. Tried something simple like: function reverseArray(array) { for (var i = 0; i < array.length; i++) { // var elem = array.shift(); var elem = array.shift() array.push(elem) }