Professional Documents
Culture Documents
For example,
SOLUTION
#include <iostream>
#include <algorithm>
int low = 0;
int high = n - 1;
// sum found
cout << "Pair found (" << nums[low] << ", " << nums[high] << ")\n";
return;
// increment `low` index if the total is less than the desired sum;
// decrement `high` index if the total is more than the desired sum
int main()
int nums[] = { 8, 7, 2, 5, 3, 1 };
int n = sizeof(nums)/sizeof(nums[0]);
findPair(nums, n, target);
return 0;
QUESTION 2
#include <iostream>
#include <unordered_set>
unordered_set<int> set;
// insert 0 into the set to handle the case when subarray with
set.insert(0);
int sum = 0;
// traverse the given array
sum += nums[i];
if (set.find(sum) != set.end()) {
return true;
else {
set.insert(sum);
return false;
int main()
int n = sizeof(nums)/sizeof(nums[0]);
hasZeroSumSubarray(nums, n) ?
return 0;
QUESTION 3
#include <iostream>
#include <numeric>
int right = 0;
right += A[i];
int main()
findEquilibriumIndex(A, n);
return 0;
}
QUESTION 4
#include <stdio.h>
return (x < y) ? x : y;
return (x > y) ? x : y;
// base case
if (n == 0) {
return 0;
return max_so_far;
int main(void)
{
findMaxProduct(arr, n));
return 0;
QUESTION 5
#include <iostream>
#include <cmath>
// Function to decode given array to get back the original array elements
// base case
if (m == 0 || m == 2) {
return;
}
// calculate the size of the original array
int n = (sqrt(8*m + 1) + 1) / 2;
int A[n];
if (n == 1 || m == 1) {
A[0] = inp[0];
else if (n == 2) {
else {
int main()
int inp[] = { 3, 4, 5, 6, 5, 6, 7, 7, 8, 9 };
int m = sizeof(inp)/sizeof(inp[0]);
decode(inp, m);
return 0;
QUESTION 6
#include <iostream>
#include <unordered_set>
#include <algorithm>
// array is unsorted
unordered_set<int> set;
cout << "(" << arr[i] << ", " << arr[i] - diff<< ")\n";
cout << "(" << arr[i] + diff << ", " << arr[i] << ")\n";
set.insert(arr[i]);
}
int main()
int diff = 3;
findPair(arr, n, diff);
return 0;
QUESTION 7
#include <iostream>
#include <algorithm>
// subarray `arr[j+1…n)`
low++;
high--;
else {
cout << "(" << arr[i] << " " << arr[j] << " " <<
arr[low] << " " << arr[high] << ")\n";
low++, high--;
int main()
int arr[] = { 2, 7, 4, 0, 9, 5, 1, 3 };
quadTuple(arr, n, target);
return 0;
QUESTION 8
#include <vector>
#include <unordered_map>
#include <cstdlib>
#include <ctime>
// Function to generate random nums from a vector according to the given probabilities
int n = nums.size();
if (n != probability.size()) {
prob_sum[0] = probability[0];
// generate a random integer from 1 to 100 and check where it lies in `prob_sum[]`
int r = (rand() % 100) + 1;
return nums[0];
return nums[i];
int main()
srand(time(NULL));
freq[val]++;
cout << nums[i] << " ~ " << freq[nums[i]]/10000.0 << "%" << endl;
return 0;
QUESTION 9
#include <vector>
#include <climits>
if (j <= i + 1) {
return 0;
/*
(M[i+1]) × (M[i+2]………………M[j])
(M[i+1]M[i+2]) × (M[i+3…………M[j])
…
(M[i+1]M[i+2]…………M[j-1]) × (M[j])
*/
min = cost;
return min;
}
// Matrix Chain Multiplication Problem
int main()
int n = dims.size();
return 0;
QUESTION 10
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#define N 10
return (x < y) ? x : y;
}
void merge(int A[], int temp[], int from, int mid, int to)
// loop till no elements are left in the left and right runs
temp[k++] = A[i++];
else {
temp[k++] = A[j++];
temp[k++] = A[i++];
A[i] = temp[i];
// m = [1, 2, 4, 8, 16…]
// for m = 1, i = 0, 2, 4, 6, 8…
// for m = 2, i = 0, 4, 8…
// for m = 4, i = 0, 8…
// …
int from = i;
int mid = i + m - 1;
printf("\n");
int main()
srand(time(NULL));
}
printf("Original array: ");
printArray(A);
printArray(A);
return 0;