Reduce Binary Array by replacing both 0s or both 1s pair with 0 and 10 or 01 pair with 1
Given a binary array arr[] of size N, the task is to find the last number remaining in the array after performing a set of operations. In each operation, select any two numbers and perform the following:
- If both numbers are the same, remove them from the array and insert a 0.
- If both numbers are different, remove both of them and insert a 1.
Example:
Input: arr[]={0, 0, 1}
Output: 1
Explanation: There are two possible sequence of operations as follows:
- arr[] = {0, 0, 1}, delete (0, 1) and insert 0 => arr[] = {0, 0}, delete (0, 0) and insert 1=> arr[] = {1}.
- arr[] = {0, 0, 1}, delete (0, 0) and insert 0 => arr[] = {0, 1}, delete (0, 1) and insert 1=> arr[] = {1}.
Hence the remaining element is 1.
Input: arr[]={1, 0, 0, 0, 1}
Output: 0
Approach: The given problem can be solved based on the following observations:
- 2 same numbers are getting replaced by a 0.
- 2 different numbers are getting replaced by a 1.
Now, the creating a table for each outcome:
Upon careful observation of the above table, it can be noticed that the table represents the bitwise XOR operation. Hence, the remaining integer will be equal to the bitwise XOR of the given array elements which can be further simplified as if the frequency of 1 is even, the result is 0, otherwise, it’s 1.
Below is the implementation of the above approach.
C++
// C++ program of the above approach #include <bits/stdc++.h> using namespace std; // Function to find last remaining // integer in the given array int lastNumber(vector< int >& arr) { // Variable to store the // frequency of 1 int one = 0; // Loop to iterate the // given array for ( int x : arr) { if (x == 1) { one += 1; } } // If frequency of 1 is even if (one % 2 == 0) return 0; // If frequency of 1 is odd return 1; } // Driver Code int main() { vector< int > arr = { 1, 0, 0, 0, 1 }; cout << lastNumber(arr); } |
Java
// Java program of the above approach import java.util.ArrayList; class GFG { // Function to find last remaining // integer in the given array static Integer lastNumber(ArrayList<Integer> arr) { // Variable to store the // frequency of 1 int one = 0 ; // Loop to iterate the // given array for ( int x : arr) { if (x == 1 ) { one += 1 ; } } // If frequency of 1 is even if (one % 2 == 0 ) return 0 ; // If frequency of 1 is odd return 1 ; } // Driver Code public static void main(String args[]) { ArrayList<Integer> arr = new ArrayList<Integer>(); arr.add( 1 ); arr.add( 0 ); arr.add( 0 ); arr.add( 0 ); arr.add( 1 ); System.out.println(lastNumber(arr)); } } // This code is contributed by gfgking |
Python3
# python program of the above approach # Function to find last remaining # integer in the given array def lastNumber(arr): # Variable to store the # frequency of 1 one = 0 # Loop to iterate the # given array for x in arr: if (x = = 1 ): one + = 1 # If frequency of 1 is even if (one % 2 = = 0 ): return 0 # If frequency of 1 is odd return 1 # Driver Code if __name__ = = "__main__" : arr = [ 1 , 0 , 0 , 0 , 1 ] print (lastNumber(arr)) # This code is contributed by rakeshsahni |
C#
// C# program of the above approach using System; using System.Collections.Generic; class GFG{ // Function to find last remaining // integer in the given array static int lastNumber(List< int > arr) { // Variable to store the // frequency of 1 int one = 0; // Loop to iterate the // given array foreach ( int x in arr) { if (x == 1) { one += 1; } } // If frequency of 1 is even if (one % 2 == 0) return 0; // If frequency of 1 is odd return 1; } // Driver Code public static void Main() { List< int > arr = new List< int >(){ 1, 0, 0, 0, 1 }; Console.WriteLine(lastNumber(arr)); } } // This code is contributed by ukasp |
Javascript
<script> // JavaScript code for the above approach // Function to find last remaining // integer in the given array function lastNumber(arr) { // Variable to store the // frequency of 1 let one = 0; // Loop to iterate the // given array for (let x of arr) { if (x == 1) { one += 1; } } // If frequency of 1 is even if (one % 2 == 0) return 0; // If frequency of 1 is odd return 1; } // Driver Code let arr = [1, 0, 0, 0, 1]; document.write(lastNumber(arr)); // This code is contributed by Potta Lokesh </script> |
0
Time Complexity: O(N)
Auxiliary Space: O(1)