Saturday, October 28, 2017

Algorithm

BinaryGap

binary gap within a positive integer N is any maximal sequence of consecutive zeros that is surrounded by ones at both ends in the binary representation of .N. 

public int solution(int N) {
String binaryNo = Integer.toBinaryString(N);
int firstIndex = binaryNo.lastIndexOf("1");
int currentGap = 0;
int biggestGap = 0;
for (int i = firstIndex; i >= 0; i--) {
if (getNthDegit(binaryNo, i) == 0) {
currentGap++;
} else {
if (biggestGap < currentGap) {
biggestGap = currentGap;
}
currentGap = 0;
}
}
return biggestGap;
}

public static int getNthDegit(String number, int position) {

String numberString = "" + number;
char c = numberString.charAt(position);
return Character.getNumericValue(c);
}


OddOccurrencesInArray


Find value that occurs in odd number of elements.

public int solution(int[] A) { int num = 0; int max = 0; for (int i = 0; i < A.length; i++) { if (max <= A[i]) { max = A[i]; } } int[] B = new int[max]; for (int i = 0; i < A.length; i++) { if (B[A[i] - 1] == 0) { B[A[i] - 1] = A[i]; } else { B[A[i] - 1] = 0; } } for (int i = 0; i < B.length; i++) { if (B[i] != 0) { num = B[i]; break; } } return num; }



CyclicRotation

Each element is shifted right by one index, and the last element of the array is also moved to the first place.


public int[] solution(int[] A, int K) { if (K > 0 && A.length > 0) { K = K % A.length; int[] B = new int[A.length]; for (int i = 0; i < A.length; i++) { if ((i + K) > (A.length - 1)) { B[i + K - A.length] = A[i]; } else { B[i + K] = A[i]; } } return B; } else { return A; } }


FrogJmp


Count minimal number of jumps from position X to Y.


public int solution(int X, int Y, int D) { if (X == Y) return 0; else if ((Y - X) % D == 0) return ((Y - X) / D); else return ((Y - X) / D) + 1; }


Distinct

Compute number of distinct values in an array.


public int solution(int[] A) { Set<Integer> s = new HashSet<>(); for (int i = 0; i < A.length; i++) { s.add(A[i]); } return s.size(); }


CountDiv


Compute number of integers divisible by k in range [a..b].


public int solution(int A, int B, int K) {

return (B / K) - (A / K) + ((A % K) == 0 ? 1 : 0); }


TapeEquilibrium

Minimize the value |(A[0] + ... + A[P-1]) - (A[P] + ... + A[N-1])|. The difference between the two parts is the value of: |(A[0] + A[1] + ... + A[P − 1]) − (A[P] + A[P + 1] + ... + A[N − 1])|.

public int solution(int[] A) {
int N = A.length; int leftsum = 0; int difference; int rightSum; int min = Integer.MAX_VALUE; int sum = 0; for (int i = 0; i < N; i++) { sum = sum + A[i]; } for (int i = 0; i < N - 1; i++) { leftsum = leftsum + A[i]; rightSum = sum - leftsum; difference = Math.abs(rightSum - leftsum); min = Math.min(min, difference); } return min; }


Friday, July 14, 2017

Simple JSP Ajax Tutorial

In this blog post, a simple JSP Ajax sample for adding two numbers.  Ajax is a method for returning a result without reloading the HTML page using JavaScript, DOM, XMLHttpRequest and CSS technologies.

ajaxsample.html


<html>

<head>
<script type="text/javascript">
var request;
function addnum() {
var num1 = document.addform.num1.value;
var num2 = document.addform.num2.value;
var url = "sum.jsp?num1=" + num1 + "&num2=" + num2;
if (window.XMLHttpRequest) {
request = new XMLHttpRequest();
} else if (window.ActiveXObject) {
request = new ActiveXObject("Microsoft.XMLHTTP");
}

try {

request.onreadystatechange = getsum;
request.open("GET", url, true);
request.send();
} catch (e) {
alert("Unable to process");
}
}

function getsum() {

if (request.readyState == 4) {
var sum = request.responseText;
document.getElementById("sumvalue").innerHTML = sum;
}
}
</script>
</head>
<body>
<h1>JSP Ajax Simple Example</h1>
<form name="addform">
<input type="text" name="num1"> + <input type="text"
name="num2"> <input type="button" value="Add"
onclick="addnum()">
</form>
<span id="sumvalue"></span>
</body>
</html>


sum.jsp


<%

int num1 = Integer
.parseInt(request.getParameter("num1").toString());
int num2 = Integer
.parseInt(request.getParameter("num2").toString());
int sum = num1 + num2;
out.print("Sum is " + sum);
%>

web.xml


<?xml version="1.0" encoding="UTF-8"?>

<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
   <session-config>
        <session-timeout>
            30
   </session-timeout>
 </session-config>
 <welcome-file-list>
        <welcome-file>ajaxsample.html</welcome-file>
        </welcome-file-list>
 </web-app>

Create a REST API with Spring Boot

In this post, I will explain how to create a simple a REST API with Spring Boot Spring Boot Spring Boot is a framework that provides inbuil...