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>
usingnamespacestd;
voidchangeToZero(inta[2])
{
a[ a[1] ] = a[ !a[1] ];
}
// Driver code
intmain()
{
inta[] = {1, 0};
changeToZero(a);
cout<<"arr[0] = "<<a[0]<<endl;
cout<<" arr[1] = "<<a[1];
return0;
}
// This code is contributed by rathbhupendra
C
voidchangeToZero(inta[2])
{
a[ a[1] ] = a[ !a[1] ];
}
intmain()
{
inta[] = {1, 0};
changeToZero(a);
printf(" arr[0] = %d \n", a[0]);
printf(" arr[1] = %d ", a[1]);
getchar();
return0;
}
Java
importjava.io.*;
classGFG{
publicstaticvoidchangeToZero(inta[])
{
a[a[1]] = a[1- a[1]];
}
// Driver code
publicstaticvoidmain(String args[])
{
int[] arr;
arr = newint[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
defchangeToZero(a):
a[ a[1] ] =a[ nota[1] ]
returna
# 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#
usingSystem;
classGFG {
publicstaticvoidchangeToZero(int[] a)
{
a[a[1]] = a[1 - a[1]];
}
// Driver code
publicstaticvoidMain()
{
int[] arr;
arr = newint[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>
functionchangeToZero(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++
voidchangeToZero(inta[2])
{
a[ !a[0] ] = a[ !a[1] ];
}
C
voidchangeToZero(inta[2])
{
a[ !a[0] ] = a[ !a[1] ]
}
Java
voidchangeToZero(int[2]a) {
a[!a[0]] = a[!a[1]];
}
// This code is contributed by souravmahato348.
Python3
defchangeToZero(a):
a[ !a[0] ] =a[ !a[1] ]
# This code is contributed by sanjoy_62.
C#
staticvoidchangeToZero(int[2]a) {
a[!a[0]] = a[!a[1]];
}
// This code is contributed by souravmahato348.
Javascript
<script>
functionchangeToZero(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
voidchangeToZero(inta[2])
{
a[ a[1] ] = a[ a[0] ]
}
Java
staticvoidchangeToZero(inta[2])
{
a[ a[1] ] = a[ a[0] ]
}
// this code is contributed by shivanisinghss2110
Python3
defchangeToZero(a) :
a[ a[1] ] =a[ a[0] ]
C#
staticvoidchangeToZero(int[] a)
{
a[ a[1] ] = a[ a[0] ];
}
//this code is contributed by phasing17
Javascript
functionchangeToZero(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
voidchangeToZero(inta[2])
{
a[0] = a[a[0]];
a[1] = a[0];
}
Java
staticvoidchangeToZero(inta[])
{
a[0] = a[a[0]];
a[1] = a[0];
}
//This code is contributed by shruti456rawal
C#
staticvoidchangeToZero(int[] a)
{
a[0] = a[a[0]];
a[1] = a[0];
}
//This code is contributed by shruti456rawal
Python3
# Python code for the above approach
defchangeToZero(a) :
a[0] =a[a[0]]
a[1] =a[0]
# This code is contributed by splevel62.
Javascript
// JavaScript function to implement
// the approach
functionchangeToZero(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.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy
Improvement
This article is being improved by another user right now. You can suggest the changes for now and it will be under the article’s discussion tab.
You will be notified via email once the article is available for improvement.
Thank you for your valuable feedback!
Please Login to comment...