There are 100 doors in a row, all doors are closed. If you’re close, you’ll be able to go through the doors.
')
In the first walk, the person toggles every door.
In the second walk, the person toggles every second door, ie, the 2nd, 4th, 6th, 8th, ...
In the third walk, the person toggles every third door, ie 3rd, 6th, 9th, ...
... ... ...
In 100th walk, the person toggles 100th door.
Which doors are open in the end?
There are three light bulbs. Outside the room there are three switches, connected to the bulbs. You can change them. Identify each switch with its bulb.
Write a program to test if it’s not a one line code.
Found in O (log n) time through binary search. But suppose we rotate an ascending order sorted array at some pivot beforehand. So for instance, 1 2 3 4 5 might become 3 4 5 1 2. In the (log n) time.
The number of possible numbers is a print of -1.
N <= 1,000,000,000.
Examples:
Input: N = 21
Output: X = 15
Explanation: X + its digit sum
= 15 + 1 + 5
= 21
Input: N = 5
Output: -1
Input: N = 100000001
Output: X = 99999937
X = 1,000,000,000
#include<stdio.h> #include<stdbool.h> #include<math.h> /* Function to check if x is power of 2*/ bool isPowerOfTwo(int n) { return (ceil(log2(n)) == floor(log2(n))); } // Driver program int main() { isPowerOfTwo(31)? printf("Yes\n"): printf("No\n"); isPowerOfTwo(64)? printf("Yes\n"): printf("No\n"); return 0; }
/* Program to search an element in a sorted and pivoted array*/ #include <stdio.h> int findPivot(int[], int, int); int binarySearch(int[], int, int, int); /* Searches an element key in a pivoted sorted array arrp[] of size n */ int pivotedBinarySearch(int arr[], int n, int key) { int pivot = findPivot(arr, 0, n-1); // If we didn't find a pivot, then array is not rotated at all if (pivot == -1) return binarySearch(arr, 0, n-1, key); // If we found a pivot, then first compare with pivot and then // search in two subarrays around pivot if (arr[pivot] == key) return pivot; if (arr[0] <= key) return binarySearch(arr, 0, pivot-1, key); return binarySearch(arr, pivot+1, n-1, key); } /* Function to get pivot. For array 3, 4, 5, 6, 1, 2 it returns 3 (index of 6) */ int findPivot(int arr[], int low, int high) { // base cases if (high < low) return -1; if (high == low) return low; int mid = (low + high)/2; /*low + (high - low)/2;*/ if (mid < high && arr[mid] > arr[mid + 1]) return mid; if (mid > low && arr[mid] < arr[mid - 1]) return (mid-1); if (arr[low] >= arr[mid]) return findPivot(arr, low, mid-1); return findPivot(arr, mid + 1, high); } /* Standard Binary Search function*/ int binarySearch(int arr[], int low, int high, int key) { if (high < low) return -1; int mid = (low + high)/2; /*low + (high - low)/2;*/ if (key == arr[mid]) return mid; if (key > arr[mid]) return binarySearch(arr, (mid + 1), high, key); return binarySearch(arr, low, (mid -1), key); } /* Driver program to check above functions */ int main() { // Let us search 3 in below array int arr1[] = {5, 6, 7, 8, 9, 10, 1, 2, 3}; int n = sizeof(arr1)/sizeof(arr1[0]); int key = 3; printf("Index: %dn", pivotedBinarySearch(arr1, n, key)); return 0; }
#include <cmath> #include <cstdlib> #include <iostream> #include <vector> using namespace std; // Computing the sum of digits of x int sumOfDigits(long int x) { int sum = 0; while (x > 0) { sum += x % 10; x /= 10; } return sum; } // Checks for 100 numbers on both left // and right side of the given number to // find such numbers X such that X + // sumOfDigits(X) = N and updates the answer // vector accordingly void compute(vector<long int>& answer, long int n) { // Checking for all possibilities of // the answer for (int i = 0; i <= 100; i++) { // Evaluating the value on the left // side of the given number long int valueOnLeft = abs(n – i) + sumOfDigits(abs(n – i)); // Evaluating the value on the right // side of the given number long int valueOnRight = n + i + sumOfDigits(n + i); // Checking the condition of equality // on both sides of the given number N // and updating the answer vector if (valueOnLeft == n) answer.push_back(abs(n – i)); if (valueOnRight == n) answer.push_back(n + i); } } // Driver Function int main() { long int N = 100000001; vector<long int> answer; compute(answer, N); // If no solution exists, print -1 if (answer.size() == 0) cout << -1; else { // If one or more solutions are possible, // printing them! for (auto it = answer.begin(); it != answer.end(); ++it) cout << "X = " << (*it) << endl; } return 0; }
Source: https://habr.com/ru/post/348746/
All Articles