Count distinct Bitwise OR of all Subarrays | Java implementation of the above approach ; function to calculate count of distinct bitwise OR of all subarrays . ; Driver code
import java . io . * ; import java . util . * ; class GFG { static int distintBitwiseOR ( int arr [ ] , int n ) { HashSet < Integer > ans = new HashSet < > ( ) ; HashSet < Integer > prev = new HashSet < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { HashSet < Integer > ne = new HashSet < > ( ) ; ne . add ( arr [ i ] ) ; for ( int x : prev ) { ne . add ( arr [ i ] x ) ; } for ( int x : ne ) { ans . add ( x ) ; } prev = ne ; } return ans . size ( ) ; } public static void main ( String [ ] args ) { int n = 3 ; int arr [ ] = { 1 , 2 , 4 } ; System . out . println ( distintBitwiseOR ( arr , n ) ) ; } }