Safe programming
Practice
3.3 (6 votes)
Basics of greedy algorithms
Greedy algorithms
Algorithms
Sorting
Problem
86% Success 787 Attempts 20 Points 1s Time Limit 256MB Memory 1024 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 a> 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 2a- 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.

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...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:20
16 votes
Tags:
AlgorithmsEasyGreedy Algorithms
Points:20
12 votes
Tags:
Binary SearchEasyGreedy AlgorithmsHiringSorting
Points:20
322 votes
Tags:
Ad-HocEasyGreedy Algorithms