Posts

Showing posts from 2015

Java program to find the highest product of 3 elements in an array

This blog post is about java program to find the highest product of 3 elements in an array. Introduction: When this question is asked, the usual solution which comes to our mind will be multiplying each element with other elements and finding the maximum product out of those, which is the brute force solution. The function for brute force  approach is as follows: /* * Method to return the highest product of three elements. * return type: Integer * input: int arr[] */ public static Integer returnHighestProduct( int arr[]) { if ((arr== null )||(arr.length<3)) { return null ; } else { int product = Integer.MIN_VALUE; for ( int i=0;i<arr.length;i++) { for ( int j=i+1;j<arr.length;j++) { for ( int k=j+1;k<arr.length;k++) { int max_product = arr[i]*arr[j]*arr[k]; if (max_product>=product) { product = max_product; } } } } return product; } } Bu

Java program to check if an array is bitonic

This blog post is about a java program to check whether an array is bitonic or not. Introduction A bitonic array for this program is defined as an array of length 'N" and for an index 'K' in this array, where 0<K<N-1, the sequence from 0 to K should be increasing order and from K to N-1 should be in decreasing order. Eg: {1,5,3}, {1,2,3,10,9,8} Solution From starting position, find the element K , upto which the sequence is in increasing order. Check whether the sequence is in decreasing order from K to last element in the array. If not return false. Return true after execution of the method. Program package techjourney.programs; /** * Created by Sujith Mohan on 7/20/2015. * This java program is for checking whether a given array is bitonic or not. * A bitonic array for this program is defined as an array of length 'N' and * for an index 'K' in this array, where 0<K<N-1, the sequence from 0 to K should be * incre

Shell script to find factorial of a number using recursion

A shell script is a computer program designed to be run by the Unix shell, a command line interpreter. This blog is about a shell scripting program to find the factorial of a number using recursion. Logic The logic is simple. If the number ' n ' is 1, return 1. Else return ' n ' * ' factorial(n-1) '. Program #!/bin/sh factorial() { if [ "$1" -gt "1" ]; then a=`expr $1 - 1` b=`factorial $a` c=`expr $1 \* $b` echo $c else echo 1 fi } echo "Enter a number:" read x factorial $x Things to note: - After writing the script, change the mod of the script to enable execution. This is done by: chmod +x factorial.sh or chmod 755 factorial.sh Output of the program Enter a number: 4 24 Good to read 1) Java program to convert mobile number pad to characters 2) Difference between "diff" and "sdiff" command 3) Java program to return first non repeated character in a string

Java program to convert mobile number pad to characters

Image
This post is about Java program to convert mobile number pad to characters. Example ========= Here is an image of mobile phone num pad. For every numbers except 1, there are certain character value associated with that. # is used to separate the letters. If the user presses 44#44, it should print HH Logic of the program ================ A hashmap is used to store the number and its corresponding characters. If the user presses the number n times before pressing # , the nth character is selected from the hashmap. if n is greater than number of characters, select the character which is at the position (n mod number of characters) Program ======== The java program is as follows: import java.util.HashMap; public class MobilePhoneNumPadToCharacters { public static void main(String[] args) { System.out.println(returnWords( "222#2#8" )); //CAT System.out.println(returnWords( "5555#2#888888#2222" )); /

Java program to return the first non repeated character in a string

Image
This post is about java program to return the first non repeated character in a string. Example: Given a string "hhello", It should return 'e' as the first non repeated character. Logic of the program: Using a linked hash map will solve this program. All the characters[a-z] can be stored in a linked hash map as key and value should be corresponding to be number of occurrence of each character.  To return the first non repeated char, just iterate the linked hash map and return first key having value as 1. Program: package com.techjourney; import java.util.LinkedHashMap; import java.util.Map; /** * Created by Sujith Mohan on 6/4/2015. */ public class FirstNonRepeatedChar { public static char returnFirstNonRepeatedChar(String str) { if (str== null ) throw new NullPointerException(); if (str.equals( "" )) return '\0' ; LinkedHashMap<Character, Integer> map =

Java program to check if a string is pangram

Hi everyone, in this blog, I will be writing about a java program to check whether a string is pangram or not. Pangrams are words or sentences containing every letter of alphabet at least once. Most commonly used example of pangram is " A quick brown fox jumps over the lazy dog. " Logic of the program Initialize a string testString as "abcdefghijklmnopqrstuvwxyz" Loop the input string for every character, and replace those character in testString with empty character. If testString is an empty string, then input string is Pangram The program for pangram is as follows: package com.blog.techjourney; public class Pangram { public static Boolean checkForPangram(String str) { int len = str.length(); //test string for testing all characters are present in the given string String testString = "abcdefghijklmnopqrstuvwxyz" ; for ( int i=0;i<len;i++) { char chars =str.charAt(i); //Converting the char to String a

Difference between "diff" and "sdiff" commands in Unix

Image
Hi everyone, in this blog post, I will be discussing about the difference between "diff" and "sdiff" commands in Unix. This is my first post about Unix. diff command diff command will compare two files and prints the difference between the 2 files. The format of diff command is diff file1 file2 . sdiff command sdiff command will compare two files side by side. The format of sdiff command is sdiff file1 file2. The contents of files are displayed side by side with a marker denoting the difference between each files. Some of the important markers used in sdiff are as follows: | - denotes the 2 files differ in the line completely. < - only the first file contains the line. > - only the second file contains the line. The better way to depict the difference is by showing examples. Here, I am comparing 2 files which are named as " ipl_teams_2008.txt " and " ipl_teams_2015.txt ". Let's see the contents of the files.

Java program to check if two elements of an array adds to the given sum

Hi everyone, in this blog post, I will be discussing about a famous java program, which is to check if two elements of an array adds to the given sum. Problem Given an array with elements and a sum. Check whether any 2 elements of the array adds to the given sum. Return true if the sum is present, else return false. Example Given an array {1,2,4}. The array should return true for the sum 5 and false for 4. Solution The basic solution which comes to everyone's mind would be to check each element with each other elements for the given sum. But the total time complexity in doing that will be O(n^2). We will approach the problem in a much easier way. The logic for that is as follows: Sort the given array in ascending order. Create two variables, one which tracks from the first position of array ( pos_first ) and other which tracks from the last position of the array ( pos_last ). While pos_first is less than pos_last , do the below steps. Add the elements corr

Java program to change the elements of the array with the product of remaining elements

Hi Everyone, In this blog post I will be discussing about an interesting program, which is to change the " elements of the array with the product of remaining elements ". This program is really interesting as well as tricky. If an array contains 0, the product of all the elements in the array will be 0. For eg: If an array is [0,1,2], after performing the algorithm, it will be [2,0,0]. Logic The logic of the program is as follows: Initialize an integer variable product = 1. (This variable is used to store the product of all elements of array) Initialize an integer variable countOfZeros =0 Find the product of all the non-zero elements in the array as well as count the number of zero's in the array. If countOfZeros is greater than or equal to 2, then change all the elements of array to 0. If countOfZeros is 1, then change all non zero elements to 0 and 0 to product . If countOfZeros is 0, then change all elements to product /(element). IDE used - Intel

Java program to get the Next Greater Element in an array

Hi Everyone, it's been a long time since I have made a post. So here goes one. In this blog post, I will be writing about a Java program to get the next greater element in an array. Expected output : For every element in the array, print the number which is the next greater to the element and which occurs after the element in position. If greater element is not found print -1. Logic of the program : Assign an element next greater value, " ngv "as -1. For every element " i " in the array, loop from the next element till last element. In the loop, if an element is greater than " i " and " ngv " is -1 , assign " ngv " as the element. In the loop, if an element is greater than " i " and " ngv " is not -1, check whether " ngv " greater than that element. If so, assign " ngv " as the element. Return " ngv ". The java program is as follows: package  com.blog.techjourney;