- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Minimum value among AND of elements of every subset of an array in C++
Problem statement
Given an array of integers, the task is to find the AND of all elements of each subset of the array and print the minimum AND value among all those.
Example
If arr[] = {1, 2, 3, 4, 5} then (1 & 2) = 0 (1 & 3) = 1 (1 & 4) = 0 (1 & 5) = 1 (2 & 3) = 2 (2 & 4) = 0 (2 & 5) = 0 (3 & 4) = 0 (3 & 5) = 1 (4 & 5) = 4
Algorithm
- The minimum AND value of any subset of the array will be the AND of all the elements of the array.
- So, the simplest way is to find AND of all elements of subarray.
Example
#include <bits/stdc++.h> using namespace std; int getMinAndValue(int *arr, int n) { int result = arr[0]; for (int i = 1; i < n; ++i) { result = result & arr[i]; } return result; } int main() { int arr[] = {1, 2, 3, 4, 5}; int n = sizeof(arr) / sizeof(arr[0]); cout << "Minimum value = " << getMinAndValue(arr, n) << endl; return 0; }
When you compile and execute above program. It generates following output −
Output
Minimum value = 0
- Related Articles
- Maximum value of XOR among all triplets of an array in C++
- Count of elements of an array present in every row of NxM matrix in C++
- Sort subset of array elements in Java
- Maximum product subset of an array in C++
- Maximum and minimum of an array using minimum number of comparisons in C
- Get average of every group of n elements in an array JavaScript
- Find a non empty subset in an array of N integers such that sum of elements of subset is divisible by N in C++
- Maximum product subset of an array in C++ program
- How to add together a subset of elements of an array in MongoDB aggregation?
- Return an array of all the indices of minimum elements in the array in JavaScript
- Recursive Programs to find Minimum and Maximum elements of array in C++
- Divide every element of one array by other array elements in C++ Program
- Function that returns the minimum and maximum value of an array in JavaScript
- XOR of Sum of every possible pair of an array in C++
- Find indexes of multiple minimum value in an array in JavaScript

Advertisements