Header Ads Widget

Power Pokemon coding ninja solution

 Problem:-

A Power Pokemon Couple is a pair of pokemons with a sum of their power levels equal to a power of two.

You are given an array power, containing the power levels of many pokemons. You can pick any two to make a power couple.

Note: Pokemons with different indices are considered different even if they have the same power level. Since the answer can be huge, print it modulo 10^9 + 7

Input Format:
First line of input contains a integer N, representing the number of pokemons.
Second line of input contains N space separated integers representing the power levels of the pokemons
Constrains:
1 <= power.length <= 10^5
0 <= power[i] <= 22
Sample Input 1:
5
1 3 5 7 9
Sample Output 1:
4
Explanation:
 The power pokemon couples formed are (1,3), (1,7), (3,5) and, (7,9).
 Their respective sums are 4, 8, 8, and 16, all of which are powers of 2.
Sample Input 2:
6
24 8 7 1 8 57
Sample Output 2:
5

Solution:-

#include <bits/stdc++.h>
using namespace std;
int main()
{
    //Write your code here
    int n;
    cin>>n;
    int arr[n];
    for(int i=0;i<n;i++)
      cin>>arr[i];
    int count=0;
    for(int i=0;i<n;i++)
    {
        for(int j=i+1;j<n;j++)
        {
            int a=arr[i]+arr[j];
           
            if(ceil(log2(a))==floor(log2(a)))
              count++;
        }
    }
    cout<<count;
}
#include <bits/stdc++.h>
using namespace std;
int main()
{
    //Write your code here
    int n;
    cin>>n;
    int arr[n];
    for(int i=0;i<n;i++)
      cin>>arr[i];
    int count=0;
    for(int i=0;i<n;i++)
    {
        for(int j=i+1;j<n;j++)
        {
            int a=arr[i]+arr[j];
           
            if(ceil(log2(a))==floor(log2(a)))
              count++;
        }
    }
    cout<<count;
}


Recommended Post :-

HCL Coding Questions:-

Capgemini Coding Questions:-

Companies interview:-

Full C course:-    

Key points:-

Cracking the coding interview:-

 Array and string:-

Tree and graph:-

Hackerearth Problems:-

Hackerrank Problems:-

Data structure:-

 MCQs:-


Post a Comment

0 Comments