VOOZH about

URL: https://www.geeksforgeeks.org/dsa/bitwise-or-of-a-binary-array/

⇱ Bitwise OR of a Binary Array - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Bitwise OR of a Binary Array

Last Updated : 4 Jun, 2026

Given a binary array, find Bitwise OR of the given array.

Examples :

Input: arr[] = [1 1 1 0]
Output:1
Explanation:
1 | 1 = 1
1 | 1 = 1
1 | 0 = 1
hence output is 1

Input: arr[] = [0 0 1 0]
Output: 1
Explanation:
0 | 0 = 0
0 | 1 = 1
1 | 0 = 1
hence output is 1

Bitwise OR Accumulation - Theta(n) Time and O(1) Space

The OR gate outputs 1 if at least one input is 1. Simply iterate through all bits and compute cumulative OR using bitwise OR operator.

  • Initialize result = 0.
  • Iterate through each element in array.
  • Update result = result | element.
  • Return final result.

Output
1

Linear Scan with Early Exit - O(n) Time and O(1) Space

OR gate outputs 1 if at least one input is 1. Scan the array and return 1 immediately when a 1 is found. If no 1 is found after scanning all elements, return 0.

  • Iterate through each bit in array.
  • If current bit equals 1, return 1 immediately.
  • After loop ends, return 0.

Output
1


Comment
Article Tags:
Article Tags: