There are 3 buckets of 8, 5, 3 liters in front of you. The bucket with capacity 8 is completely filled with ice water. 8 liters of 2 liters of each liter into 2 portions of 4 liter each using above 3 buckets.
There are 4 persons (A, B, C and D).
A takes 1 minute to cross the bridge.
B takes 2 minutes to cross the bridge.
C takes 5 minutes to cross the bridge.
D takes 8 minutes to cross the bridge.
Cannot be crossed without the torch. There is no way for you to go.
Can they all cross the bridge in 15 minutes?
Given an array of A [] const 0s, 1s and 2s, write a function that sorts A []. The functions should be put all 0s first, then all 1s and all 2s in last.
Examples:
Input: {0, 1, 2, 0, 1, 2}
Output: {0, 0, 1, 1, 2, 2}
Input: {0, 1, 1, 0, 1, 2, 1, 2, 0, 0, 0, 1}
Output: {0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 2}
Given a generator producing random numbers. If you’re still a number of people.
This is a random number from 1 / n. with O (1) extra space?
The sum of selected numbers. If you want to select a number, if you want, you need to add one Repeat these steps until the array gets empty. The sum of selected numbers.
Note: We have to delete A i + 1 and A i -1 elements if I’m 1 and A i – 1 .
Examples:
Input: a [] = {1, 2, 3}
Output: 4
Explanation: At first step we select 1, so 1 and 2 are deleted from the sequence leaving us with 3.
Then we select 3 from the sequence and delete it. So the sum of selected numbers is 1 + 3 = 4.
Input: a [] = {1, 2, 2, 2, 3, 4}
Output: 10
Explanation: Select one of the 2s, 2-1, 2 + 1 will be deleted from the 2, 2, 4, 1, 3, 1, 3, 1, and 3 are deleted. Select 2 in the last step. We get a sum of 2 + 2 + 2 + 4 = 10 which is the maximum possible.
// An efficient C program to randomly select a number from stream of numbers.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
// A function to randomly select a item from stream[0], stream[1], .. stream[i-1]
int selectRandom(int x)
{
static int res; // The resultant random number
static int count = 0; //Count of numbers visited so far in stream
count++; // increment count of numbers seen so far
// If this is the first element from stream, return it
if (count == 1)
res = x;
else
{
// Generate a random number from 0 to count - 1
int i = rand() % count;
// Replace the prev random number with new number with 1/count probability
if (i == count - 1)
res = x;
}
return res;
}
// Driver program to test above function.
int main()
{
int stream[] = {1, 2, 3, 4};
int n = sizeof(stream)/sizeof(stream[0]);
// Use a different seed value for every run.
srand(time(NULL));
for (int i = 0; i < n; ++i)
printf("Random number from first %d numbers is %d \n",
i+1, selectRandom(stream[i]));
return 0;
}
// CPP program to Maximize the sum of selected
// numbers by deleting three consecutive numbers.
#include <bits/stdc++.h>
using namespace std;
// function to maximize the sum of selected numbers
int maximizeSum(int a[], int n) {
// stores the occurrences of the numbers
unordered_map<int, int> ans;
// marks the occurrence of every number in the sequence
for (int i = 0; i < n; i++)
ans[a[i]]++;
// maximum in the sequence
int maximum = *max_element(a, a + n);
// traverse till maximum and apply the recurrence relation
for (int i = 2; i <= maximum; i++)
ans[i] = max(ans[i - 1], ans[i - 2] + ans[i] * i);
// return the ans stored in the index of maximum
return ans[maximum];
}
// Driver code
int main()
{
int a[] = {1, 2, 3};
int n = sizeof(a) / sizeof(a[0]);
cout << maximizeSum(a, n);
return 0;
}
Source: https://habr.com/ru/post/350788/
All Articles