Count pairs made up of an element divisible by the other from an array consisting of powers of 2
Last Updated :
26 Oct, 2023
Given an array arr[] consisting of N powers of 2, the task is to count the number of pairs (arr[i], arr[j]) such that i < j and arr[j] is divisible by arr[i].
Examples:
Input: arr[] = {4, 16, 8, 64}
Output: 5
Explanation:
The pairs satisfying the given condition are {4, 16}, {4, 8}, {4, 64}, {16, 64}, {8, 64}.
Input: arr[] = {2, 4, 8, 16}
Output: 6
Explanation:
The pairs satisfying the given condition are {2, 4}, {2, 8}, {2, 16}, {4, 8}, {4, 16}, {8, 16}.
Naive Approach: The simplest approach is to generate all pairs of the given array arr[] and for each pair, check if arr[j] % arr[i] is 0 or not. If found to be true, increment count by 1. Finally, print the value of count after checking for all pairs.
C++
#include <iostream>
#include <vector>
using namespace std;
int countPairs(vector< int > arr) {
int count = 0;
for ( int i = 0; i < arr.size(); i++) {
for ( int j = i+1; j < arr.size(); j++) {
if (arr[j] % arr[i] == 0 && (arr[j] & (arr[j]-1)) == 0) {
count++;
}
}
}
return count;
}
int main() {
vector< int > arr = {4, 16, 8, 64};
cout << countPairs(arr) << endl;
vector< int > arr2 = {2, 4, 8, 16};
cout << countPairs(arr2) << endl;
return 0;
}
|
Java
import java.util.ArrayList;
import java.util.List;
public class Main {
static int countPairs(List<Integer> arr) {
int count = 0 ;
for ( int i = 0 ; i < arr.size(); i++) {
for ( int j = i+ 1 ; j < arr.size(); j++) {
if (arr.get(j) % arr.get(i) == 0 &&
(arr.get(j) & (arr.get(j) - 1 )) == 0 ) {
count++;
}
}
}
return count;
}
public static void main(String[] args) {
List<Integer> arr = new ArrayList<>();
arr.add( 4 );
arr.add( 16 );
arr.add( 8 );
arr.add( 64 );
System.out.println(countPairs(arr));
List<Integer> arr2 = new ArrayList<>();
arr2.add( 2 );
arr2.add( 4 );
arr2.add( 8 );
arr2.add( 16 );
System.out.println(countPairs(arr2));
}
}
|
Python
def count_pairs(arr):
count = 0
for i in range ( len (arr)):
for j in range (i + 1 , len (arr)):
if arr[j] % arr[i] = = 0 and (arr[j] & (arr[j] - 1 )) = = 0 :
count + = 1
return count
arr = [ 4 , 16 , 8 , 64 ]
print (count_pairs(arr))
arr = [ 2 , 4 , 8 , 16 ]
print (count_pairs(arr))
|
C#
using System;
using System.Collections.Generic;
class Program {
static int CountPairs(List< int > arr) {
int count = 0;
for ( int i = 0; i < arr.Count; i++) {
for ( int j = i+1; j < arr.Count; j++) {
if (arr[j] % arr[i] == 0 && (arr[j] & (arr[j]-1)) == 0) {
count++;
}
}
}
return count;
}
static void Main( string [] args) {
List< int > arr = new List< int > {4, 16, 8, 64};
Console.WriteLine(CountPairs(arr));
List< int > arr2 = new List< int > {2, 4, 8, 16};
Console.WriteLine(CountPairs(arr2));
}
}
|
Javascript
function countPairs(arr) {
let count = 0;
for (let i = 0; i < arr.length; i++) {
for (let j = i + 1; j < arr.length; j++) {
if (arr[j] % arr[i] === 0 && (arr[j] & (arr[j] - 1)) === 0) {
count++;
}
}
}
return count;
}
const arr1 = [4, 16, 8, 64];
console.log(countPairs(arr1));
const arr2 = [2, 4, 8, 16];
console.log(countPairs(arr2));
|
Time Complexity: O(N2)
Auxiliary Space: O(1)
Efficient Approach: To optimize the above approach, the idea is based on the observation that any power of 2 has only one set bit in its binary representation. For any such element arr[j], all the powers of 2 which have their set bits at a position less than or equal to the position of a set bit of arr[j], will satisfy the given condition. Follow the steps below to solve the problem:
- Initialize an auxiliary array setBits of size equal to 31, and initialize count as 0 to store the number of required pairs.
- Traverse the array arr[] using the variable i and perform the following operations:
- After completing the above steps, print the value of count as the result.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void numberOfPairs( int arr[], int N)
{
int set_bits[31] = { 0 };
int count = 0;
for ( int i = 0; i < N; i++) {
int x = arr[i];
int bitpos = -1;
while (x > 0) {
bitpos++;
x /= 2;
}
for ( int j = 0;
j <= bitpos; j++) {
count += set_bits[j];
}
set_bits[bitpos]++;
}
cout << count;
}
int main()
{
int arr[] = { 4, 16, 8, 64 };
int N = sizeof (arr) / sizeof (arr[0]);
numberOfPairs(arr, N);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static void numberOfPairs( int arr[], int N)
{
int []set_bits = new int [ 31 ];
Arrays.fill(set_bits, 0 );
int count = 0 ;
for ( int i = 0 ; i < N; i++)
{
int x = arr[i];
int bitpos = - 1 ;
while (x > 0 )
{
bitpos++;
x /= 2 ;
}
for ( int j = 0 ;
j <= bitpos; j++)
{
count += set_bits[j];
}
set_bits[bitpos]++;
}
System.out.println(count);
}
public static void main(String args[])
{
int arr[] = { 4 , 16 , 8 , 64 };
int N = arr.length;
numberOfPairs(arr, N);
}
}
|
Python3
def numberOfPairs(arr, N):
set_bits = [ 0 ] * 31
count = 0
for i in range (N):
x = arr[i]
bitpos = - 1
while (x > 0 ):
bitpos + = 1
x / / = 2
for j in range (bitpos + 1 ):
count + = set_bits[j]
set_bits[bitpos] + = 1
print (count)
if __name__ = = '__main__' :
arr = [ 4 , 16 , 8 , 64 ]
N = len (arr)
numberOfPairs(arr, N)
|
C#
using System;
class GFG
{
static void numberOfPairs( int [] arr, int N)
{
int []set_bits = new int [31];
for ( int i = 0; i < N; i++)
{
set_bits[i] = 0;
}
int count = 0;
for ( int i = 0; i < N; i++)
{
int x = arr[i];
int bitpos = -1;
while (x > 0)
{
bitpos++;
x /= 2;
}
for ( int j = 0;
j <= bitpos; j++)
{
count += set_bits[j];
}
set_bits[bitpos]++;
}
Console.Write(count);
}
static public void Main()
{
int [] arr = { 4, 16, 8, 64 };
int N = arr.Length;
numberOfPairs(arr, N);
}
}
|
Javascript
<script>
function numberOfPairs(arr, N)
{
let set_bits = [];
for (let i = 0; i < 31; i++)
{
set_bits[i] = 0;
}
let count = 0;
for (let i = 0; i < N; i++)
{
let x = arr[i];
let bitpos = -1;
while (x > 0)
{
bitpos++;
x = Math.floor( x / 2 );
}
for (let j = 0;
j <= bitpos; j++)
{
count += set_bits[j];
}
set_bits[bitpos]++;
}
document.write(count);
}
let arr = [ 4, 16, 8, 64 ];
let N = arr.length;
numberOfPairs(arr, N);
</script>
|
Time Complexity: O(N*log M), where M is the largest element in the array.
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...