Safe programming
86% Success790 Attempts20 Points1s Time Limit256MB Memory1024 KB Max Code

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:

  • If n is a number that can be represented by the bit size ai, and if at least one aj > ai is present in the given array, then we must be able to represent n3 by any one of the bit sizes given in array a.

Task

Output 1 if it is "Safe Programming", else output 0.

Example

Assumptions

  • N = 3
  • A = [4, 3, 7]

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:

  • N: Represents the number of available variations of bit sizes
  • a[ ]: Represents the array of Available variations of bit sizes

Input format

  • The first line contains N denoting the number of available variations of bit sizes
  • The second line contains N space-separated integers denoting the available bit sizes.

Output Format

Output 1 if it is safe programming; else, output 0.

Constraints

\(2 \le N \le 1e6\)

\(3 \le a[i] \le 1e7\)

Code snippets (also called starter code/boilerplate code)

This question has code snippets for C, CPP, Java, and Python.

Examples
Input
4
3 10 3 3 
Output
1
Explanation

Given

  • N = 4
  • a = [3,10, 3, 3]

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.

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading Editor...
Results