Ak13
BAN USER
bool canRotate180(int* arr, int size, const std::unordered_map<int, int>& matches) {
int l = 0, r = size-1;
while( l < r) {
auto it = matches.find(arr[l]);
if( it == matches.end()) {
return false;
} else if (it->second != arr[r]) {
return false;
} else {
l++;
r--;
}
}
return true;
}
int main() {
std::unordered_map<int, int> matches({
{0, 0},
{1, 1},
{8, 8},
{6, 9},
{9, 6}
});
int arr1[] = {1,2,4,7,1};
int size = (sizeof arr1/sizeof arr1[0]);
std::cout << canRotate180(arr1, size, matches) << std::endl;
int arr2[] = {1,6,8,9,1};
size = (sizeof arr2/sizeof arr2[0]);
std::cout << canRotate180(arr2, size, matches) << std::endl;
return 0;
}
// This function returns overall maximum path sum in 'res'
// And returns max path sum going through root.
int findMaxUtil(Node* root, int &res)
{
//Base Case
if (root == NULL)
return 0;
// l and r store maximum path sum going through left and
// right child of root respectively
int l = findMaxUtil(root->left,res);
int r = findMaxUtil(root->right,res);
// Max path for parent call of root. This path must
// include at-most one child of root
int max_single = max(max(l, r) + root->data, root->data);
// Max Top represents the sum when the Node under
// consideration is the root of the maxsum path and no
// ancestors of root are there in max sum path
int max_top = max(max_single, l + r + root->data);
res = max(res, max_top); // Store the Maximum Result.
return max_single;
}
// Returns maximum path sum in tree with given root
int findMaxSum(Node *root)
{
// Initialize result
int res = INT_MIN;
// Compute and return result
findMaxUtil(root, res);
return res;
}
Very easily extendable code/
// C++ program to find count of endless points
#include<bits/stdc++.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
using namespace std;
const int MAX = 100;
/* The first string is not used, it is to make
array indexing simple */
string single_digits[10] = { "", "one", "two",
"three", "four","five",
"six", "seven", "eight", "nine"};
/* The first string is not used, it is to make
array indexing simple */
string two_digits[11] = {"", "ten", "eleven", "twelve",
"thirteen", "fourteen",
"fifteen", "sixteen",
"seventeen", "eighteen", "nineteen"};
/* The first two string are not used, they are to make
array indexing simple*/
string tens_multiple[10] = {"", "", "twenty", "thirty", "forty", "fifty",
"sixty", "seventy", "eighty", "ninety"};
string hundred= "hundred";
string power3[] = {"", "thousand", "million"};
string printlessThanThousand(int i) {
string output;
if (i >100) {
output += single_digits[i/100];
output += " ";
output += hundred;
i = i%100;
}
if (i>9 && i < 20) {
output += " ";
output += two_digits[i-9];
return output;
}
if (i>19) {
output += " ";
output += tens_multiple[i/10];
i = i%10;
}
output += " ";
output += single_digits[i];
return output;
}
void convert_to_words(int i)
{
std::string words = std::to_string(i);
/* Base cases */
if (words.size() == 0) {
fprintf(stderr, "empty string\n");
return;
}
if (words.size() > 7) {
fprintf(stderr, "Length more than 7 is not supported\n");
return;
}
if (i == 0) cout << "Zero" << endl;
int allowed = sizeof power3/sizeof power3[0];
string output;
for (int x=allowed; x >= 0; --x) {
int p = pow(1000, x);
if (i/p > 0) {
output += printlessThanThousand(int(i/p));
output += " ";
output += power3[x];
output += " ";
i %= p;
}
}
cout << output << endl;
}
/* Driver program to test above function */
int main(void)
{
convert_to_words(9923);
convert_to_words(523);
convert_to_words(89);
convert_to_words(8989);
convert_to_words(8123989);
return 0;
}
- Ak13 October 23, 2018