Posts

Program to place all zeros in the end of the array

This post will help you in understanding how to place all the zeros in the end of an unsorted array. The logic is like this - whenever you encounter a non zero element increment the counter and check if the counter less than the traversed position. If counter is less than the traversed position, copy the element from traversed position to the position of counter in the array. Complexity - O(n). The program is as follows - public class arrayzero { public static void main(String args[]) { int arr[] = {15,2,5,1,0,5,6,0,0,3,7,9,0,0,66,7,0,3,0,-1,2,0}; zerolast(arr); System. out .println( " Modified array = " ); for ( int i=0;i<arr. length ;i++) { System. out .print(arr[i] + "  " ); } } public static void zerolast( int arr[]) { int index=-1; for ( int i=0; i<arr. length ;i++) { if (arr[i]!=0) { index++; if (i>index) { arr[index]=arr[i]; ...

Program to find pythagorean triplets

This program helps you in finding the total number of pythagorean triplets in a given range. Here, the total time complexity, we are taking is O(n^2). In the first loop, we will traverse all the elements from largest to smallest. In the second loop, we will traverse all the remaining elements in the from largest to smallest. Then for each elements we will subtract the square of element found in second loop from the element found in first loop. If the resulting value is a perfect square, then we found a pythagorean triplet, which contains the value of the outer loop, value of inner loop and square root of perfect square. public class pytho {              public static int numPythagoreanTriplets( int N) {                 int x = 0;                 for ( int c = N; c > 1; c--) {                     ...

Program to find the maximum sum of elements in an array

This program will help you in finding the maximum sum of elements in an array. If all the elements are positive, the maximum sum is sum of all the elements in the array. If all the elements are negative, the maximum sum is the first element in array. If array has both positive and negative elements, then we need to traverse the entire elements. Here, we are initializing 2 variables sum and maxsum as 0. For all the elements, we are adding to sum. If sum > maxsum, we will add those value to maxsum as well. public class arraySum { public static void main(String args[]) { int array[] = {1,2,5,-3,-5,-6,11,13,-2,1,-5,4}; int maxsum = sum(array); System. out .println( " Maximum sum = " +maxsum); } public static int sum( int arr[]) { int sum=0; int maxsum=0; for ( int i=0; i<arr. length ;i++) { sum=sum+arr[i]; if (maxsum<sum) { maxsum=sum; } } return maxsum; } ...

Java program to find whether a string is palindrome or not

This post help you in finding whether a string is palindrome or not. Here we are copying the string from  its last position to another string and checking whether they are equal or not. The time complexity is O(n). It can also be done without using additional memory. import java.util.Scanner; public class palindrome { public static void main(String args[]) { Scanner in= new Scanner(System. in ); String original,reverse= "" ; do { System. out .println( "Enter the string to check.." ); original=in.nextLine(); reverse= "" ; int length=original.length(); for ( int i=length-1;i>=0;i--) { reverse=reverse+original.charAt(i); } System. out .println( "Original= " +original + " Reverse= " +reverse); if (original.equalsIgnoreCase(reverse)) { System. out .println( "Palindrome" ); } else { System. out .p...

Program to find the second largest element in an array

This program helps you to find the second largest element in an array. There are many methods by which we can find the second largest element. I am using a method which has the lowest time complexity - O(n). Here we are first finding the largest element in an array by traversing each element. In the next iteration, we are checking whether each element is less than the largest element and thus assigning its value to second largest element. There are some other methods like sorting the array and traversing from n-2 th element to 0th element to check whether it is different from n-1 th element and returning the value. This will take a time complexity of O(n logn). package com.programs; /* * Java program to get the second largest element in an array */ public class secondLargest { public static int findSecondLargest( int arr[]) { int length = arr.length; int large, slarge, flag = 0; large = arr[0]; slarge = arr[0]; for ( int i = 0; i < length; i++) ...

Duplicate values in an array

The program is about finding the duplicate values inside an array. Here, we are adding all the values of the array to a map and check if the  value is already existing in the map. If the get count of map is equal to 2, the value will be added to a set. Finally the set which contains all the duplicate values will be returned to the main function. The program is as shown below: package com.programs; import java.util.HashMap; import java.util.HashSet; import java.util.Map; import java.util.Set; /* * Java program to print duplicate elements in an array */ public class dupList { public static void main(String args[]) { int arr[] = { 1, 6, 4, 4, 4, 3, 6, 7, 2, 2, 9, 0, 13, 12, 1, 0, 4, 13, 7 } ; Set<Integer> bset = new HashSet<Integer>(); bset = returndup(arr); System.out.println( " The list of duplicate elements are as follows.." ); for (Integer i : bset) { System.out.print(i + " " ); } } ...

Binary tree implementation in java

This post will teach you how to implement a binary tree in java and perform the following functions on it. insert - Inserts the elements to binary tree printInOrder - To print the inorder elements of the binary tree maxDepth and minDepth - To find whether a tree is balanced or not commonAncestor - To find the common ancestor between two nodes findSum - to find all paths whose sum is equal to the given sum addToTree - To insert elements to the tree in such a way that there will be minimum number of leaves. findLevelLinkedList - To find elements in each level The java program is as follows: package com.algorithm; import java.util.ArrayList; import java.util.LinkedList; public class binaryTree { public static void main(String args[]) { new binaryTree().run(); } public static class Node { Node left; Node right; int value; public Node( int value) { this.value = value; } } public void run() { Node ro...