array-algorithms

Given an array of integers, find the first missing positive integer in linear time and constant space

百般思念 提交于 2020-07-17 05:41:10
问题 In other words, find the lowest positive integer that does not exist in the array. The array can contain duplicates and negative numbers as well. This question was asked by Stripe in it's programming interview. I have devised a solution for the same as below: #include<bits/stdc++.h> using namespace std; int main(){ int arr[]={1,-1,-5,-3,3,4,2,8}; int size= sizeof(arr)/sizeof(arr[0]); sort(arr, arr+size); int min=1; for(int i=0; i<size; i++){ if(arr[i]>min) break; if(arr[i]==min) min=min+1; }

Strassen Vinograd Algorithm

牧云@^-^@ 提交于 2020-01-13 18:10:48
问题 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

Strassen Vinograd Algorithm

落爺英雄遲暮 提交于 2020-01-13 18:08:31
问题 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

Longest Contiguous Subarray with Average Greater than or Equal to k

非 Y 不嫁゛ 提交于 2019-12-30 01:30:23
问题 Consider an array of N integers. Find the longest contiguous subarray so that the average of its elements is greater (or equal) than a given number k. The obvious answer has O(n^2) complexity. Can we do better? 回答1: We can reduce this problem to longest contiguous subarray with sum >= 0 by subtracting k from all values in O(n) time. Now let's calculate prefix sums: index 0 1 2 3 4 5 6 array 2 -3 3 2 0 -1 prefix 0 2 -1 2 5 5 4 Now this problem is finding the two indices most far apart with

Median of medians algorithm: why divide the array into blocks of size 5

丶灬走出姿态 提交于 2019-12-23 09:39:19
问题 In median-of-medians algorithm, we need to divide the array into chunks of size 5. I am wondering how did the inventors of the algorithms came up with the magic number '5' and not, may be, 7, or 9 or something else? 回答1: I think that if you'll check "Proof of O(n) running time" section of wiki page for medians-of-medians algorithm: The median-calculating recursive call does not exceed worst-case linear behavior because the list of medians is 20% of the size of the list, while the other

Finding overlapping data in arrays

孤人 提交于 2019-12-21 03:56:17
问题 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

how does Float.toString() and Integer.toString() works?

与世无争的帅哥 提交于 2019-12-12 05:57:54
问题 How can i implement an algorithm to convert float or int to string? I found one link http://geeksforgeeks.org/forum/topic/amazon-interview-question-for-software-engineerdeveloper-0-2-years-about-algorithms-13 but i cant understand the algorithm given there 回答1: the numbers 0-9 are sequential in most character encoding so twiddling with the integral value of it will help here: int val; String str=""; while(val>0){ str = ('0'+(val%10)) + str; val /= 10; } 回答2: Here's a sample of how to do the

Convert integer to roman numerals - there must be a better way [closed]

自闭症网瘾萝莉.ら 提交于 2019-12-12 03:46:41
问题 Closed . This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post. Closed 3 years ago . I'm working through the intermediate algorithms in the FreeCodeCamp curriculum. One of them involves converting integers to roman numerals. My solution (presented below) works, however it is very much the "naive" approach, if you will. The task hints that array.splice(), array

Bubble Sort in Python 3

时间秒杀一切 提交于 2019-12-11 15:39:00
问题 Write a bubble sort program in Python 3. A bubble sort is an algorithm that will sort a list of values into order. I am trying to get this result towards the end. Original List: 4, 9, 74, 0, 9, 8, 28, 1 Sorted List: 0, 1, 4, 8, 9, 9, 28, 74 Number of Passes: 6 How can I accomplish it? import sys def bubblesort(mylist): changes = passes = 0 last = len(mylist) swapped = True print("Original List: ", ','.join(map(str, mylist)) ) while swapped: swapped = False for j in range(1, last): if mylist[j

integers which have smallest absolute difference there can be one such pair or multiple such pair

淺唱寂寞╮ 提交于 2019-12-11 02:02:39
问题 sample input= 10 -20 -3916237 -357920 -3620601 7374819 -7330761 30 6246457 -6461594 266854 sample output = -20 30 thanks in advance i am beginner in programing class Main { public static void main(String[] args) { int _a_size = Integer.parseInt(args[0]); Assert.assert2(args.length,_a_size+1); int[] _a = new int[_a_size]; for(int _a_i = 0;_a_i<_a_size;_a_i++) { _a[_a_i] = Integer.parseInt(args[_a_i+1]); } Operation.minDiffrence (_a); } } 回答1: You can do public static List<int[]> minDifference