Perform Bitwise AND(&) operation on array....

Hello friends, Today I have brought a special programming challenge for you.If you are a beginner in programming then don't worried about that. This challenge is also for beginners if you have a knowledge of operators and array in programming language.This challenge will also help to enhance your thinking level and develop your logic as well.

The challenge is that suppose you have an array of N integer. and I want to give you two queries which contains three values which i explained below. then you have to perform Bitwise AND (&) operation on that.

Input Format:-

First line contains N integer( Size of an array) and Q integer(Number of Queries) each separated by space.

Second line contains value of array(arr[ ]) with space separated each.

Third line contains first query which contains three value each separated by space and first value(X) is initial value of an array and second value(Y) is where to stop an array and third value(Z) is very existing which perform bitwise AND(&) operation with each element of an array starting from arr[X] to arr[Y]. Also update a array with new value which got after perform a bitwise and(&) operation on that.

Fourth line contains second query which also have three value and perform same operation as previous and update an array.

Similarly after all you will get a final updated array and you have to display on screen.

Output Format :-

Print the final updated array on a single line each separated by space.

Input:-

5 2
7 7 7 7 7
1 3 4
1 5 6

Output:-

4 4 4 6 6

Note:- You can make in any language but I will recommended you to make it on Java language.

If you have an problem to understand it or make a logic on that then you can comment below i will definitely help you if i will get a lots of comment then I will upload a solution of this particular problem.  Thank you!!

Comments

  1. /*
    * To change this license header, choose License Headers in Project Properties.
    * To change this template file, choose Tools | Templates
    * and open the template in the editor.
    */
    package dbconnection.dbutil;

    import java.util.Scanner;

    /**
    *
    * @author hp
    */
    public class Ad
    {
    public static void main(String[] args) {
    System.out.println("Enter first line:");
    int a,b;
    Scanner kb=new Scanner (System.in);
    a=kb.nextInt();
    b=kb.nextInt();
    int[]arr=new int[a];
    System.out.println("Enter the array value:");
    for(int i=0;i<arr.length;i++)
    {
    arr[i]=kb.nextInt();
    }
    for(int i=1;i<=b;i++)
    {
    System.out.println("Enter the third line: ");
    int s=kb.nextInt();
    s=s-1;
    int e=kb.nextInt();
    e=e-1;
    int op=kb.nextInt();
    for(;s<=e;s++)
    {
    arr[s]=arr[s] & op;
    }
    }
    System.out.println("-----------------Output------------");
    for(int j=0;j<arr.length;j++)
    {
    System.out.println(arr[j]);
    }

    }
    }

    ReplyDelete

Post a Comment