Created
August 15, 2020 16:39
-
-
Save ShubhamRwt/561243130e3565daee03994601478683 to your computer and use it in GitHub Desktop.
workshop
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.company; | |
public class Main { | |
public static void main(String[] args) { | |
oddeven(6); | |
int[] ar ={1,1,5,2,2,6,6,7}; | |
System.out.println(uniqueno(ar)); | |
System.out.println(countsetbits(4)); | |
System.out.println(magicno(5)); | |
uniqueno2(ar); | |
} | |
public static void oddeven(int n){ | |
if((n&1)==1){ | |
System.out.println("odd"); | |
} | |
else{ | |
System.out.println("even"); | |
} | |
} | |
public static int uniqueno( int[] ar){ | |
int res=0; | |
for (int i = 0; i <ar.length ; i++) { | |
res=res ^ar[i]; | |
} | |
return res; | |
} | |
public static int countsetbits(int n){ | |
int cnt=0; | |
while(n!=0){ | |
if((n&1)==1){ | |
cnt=cnt+1; | |
} | |
n=n>>1; | |
} | |
return cnt; | |
} | |
public static int magicno(int n){ | |
int pow = 1; | |
int ans =0; | |
while(n!=0){ | |
pow=pow*5; | |
if((n&1)==1){ | |
ans= ans +pow; | |
} | |
n=n>>1; | |
} | |
return ans; | |
} | |
public static void uniqueno2(int[] ar){ | |
int res=0; | |
for (int i = 0; i <ar.length ; i++) { | |
res=res^ar[i]; | |
} | |
int temp= res; | |
int cnt=0; | |
// this is to check the first rightmost set bit | |
while(temp!=0){ | |
if((temp&1)==1){ | |
break; | |
} | |
cnt= cnt+1; | |
temp = temp>>1; | |
} | |
int mask = 1<<cnt; | |
int x=0; | |
for (int i = 0; i <ar.length ; i++) { | |
if((ar[i] & mask) >=1){ | |
x= x^ar[i]; | |
} | |
} | |
int y= x^res; | |
System.out.println(x +" "+ y); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment