C Programming – HackerRank Solution | Maximum of an array power of 2 |

(HackerRank) Write a Modular C Programming code to solve the Maximum of an array power of 2, Given an array of ‘N’ integers check if the maximum of an array is a power of 2 or not

Given an array of ‘N’ integers check if the maximum of an array is a power of 2 or not.

Input Format

First Line contains ‘N’ the number of array elements.

Second-line contains ‘N’ array elements separated by space.

Constraints

N>0 and N<=10^5

0

Output Format

Display YES if the maximum number is the power of 2, else display NO

Sample Input 0

-1

Sample Output 0

Invalid

Sample Input 1

7
10 11 12 13 14 15 16

Sample Output 1

YES

Explanation 1

Maximum element of an array is 16, which is power of 2. (2 power 4 = 16)

Refer : C Programming HackerRank all solutions for Loops | Arrays | strings | Data Structures | Linked lists | Stacks | Queues | Binary Trees

 

CODE:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>

void maximum(int a[],int n)
{
    int status=0,max=0;
    for(int i=0;i<n;i++)
    {
        if(max<a[i])
        {
            max=a[i];
        }
    }
    for(int i=1;i<60;i++) { if(max==pow(2,i)) { status=1; break; } } if(status==1) { printf("YES\n"); } else printf("NO\n"); } int main() { int n,a[100],i; scanf("%d",&n); if(n>0&&n<=100000)
    {
        for(i=0;i<n;i++)
        {
           scanf("%d",&a[i]);
        }
        maximum(a,n);
    }
    else
    {
        printf("Invalid");
    }
    return 0;
}

OUTPUT

Congratulations, you passed the sample test case.

Click the Submit Code button to run your code against all the test cases.

Input (stdin)

-1
Your Output (stdout)

Invalid
Expected Output

Invalid

 

Please find some more codes of Loops, Condition Statements, 1D Arrays, 2D Arrays, Strings, Pointers, Data Structures, Files, Linked lists, Stacks, Queues, Binary Trees, MISC, Solved model question papers & Hacker Rank all solutions on the below page:

Top 100+ C Programming codes – KLE Technological University

Leave a Comment

Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

Welcome to FactsPrime

Sorry, We have detected that you have activated Ad-Blocker. Please Consider supporting us by disabling your Ad Blocker, It helps us in maintaining this website. To View the content, Please disable adblocker and refresh the page.

Thank You !!!