Skip to content

Instantly share code, notes, and snippets.

View tgvdinesh's full-sized avatar

Dinesh V tgvdinesh

View GitHub Profile
@tgvdinesh
tgvdinesh / Android API connection using HttpURLConnection
Last active August 29, 2015 14:15 — forked from anonymous/gist:1c04bf2423579e9d2dcd
The sample code provided is used to get data from any API in Android
// These two need to be declared outside the try/catch
// so that they can be closed in the finally block.
HttpURLConnection urlConnection = null;
BufferedReader reader = null;
// Will contain the raw JSON response as a string.
String forecastJsonStr = null;
try {
// Construct the URL for the OpenWeatherMap query
@tgvdinesh
tgvdinesh / BinarySearchTree.java
Last active November 15, 2016 15:46
Binary search Tree data structure implementation in Java
package com.oracle.java;
public class BinarySearchTree {
private int treeHeight(Node root) {
if (root == null) return 0;
return (1 + Math.max(treeHeight(root.left), treeHeight(root.right)));
}
private boolean isBalancedNaive(Node node) {
@tgvdinesh
tgvdinesh / App.java
Created November 18, 2016 08:29
Divide and conquer approach to find if sum of any two value a and b is equal to user given input.
public class App {
public static void main(String[] args) {
int x = 8;
int[] arr = {-3, -2, 1, 7, 8, 11, 12, 21};
for (int i = 0; i < arr.length; i++) {
int a = arr[i];
int b = x - a;
if (b <= arr[arr.length - 1]) {
int result = divideAndConquer(arr, b, i + 1, arr.length - 1);
if (result != -1) {
@tgvdinesh
tgvdinesh / maxInversions.java
Created January 14, 2017 04:00
Count Inversions in an array (Possible inversion 3) - http://www.geeksforgeeks.org/counting-inversions/
public class App {
public static void main(String[] args) {
int[] prices = {5, 8, 6, 1, 4, 5};
int count = 0;
if (prices.length >= 1 && prices.length <= 5000) {
for (int i = 0; i < prices.length; i++) {
if (i <= prices.length - 2) {
for (int j = i + 1; j < prices.length; j++) {
if (j <= prices.length - 1) {
if (prices[i] > prices[j]) {
@tgvdinesh
tgvdinesh / Summation.java
Created January 17, 2017 03:13
Summation program with file read & write snippet
class Summation {
public static void main(String[] args) throws IOException {
// Read & write data from file.
Scanner in = new Scanner(System.in);
final String fileName = System.getenv("OUTPUT_PATH");
BufferedWriter bw = new BufferedWriter(new FileWriter(fileName));
int res;
int _numbers_size = 0;
_numbers_size = Integer.parseInt( in .nextLine().trim());
@tgvdinesh
tgvdinesh / Tree.cpp
Created January 19, 2017 03:38
Tree DS implementation in C++ language
#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
@tgvdinesh
tgvdinesh / Tree.c
Created January 19, 2017 03:38
Tree DS implementation in C language
int main(){
node * root = NULL;
int a[100005],K,i = 0,j = 0, _element, present;
scanf("%d",&K);
for( j = 0; j < K;j++ ) {
scanf("%d",&a[i++]);
}
for( i = 0; i < K;i++ ){
@tgvdinesh
tgvdinesh / ExceptionHandling.java
Created February 5, 2017 09:02
Exception handling in Java 1.6
package com.example.java;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
/**
* Exception handling
* <a href="http://www.journaldev.com/1696/exception-handling-in-java">Exception Handling in Java</a>
@tgvdinesh
tgvdinesh / ThreadJoin.java
Created February 6, 2017 00:58
Sometimes we need to wait for other threads to finish it’s execution before we can proceed. We can achieve this using Thread join, learn how it works and when we should use it.
import java.util.logging.Logger;
/**
* Thread join
* <a href="http://www.journaldev.com/1024/java-thread-join-example">Java Thread Join</a>
*/
class ThreadJoin {
static Logger logger = Logger.getLogger(App.class.getSimpleName());
@tgvdinesh
tgvdinesh / XMLParser.java
Created February 6, 2017 10:46
Recursively looks for a node name and prints specified attribute value
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.IOException;