Sum vs XOR

From Manogna, 1 Month ago, written in Java, viewed 27 times.
URL http://codebin.org/view/6becb8e2 Embed
Download Paste or View Raw
  1. import java.io.*;
  2. import java.math.*;
  3. import java.security.*;
  4. import java.text.*;
  5. import java.util.*;
  6. import java.util.concurrent.*;
  7. import java.util.function.*;
  8. import java.util.regex.*;
  9. import java.util.stream.*;
  10. import static java.util.stream.Collectors.joining;
  11. import static java.util.stream.Collectors.toList;
  12.  
  13. class Result {
  14.  
  15.     /*
  16.      * Complete the 'sumXor' function below.
  17.      *
  18.      * The function is expected to return a LONG_INTEGER.
  19.      * The function accepts LONG_INTEGER n as parameter.
  20.      */
  21.  
  22.     public static long sumXor(long n) {
  23.         long count = 1;
  24.         if(n==0L)
  25.             return 1;
  26.         if((n&(n-1))==0)
  27.             return n;
  28.         for(long i=1;i<n;i++){
  29.             if((i+n)==(i^n))
  30.                 count++;
  31.         }
  32.         return count;
  33.        
  34.     // Write your code here
  35.  
  36.     }
  37.  
  38. }
  39.  
  40. public class Solution {
  41.     public static void main(String[] args) throws IOException {
  42.         BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
  43.         BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));
  44.  
  45.         long n = Long.parseLong(bufferedReader.readLine().trim());
  46.  
  47.         long result = Result.sumXor(n);
  48.  
  49.         bufferedWriter.write(String.valueOf(result));
  50.         bufferedWriter.newLine();
  51.  
  52.         bufferedReader.close();
  53.         bufferedWriter.close();
  54.     }
  55. }
  56.  

Reply to "Sum vs XOR"

Here you can reply to the paste above