It works and I think it has a constant time complexity due to the loops not being dependent on variable inputs, but I want to find out if there is a more efficient, cleaner, or simpler way to write this. int * getRandom( ) { static int choices[5] = {}; srand((unsigned)time(NULL)); for (int i = ..

#### Category : array-algorithms

My goal is creating an array of 5 unique integers between 1 and 20. Is there a better algorithm than what I use below? It works and I think it has a constant time complexity due to the loops not being dependent on variable inputs, but I want to find out if there is a ..

I have tried logging from inside the "MergeSort" to see what is wrong and outputting the array A after it has been modified and I am seeing bizarre values like 980498560986, they as though they mite be unallocated memory or memory addresses. Any help is greatly appreciated. infinity is used for the edge case of ..

https://www.hackerrank.com/contests/cp-tutorial/challenges/cp-tutorial-11/problem below is my solution But the problem I am having is that the output prints the statement exactly the no of times the element is repeated. Can somebody help me out with this.enter image description here #include <iostream> #include <algorithm> using namespace std;“ int main() { int n; cin >> n; int a[1000005]; for(int ..

i was looking for some D&C algorithms and came out founding this one int sumArray(int anArray[], int size) { //base case if (size == 0) { return 0; } else if (size == 1) { return anArray[0]; } //divide and conquer int mid = size / 2; int rsize = size – mid; int lsum ..

I recently stumbled upon a problem on HackerEarth. The problem statement asks us to find the min(Arr[i] xor Arr[j]) for the given array In the editorial section, the problem author did something like this sort(a,a+n); long long ans = INT_MAX; for(int i=0;i<n-1;i++) { ans = min(ans, a[i]^a[i+1]); } The author mentioned that the above code ..

I have a multidimensional array with 20 columns and ‘n’ rows. I’d like to start with the first column, and return the index of the first element in that column satisfying some condition (say, >3). Then, I’d like to move to the second column, and find the index of the ‘nearest element’ satisfying some condition. ..

## Recent Comments