

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- 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 Questions & Answers
- Maximum value of XOR among all triplets of an array in C++
- Sort subset of array elements in Java
- Get average of every group of n elements in an array JavaScript
- Count of elements of an array present in every row of NxM matrix in C++
- How to add together a subset of elements of an array in MongoDB aggregation?
- Maximum and minimum of an array using minimum number of comparisons in C
- Return an array of all the indices of minimum elements in the array in JavaScript
- Maximum product subset of an array in C++
- Function that returns the minimum and maximum value of an array in JavaScript
- Find indexes of multiple minimum value in an array in JavaScript
- Java Program to sort a subset of array elements
- How to find the minimum value of an array in 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++
- Insert value in the middle of every value inside array JavaScript
- XOR of Sum of every possible pair of an array in C++
Advertisements