Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> C++

Count pairs with bitwise OR less than Max in C++

กำหนด array หาจำนวนคู่ (i,j) ที่ i < j และ (arr[i] | arr[j]) ≤ max(arr[i], arr[j])

ข้อสังเกต: a | b ≤ max(a,b) ⟺ a | b = max(a,b) ⟺ bit ของตัวเล็กเป็น subset ของ bit ของตัวใหญ่

ตัวอย่าง: {2,5,1,8,9} → คู่ที่ตรง: (5,1)→5, (8,9)→9, (2,?) ไม่มี → Output: 3

ตัวอย่างโค้ด C++

#include <bits/stdc++.h>
using namespace std;

int Pair_OR(int arr[], int n) {
    int count = 0;
    for(int i=0;i

Input

{4,8,9,10,23}

Output

3