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

Last Updated : 11 Jul, 2022

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 ` `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

 ``

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 ` `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

 ``

Output

`5`

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

Article Tags :
Practice Tags :