Archive for February 2017
DISCLAIMER: The information below is for information purposes only. I happened to be browsing on SPIEGEL Online the other day and noticed that articles published for non SPIEGEL Plus subscribers were still partially available on the article’s page in readable form while the largest portion of the text was blurred . . . Read more
In a previous post I have illustrated how to deploy a Node.js app to OpenShift from a private GitHub repository using Jenkins. It is often the case that you want to display the revision of the current code deployed in your test environment so you can quickly see if the . . . Read more
Problem statement Jack and Daniel are friends. They want to encrypt their conversation so that they can save themselves from interception by a detective agency. So they invent a new cipher. Every message is encoded to its binary representation of length . Then it is written down times, shifted by . . . Read more
Problem statement Sansa has an array. She wants to find the value obtained by XORing the contiguous subarrays, followed by XORing the values thus obtained. Can you help her in this task? Note: is contiguous subarray of while is not. Input Format First line contains an integer , number of . . . Read more
Problem Statement Given an integer, , find each such that: where denotes the bitwise XOR operator. Then print an integer denoting the total number of ‘s satisfying the criteria above. Input Format A single integer, . Constraints Output Format Print the total number of integer ‘s satisfying both of the . . . Read more
Chess is a very popular game played by hundreds of millions of people. Nowadays, we have chess engines such as Stockfish and Komodo to help us analyze games. These engines are very powerful pieces of welldeveloped software that use intelligent ideas and algorithms to analyze positions and sequences of moves, . . . Read more
Problem statement Given an array of n integers, find three integers in such that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input would have exactly one solution. Sample Input

S = [1, 2, 1, 4] target = 1 
Sample Output
Solution
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45

public class ThreeSum { private static int[] solve(int[] a, int t) { int[] ans = new int[3]; Arrays.sort(a); int n = a.length; int min = Integer.MAX_VALUE; for (int i = 1; i < n; i++) { int j = i + 1; int k = n  1; while (j < k) { int sum = a[i] + a[j] + a[k]; int diff = Math.abs(sum  t); if (diff == 0) { return ans; } if (diff < min) { min = diff; ans = new int[] { a[i], a[j], a[k] }; } if (sum < t) { // Increment current sum j++; } else { // Decrement current sum k; } } } return ans; } public static void main(String[] args) { int[] a = { 1, 2, 1, 4 }; int t = 1; int[] r = solve(a, t); System.out.println(r[0] + ", " + r[1] + ", " + r[2]); } } 