Input: A array arr[] of two elements having value 0 and 1
Output: Make both elements 0.
Specifications: Following are the specifications to follow.
1) It is guaranteed that one element is 0 but we do not know its position.
2) We can't say about another element it can be 0 or 1.
3) We can only complement array elements, no other operation like and, or, multi, division, .... etc.
4) We can't use if, else and loop constructs.
5) Obviously, we can't directly assign 0 to array elements.
There are several ways we can do it as we are sure that always one Zero is there. Thanks to devendraiiit for suggesting following 3 methods.
Method 1
C++
#include <bits/stdc++.h>
using namespace std;
void changeToZero(int a[2])
{
a[ a[1] ] = a[ !a[1] ];
}
// Driver code
int main()
{
int a[] = {1, 0};
changeToZero(a);
cout<<"arr[0] = "<<a[0]<<endl;
cout<<" arr[1] = "<<a[1];
return 0;
}
// This code is contributed by rathbhupendra
C
void changeToZero(int a[2])
{
a[ a[1] ] = a[ !a[1] ];
}
int main()
{
int a[] = {1, 0};
changeToZero(a);
printf(" arr[0] = %d \n", a[0]);
printf(" arr[1] = %d ", a[1]);
getchar();
return 0;
}
Java
import java.io.*;
class GFG{
public static void changeToZero(int a[])
{
a[a[1]] = a[1 - a[1]];
}
// Driver code
public static void main(String args[])
{
int[] arr;
arr = new int[2];
arr[0] = 1;
arr[1] = 0;
changeToZero(arr);
System.out.println("arr[0]= " + arr[0]);
System.out.println("arr[1]= " + arr[1]);
}
}
// This code is contributed by rohitsingh07052
Python3
def changeToZero(a):
a[ a[1] ] = a[ not a[1] ]
return a
# Driver code
if __name__=='__main__':
a = [1, 0]
a = changeToZero(a);
print(" arr[0] = " + str(a[0]))
print(" arr[1] = " + str(a[1]))
# This code is contributed by Yash_R
C#
using System;
class GFG {
public static void changeToZero(int[] a)
{
a[a[1]] = a[1 - a[1]];
}
// Driver code
public static void Main()
{
int[] arr;
arr = new int[2];
arr[0] = 1;
arr[1] = 0;
changeToZero(arr);
Console.WriteLine("arr[0]= " + arr[0]);
Console.WriteLine("arr[1]= " + arr[1]);
}
}
// This code is contributed by souravmahato348.
JavaScript
<script>
function changeToZero(a)
{
a[a[1]] = a[1 - a[1]];
}
// Driver code
let arr;
arr = [];
arr[0] = 1;
arr[1] = 0;
changeToZero(arr);
document.write("arr[0] = " + arr[0] + "<br/>");
document.write("arr[1] = " + arr[1]);
// This code is contributed by avijitmondal1998
</script>
Time Complexity: O(1)
Auxiliary Space: O(1)
Method 2
C++
void changeToZero(int a[2])
{
a[ !a[0] ] = a[ !a[1] ];
}
C
void changeToZero(int a[2])
{
a[ !a[0] ] = a[ !a[1] ]
}
Java
void changeToZero(int [2]a) {
a[!a[0]] = a[!a[1]];
}
// This code is contributed by souravmahato348.
Python3
def changeToZero(a):
a[ !a[0] ] = a[ !a[1] ]
# This code is contributed by sanjoy_62.
C#
static void changeToZero(int [2]a) {
a[!a[0]] = a[!a[1]];
}
// This code is contributed by souravmahato348.
JavaScript
<script>
function changeToZero(a)
{
a[ !a[0] ] = a[ !a[1] ];
}
// This code is contributed by souravmahato348.
</script>
Time Complexity: O(1)
Auxiliary Space: O(1)
Method 3
This method doesn't even need complement.
C++
#include <iostream>
using namespace std;
void changeToZero(int a[2])
{
a[ a[1] ] = a[ a[0] ]
}
int main() {
cout << "GFG!";
return 0;
}
C
void changeToZero(int a[2])
{
a[ a[1] ] = a[ a[0] ]
}
Java
static void changeToZero(int a[2])
{
a[ a[1] ] = a[ a[0] ]
}
// this code is contributed by shivanisinghss2110
Python3
def changeToZero(a) :
a[ a[1] ] = a[ a[0] ]
C#
static void changeToZero(int[] a)
{
a[ a[1] ] = a[ a[0] ];
}
//this code is contributed by phasing17
JavaScript
function changeToZero(a)
{
a[ a[1] ] = a[ a[0] ];
}
//this code is contributed by phasing17
Time Complexity: O(1)
Auxiliary Space: O(1)
Method 4
Thanks to purvi for suggesting this method.
C++
#include <iostream>
using namespace std;
void changeToZero(int a[2])
{
a[0] = a[a[0]];
a[1] = a[0];
}
int main() {
cout << "GFG!";
return 0;
}
C
void changeToZero(int a[2])
{
a[0] = a[a[0]];
a[1] = a[0];
}
Java
static void changeToZero(int a[])
{
a[0] = a[a[0]];
a[1] = a[0];
}
//This code is contributed by shruti456rawal
Python3
# Python code for the above approach
def changeToZero(a) :
a[0] = a[a[0]]
a[1] = a[0]
# This code is contributed by splevel62.
C#
static void changeToZero(int[] a)
{
a[0] = a[a[0]];
a[1] = a[0];
}
//This code is contributed by shruti456rawal
JavaScript
// JavaScript function to implement
// the approach
function changeToZero(a)
{
a[0] = a[a[0]];
a[1] = a[0];
}
// This code is contributed by phasing17
Time Complexity: O(1)
Auxiliary Space: O(1)
There may be many more methods.
Please write comments if you find the above codes incorrect, or find other ways to solve the same problem.
Similar Reads
A Sum Array Puzzle Given an array arr[] of n integers, construct a Sum Array sum[] (of same size) such that sum[i] is equal to the sum of all the elements of arr[] except arr[i]. Solve it without subtraction operator and in O(n). Examples: Input : arr[] = {3, 6, 4, 8, 9} Output : sum[] = {27, 24, 26, 22, 21} Input : a
15 min read
Construction of multiple AP Arrays Given an array A[] of N integers, the task is to check if it is possible to construct several arrays (at least one) using all the elements of the array A[] such that in each array, the value of each element is equal to the number of elements to its left. Examples: Input: N = 9, A[] = {0, 0, 0, 0, 1,
8 min read
Make Array Equal You are given an array A[ ] of N integers. In one operation pick any subarray A[l....r]let x be the least positive integer present in this subarrayIf x exists then update A[i] = A[i] % x (l ⤠i ⤠r)You can perform the above operation at most 10 times. Find a way to make every array element the same
4 min read
Array Rearrangement Arrays are fundamental data structures in programming, allowing us to store and manipulate collections of related data. One common operation we may need to perform on arrays is rearranging their elements. Array rearrangement can serve various purposes, such as sorting the elements, reversing the ord
3 min read
Make all elements of the Array zero Given, an array A of length N, the task is to make every element of the array equal to zero, by doing some number of moves (possibly zero). In a single move, you can choose two integers l and r such that 1 ≤ l ≤ r ≤ N. Let x = A[l] ^ A[l+1] ^ A[l+2] ... A[r], where ^ represents
6 min read