Posts

Showing posts from 2018

Check if a number is Palindrome

Given an integer, write a function that returns true if the given number is a palindrome, else false. For example, 12321 is a palindrome, but 1451 is not a palindrome. Solution 1st: Simplest  https://ide.geeksforgeeks.org/AJE7SI37TS import java.lang.*; import java.io.*; import java.util.*; class GFG {     public static void main (String[] args) {         int input, output, temp, remainder;         output=0;             Scanner sc = new Scanner(System.in);         input = sc.nextInt();         temp = input;             while(input != 0){             remainder = input % 10;             output = (output * 10) + remainder;             input = input/10;         }             if(output==temp)             System.out.println(output+ " is a Palindrome");         else             System.out.println(output+ " is not a Palindrome");     } } Solution 2nd: Out of the box https://ide.geeksforgeeks.org/tVpfTkXbCM import java.lang.*; import java.i

Find Pair with given Sum in the Array

Write a program that, given an array A[] of n numbers and another number x, determines whether or not there exist two elements in S whose sum is exactly x. Solution 1st: Using sorting + binary search (nlog(n)) https://ide.geeksforgeeks.org/d4GdbW8p8J import java.io.*; import java.util.*; import java.util.Arrays; class GFG {     public static void main (String[] args) {         int arraySize, sumUp;         Scanner sc = new Scanner(System.in);         arraySize = sc.nextInt();             int array[] = new int[arraySize];         for(int i=0;i<arraySize;i++)             array[i] = sc.nextInt();             sumUp = sc.nextInt();         Arrays.sort(array);             int startIndex, endIndex;         startIndex=0;         endIndex=arraySize-1;             while(startIndex<endIndex){             if(array[startIndex]+array[endIndex]==sumUp)                 System.out.println(array[startIndex++] +" + "+ array[endIndex--] +" = "+sumUp);

Write a program to reverse digits of a number and String

Solution 1st: Reversing digits of a number - simplest solution https://ide.geeksforgeeks.org/w1fALxIYWU import java.io.*; import java.util.*; class GFG {     public static void main (String[] args) {         int input, output, remainder;         output = 0;             Scanner sc = new Scanner(System.in);         input = sc.nextInt();             while(input != 0){             remainder = input % 10;             output = (output * 10) + remainder;             input = input/10;         }             System.out.println(output);     } } Solution 2nd:  Reversing digits of a number -  Out of the Box https://ide.geeksforgeeks.org/ppy5EQjEAR import java.lang.*; import java.io.*; import java.util.*; class GFG {     public static void main (String[] args) {         int input;             Scanner sc = new Scanner(System.in);         input = sc.nextInt();             StringBuilder strInput = new StringBuilder();         strInput.append(input);         strI