Super Niven Numbers | C ++ implementation to check if a number is Super Niven Number or not . ; Checks if sums of all subsets of digits array divides the number N ; to calculate length of array arr ; There are totoal 2 ^ n subsets ; Consider all numbers from 0 to 2 ^ n - 1 ; Consider binary representation of current i to decide which elements to pick . ; check sum of picked elements . ; Function to check if a number is a super - niven number ; to stor digits of N ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isDivBySubsetSums ( vector < int > arr , int num ) { int n = arr . size ( ) ; long long total = 1 << n ; for ( long long i = 0 ; i < total ; i ++ ) { long long sum = 0 ; for ( int j = 0 ; j < n ; j ++ ) if ( i & ( 1 << j ) ) sum += arr [ j ] ; if ( sum != 0 && num % sum != 0 ) return false ; } return true ; } bool isSuperNivenNum ( int n ) { int temp = n ; vector < int > digits ; while ( n != 0 ) { int digit = n % 10 ; digits . push_back ( digit ) ; n = n / 10 ; } return isDivBySubsetSums ( digits , temp ) ; } int main ( ) { int n = 500 ; if ( isSuperNivenNum ( n ) ) cout << " yes " ; else cout << " No " ; return 0 ; }