Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • DSA
  • Practice Bitwise Algorithms
  • MCQs on Bitwise Algorithms
  • Tutorial on Biwise Algorithms
  • Binary Representation
  • Bitwise Operators
  • Bit Swapping
  • Bit Manipulation
  • Count Set bits
  • Setting a Bit
  • Clear a Bit
  • Toggling a Bit
  • Left & Right Shift
  • Gray Code
  • Checking Power of 2
  • Important Tactics
  • Bit Manipulation for CP
  • Fast Exponentiation
Open In App
Next Article:
Modify array by replacing every array element with minimum possible value of arr[j] + |j - i|
Next article icon

Minimum value among AND of elements of every subset of an array

Last Updated : 16 Nov, 2022
Comments
Improve
Suggest changes
Like Article
Like
Report

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.
Examples: 
 

Input: arr[] = {1, 2, 3} Output: 0 AND of all possible subsets  (1 & 2) = 0, (1 & 3) = 1, (2 & 3) = 2 and  (1 & 2 & 3) = 0.  Minimum among these is 0.   Input: arr[] = {7, 2} Output: 2    

 

Approach: 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 sub-array.
Below is the implementation of the above approach: 
Implementation: 
 

C++




// C++ program for the above approach
#include<bits/stdc++.h>
using namespace std;
 
void minAND(int arr[], int n)
{
 
    int s = arr[0];
 
    // Find AND of whole array
    for (int i = 1; i < n; i++)
    {
        s = s & arr[i];
    }
 
    // Print the answer
    cout << (s) << endl;
}
     
// Driver code
int main()
{
    int arr[] = {1, 2, 3};
    int n = sizeof(arr)/sizeof(int);
    minAND(arr, n);
}
 
// This code has been contributed by Arnab Kundu
 
 

Java




// Java program for the above approach
class GFG
{
 
    static void minAND(int[] arr, int n)
    {
 
        int s = arr[0];
 
        // Find AND of whole array
        for (int i = 1; i < n; i++)
        {
            s = s & arr[i];
        }
 
        // Print the answer
        System.out.println(s);
    }
     
    // Driver code
    public static void main(String[] args)
    {
        int[] arr = {1, 2, 3};
        int n = arr.length;
        minAND(arr, n);
    }
}
 
// This code has been contributed by 29AjayKumar
 
 

Python




# Python program for the above approach
def minAND(arr, n):
     
    s = arr[0]
     
    # Find AND of whole array
    for i in range(1, n):
        s = s & arr[i]
     
    # Print the answer
    print(s)
 
# Driver code
arr = [1, 2, 3]
n = len(arr)
minAND(arr, n)
 
 

C#




// C# program for the above approach
class GFG
{
 
    static void minAND(int[] arr, int n)
    {
 
        int s = arr[0];
 
        // Find AND of whole array
        for (int i = 1; i < n; i++)
        {
            s = s & arr[i];
        }
 
        // Print the answer
        System.Console.WriteLine(s);
    }
     
    // Driver code
    static void Main()
    {
        int[] arr = {1, 2, 3};
        int n = arr.Length;
        minAND(arr, n);
    }
}
 
// This code has been contributed by chandan_jnu
 
 

PHP




<?php
// PHP program for the above approach
 
function minAND($arr, $n)
{
 
    $s = $arr[0];
 
    // Find AND of whole array
    for ($i = 1; $i < $n; $i++)
    {
        $s = $s & $arr[$i];
    }
 
    // Print the answer
    print($s . "\n");
}
     
// Driver code
$arr = array(1, 2, 3);
$n = count($arr);
minAND($arr, $n);
 
// This code is contributed
// by chandan_jnu
?>
 
 

Javascript




<script>
// Javascript implementation of the approach
function minAND(arr, n) {
 
    let s = arr[0];
 
    // Find AND of whole array
    for (let i = 1; i < n; i++) {
        s = s & arr[i];
    }
 
    // Print the answer
    document.write((s) + "<br>");
}
 
// Driver code
 
let arr = [1, 2, 3];
let n = arr.length;
minAND(arr, n);
 
 
// This code is contributed by _saurabh_jaiswal
</script>
 
 
Output: 
0

 

Time Complexity: O(n), where n is the size of the given array.
Auxiliary Space: O(1), no extra space is required, so it is a constant.



Next Article
Modify array by replacing every array element with minimum possible value of arr[j] + |j - i|

P

Pravash Jha
Improve
Article Tags :
  • Bit Magic
  • DSA
Practice Tags :
  • Bit Magic

Similar Reads

  • Sum of (maximum element - minimum element) for all the subsets of an array.
    Given an array arr[], the task is to compute the sum of (max{A} - min{A}) for every non-empty subset A of the array arr[].Examples: Input: arr[] = { 4, 7 } Output: 3There are three non-empty subsets: { 4 }, { 7 } and { 4, 7 }. max({4}) - min({4}) = 0 max({7}) - min({7}) = 0 max({4, 7}) - min({4, 7})
    10 min read
  • Sum of minimum element of all subarrays of a sorted array
    Given a sorted array A of n integers. The task is to find the sum of the minimum of all possible subarrays of A. Examples: Input: A = [ 1, 2, 4, 5] Output: 23 Subsequences are [1], [2], [4], [5], [1, 2], [2, 4], [4, 5] [1, 2, 4], [2, 4, 5], [1, 2, 4, 5] Minimums are 1, 2, 4, 5, 1, 2, 4, 1, 2, 1. Sum
    4 min read
  • Minimum Possible value of |ai + aj - k| for given array and k.
    You are given an array of n integer and an integer K. Find the number of total unordered pairs {i, j} such that absolute value of (ai + aj - K), i.e., |ai + aj - k| is minimal possible, where i != j.Examples: Input: arr[] = {0, 4, 6, 2, 4}, K = 7Output: Minimal Value = 1, Total Pairs = 5 Explanation
    13 min read
  • Sum of minimum element of all sub-sequences of a sorted array
    Given a sorted array A of n integers. The task is to find the sum of the minimum of all possible subsequences of A.Note: Considering there will be no overflow of numbers. Examples: Input: A = [1, 2, 4, 5] Output: 29 Subsequences are [1], [2], [4], [5], [1, 2], [1, 4], [1, 5], [2, 4], [2, 5], [4, 5]
    4 min read
  • Modify array by replacing every array element with minimum possible value of arr[j] + |j - i|
    Given an array arr[] of size N, the task is to find a value for each index such that the value at index i is arr[j] + |j - i| where 1 ? j ? N, the task is to find the minimum value for each index from 1 to N. Example: Input: N = 5, arr[] = {1, 4, 2, 5, 3}Output: {1, 2, 2, 3, 3}Explanation: arr[0] =
    11 min read
  • Sum of all minimum occurring elements in an Array
    Given an array of integers containing duplicate elements. The task is to find the sum of all least occurring elements in the given array. That is the sum of all such elements whose frequency is minimum in the array.Examples: Input : arr[] = {1, 1, 2, 2, 3, 3, 3, 3} Output : 2 The least occurring ele
    6 min read
  • Minimum value that can't be formed using OR of elements of the Array
    Given an array of positive integers arr[]. The task is to Find the minimum positive value which cannot be formed using OR operator on any subset of the array. Examples: Input: arr[] = {2, 3}Output: 1Explanation: Since 1 is missing from array and we cannot make it using OR for any other subset of thi
    6 min read
  • Minimize the sum of MEX by removing all elements of array
    Given an array of integers arr[] of size N. You can perform the following operation N times: Pick any index i, and remove arr[i] from the array and add MEX(arr[]) i.e., Minimum Excluded of the array arr[] to your total score. Your task is to minimize the total score. Examples: Input: N = 8, arr[] =
    7 min read
  • Minimum XOR of OR and AND of any pair in the Array
    Given an array arr[] of N positive integers the task is to find the minimum value of Bitwise XOR of Bitwise OR and AND of any pair in the given array. Examples: Input: arr[] = {1, 2, 3, 4, 5} Output: 1 Explanation: For element 2 & 3: The value of the expression (2&3) xor (2|3) is 1, which is
    5 min read
  • Minimum sum of values subtracted from array elements to make all array elements equal
    Given an array arr[] consisting of N positive integers, the task is to find the sum of all the array elements required to be subtracted from each array element such that remaining array elements are all equal. Examples: Input: arr[] = {1, 2}Output: 1Explanation: Subtracting 1 from arr[1] modifies ar
    4 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences