You are developing the habit of "Safe Programming." You are given N unsigned integer data types of sizes (in bits) a1, a2, a3, ... , an. If the ith datatype has size ai bits, you can store all integers from 0 to 2ai -1.
The rule of safe programming is as follows:
Task
Output 1 if it is "Safe Programming", else output 0.
Example
Assumptions
Approach
The given bit sizes are 4, 3, and 7 bits.
Take n = 7. n can be represented by the data type of size a2 = 3 bits. { 23-1 = 7}
Here a1 = 4 and a3 = 7 are present in the given array such that a1 > a2 and a3 > a2.
So, according to the rule, we must be able to represent n3 = 343 using the given bit sizes i.e. 4 and 7 bits.
The maximum number that can be represented by the given sizes of bits is 2a3 - 1 = 27 - 1 = 127.
Clearly, 343 > 127. Hence we fail to represent 343 from the given bit sizes.
Hence answer will be 0.
Function description
Complete the SafeProgramming() function, which takes the following arguments, and returns true if it is safe programming, otherwise, returns false:
Input format
Output Format
Output 1 if it is safe programming; else, output 0.
Constraints
2≤N≤1e6
3≤a[i]≤1e7
Code snippets (also called starter code/boilerplate code)
This question has code snippets for C, CPP, Java, and Python.
Given
Approach
The given bit sizes are 3, 10, 3, and 3.
Take n = 7. It can be represented by the data type of size a1 = 3 bits. { 23-1 = 7 }
Here a2 = 10 is present in the given array such that a2 > a1.
So, according to the rule, we must be able to represent n3 = 343 using the given bit sizes i.e. 10 bits.
The maximum number that can be represented by 10 bits is 2a2 - 1 = 210 - 1 = 1023.
Clearly, 343 < 1023. Hence we can represent 343 from the given bit sizes.
Hence answer will be 1.