Open In App

Find final value of A after alternate AND-OR operations on A and B

Last Updated : 11 Jul, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Given 3 integers A, B, and N the task is to perform alternate AND-OR operations on A and B, and then assign the result of each operation to A. Find the final value of A

Examples:

Input: A = 4, B = 5, N = 1
Output: 4
Explanation: Perform 1 operation i.e A = A & B, therefore A = 4 & 5 = 4
Input: A = 4, B = 5, N = 1000
Output: 5

 

Naive Approach : The task can be solved using observations. It can be observed that the answer will be A AND B If the Number N is Odd. The answer will be A OR B if the Number N is Even. It is because the AND-OR operation is running alternatively.

Below is the implementation of the above approach:

C++




/*package whatever //do not write package name here */
#include <bits/stdc++.h>
using namespace std;
 
int main()
{
    int A = 4;
    int B = 5;
    int n = 1000;
    int N = 1;
    for (N = 1; N <= n; N++) {
        if ((N % 2) != 0) {
            A = A & B;
        }
        else {
            A = A | B;
        }
    }
 
    cout << "Output is:" << A;
    return 0;
}
 
// This code is contributed by rakeshsahni


Java




/*package whatever //do not write package name here */
 
import java.io.*;
 
class GFG {
    public static void main (String[] args) {
        int A=4;
                int B=5;
                int n =1000;
                int N = 1;
                for(N=1;N<=n;N++)
        {
            if ((N % 2) != 0) {
                A = A & B;
            } else {
                A = A | B;
            }}
 
            System.out.println("Output is:" + A);
    }
}


Python3




# Python program for above approach
A = 4
B = 5
n = 1000
N = 1
 
for N in range(1, n+1):
    if ((N % 2) != 0):
        A = A & B
 
    else:
        A = A | B
 
print("Output is:", end="")
print(A)
 
# This code is contributed by ninja_hattori.


C#




// C# program to implement the approach
using System;
class GFG {
 
  public static void Main()
  {
        int A=4;
                int B=5;
                int n =1000;
                int N = 1;
                for(N=1;N<=n;N++)
        {
            if ((N % 2) != 0) {
                A = A & B;
            } else {
                A = A | B;
            }}
 
        Console.Write("Output is:" + A);
  }
}
 
// This code is contributed by sanjoy_62.


Javascript




<script>
     // JavaScript program for above approach
        var A = 4;
        var B = 5;
        var n = 1000;
        var N = 1;
        for (N = 1; N <= n; N++) {
            if ((N % 2) != 0) {
                A = A & B;
            } else {
                A = A | B;
            }
        }
 
        document.write("Output is:" + A);
 
// This code is contributed by Rajput-Ji
</script>


Output

Output is:5

Time Complexity: O(n)
Auxiliary Space: O(1)

Approach: The task can be solved using observations. It can be observed that the answer will be A, only when N is 1, for the rest of the N values, the answer will be B. It is because, after the first operation, both numbers will become equal to B
Below is the implementation of the above approach:

C++




// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to find the resultant value of A
int find(int A, int B, int N)
{
    if (N == 1)
        return A & B;
    else
        return B;
}
 
// Driver Code
int main()
{
    cout << find(4, 5, 1000);
    return 0;
}


Java




// JAVA program for the above approach
import java.util.*;
class GFG
{
   
  // Function to find the resultant value of A
  public static int find(int A, int B, int N)
  {
    if (N == 1)
      return A & B;
    else
      return B;
  }
 
  // Driver Code
  public static void main(String[] args)
  {
    System.out.print(find(4, 5, 1000));
  }
}
 
// This code is contributed by Taranpreet


Python3




# Python code for the above approach
 
# Function to find the resultant value of A
def find(A, B, N):
    if (N == 1):
        return A & B;
    else:
        return B;
 
# Driver Code
print(find(4, 5, 1000));
 
# This code is contributed by Saurabh Jaiswal


C#




// C# program for the above approach
using System;
class GFG
{
   
  // Function to find the resultant value of A
  static int find(int A, int B, int N)
  {
    if (N == 1)
      return A & B;
    else
      return B;
  }
 
  // Driver Code
  public static void Main()
  {
    Console.Write(find(4, 5, 1000));
  }
}
 
// This code is contributed by Samim Hossain Mondal.


Javascript




<script>
        // JavaScript code for the above approach
 
        // Function to find the resultant value of A
        function find(A, B, N) {
            if (N == 1)
                return A & B;
            else
                return B;
        }
 
        // Driver Code
        document.write(find(4, 5, 1000));
 
       // This code is contributed by Potta Lokesh
    </script>


Output

5

Time Complexity: O(1)
Auxiliary Space: O(1)



Similar Reads

Minimize operations to convert each node of N-ary Tree from initial[i] to final[i] by flipping current node subtree in alternate fashion
Given an N-ary Tree consisting of N nodes valued from [0, N - 1] and two binary arrays initial[] and final[] of size N such that initial[i] represents the value assigned to the node i, the task is to find the minimum number of operations required to convert each value of nodes initial[i] to final[i] by flipping the value of the node, its grandchild
11 min read
Find the final sequence of the array after performing given operations
Given an array arr[] of size N, the task is to perform the following operation exactly N times, Create an empty list of integers b[] and in the ith operation, Append arr[i] to the end of b[].Reverse the elements in b[]. Finally, print the contents of the list b[] after the end of all operations.Examples: Input: arr[] = {1, 2, 3, 4} Output: 4 2 1 3
12 min read
Find final value if we double after every successful search in array
Given an array and an integer k, traverse the array and if the element in array is k, double the value of k and continue traversal. In the end return value of k. Examples: Input : arr[] = { 2, 3, 4, 10, 8, 1 }, k = 2 Output: 16 Explanation: First k = 2 is found, then we search for 4 which is also found, then we search for 8 which is also found, the
11 min read
Final string after performing given operations
Given a string str containing only characters x and y, the task is to perform the following operations while possible: Find an index such that s[i] = 'x' and s[i+1] = 'y' and delete both the characters s[i] and s[i+1], if no such index is found then find an index such that s[i] = 'y' and s[i+1] = 'x' and swap(s[i], s[i+1]). Print the final string a
6 min read
Leftover element after performing alternate Bitwise OR and Bitwise XOR operations on adjacent pairs
Given an array of N(always a power of 2) elements and Q queries.Every query consists of two elements, an index, and a value... We need to write a program that assigns value to Aindex and prints the single element which is left after performing the below operations for each query: At alternate steps, perform bitwise OR and bitwise XOR operations on
21 min read
Parity of final value after repeated circular removal of Kth Integer
Given an integer N, denoting the size of a circle where first N integers are placed in clockwise order such that j and (j+1) are adjacent, and 1 and N are also adjacent. Given an integer K (K &lt; N), the task is to find if the last remaining value is odd or even when in each turn the Kth element from the start (i.e., 1) is removed and it is perfor
4 min read
Find the final number obtained after performing the given operation
Given an array of positive distinct integers arr[], the task is to find the final number obtained by performing the following operation on the elements of the array: Operation: Take two unequal numbers and replace the larger number with their difference until all numbers become equal.Examples: Input: arr[] = {5, 2, 3} Output: 1 5 - 3 = 2, arr[] = {
7 min read
Find final Array after subtracting maximum from all elements K times
Given an array arr[] of N integers and an integer K, the task is to do the below operations with this array K times. Operations are as follows: Find the maximum element (say M) from the array.Now replace every element with M-arri. where 1 ≤ i ≤ N. Examples: Input: N = 6, K = 3, arr[] = {5, 38, 4, 96, 103, 41}Output: 98 65 99 7 0 62Explanation: 1st
9 min read
Find the final string after flipping bits at the indices that are multiple of prime factors of array elements
Given a binary string S of size N and an array arr[] of M integers, the task is to find the final string after flipping the characters at indices which are multiple of the prime factors of all the array elements. Note that this problem uses 1-based indexing. Examples: Input: S = "000000", arr[] = {2, 4, 6}Output: 011100Explanation: The prime factor
13 min read
Find the size of the final imaginary Array after removing the balls
Given 2 arrays color[]and radius[] of length N each representing N balls, where color[i] represents the color of the ith ball while radius[i] represents the radius of the ith ball. If two consecutive balls have the same color and size, both are removed from the array, the task is to find the length of the final imaginary array. Examples: Input: N =
12 min read
Practice Tags :