Posts

Showing posts with the label InterviewPrograms

How To Move Zeros To End Of An Array?

Problem : Write a Java program to separate zeros from non-zeros in the given array. You have to move zeros either to end of the array or bring them to beginning of the array. For example, if{14, 0, 5, 2, 0, 3, 0} is the given array, then moving zeros to end of the array will result {14, 5, 2, 3, 0, 0, 0} import java.util.Arrays;   public class   MoveZerosToEndOfAnArray {     static void moveZerosToEnd( int inputArray [])     {         //Initializing counter to 0           int counter = 0;           //Traversing inputArray from left to right           for ( int i = 0; i < inputArray . length ; i ++)         {             //If inputArray[i] is non-zero         ...

Write a Java Program to count the number of letters, numbers ,spaces and others in a string

import java.util.Scanner; public class CountingProgram {                       static void count(String x ){                               char [] ch = x .toCharArray();                               int letter = 0;                               int space = 0;                               int num = 0; ...

Word Reverse Program

How To Reverse The Order Of  Words In A Given Input String. Input: "My name is Darga " Output: "Darga is name My" package praticeprogrms2; public class WordReverse {     public static void main(String[] args )     {         String input = "My name is Darga" ;         String output = reverseWords ( input );         System. out .println( "Input : " + input );         System. out .println( "Output : " + output );     }     public static String reverseWords(String input )     {         String[] words = input .split( " " ); // Split the input string into words         StringBuilder reversed = new StringBuilder();         for ( int i = words . length ...

Given an array of numbers, find the maximum and minimum element in the array ?

 *** Given an array of numbers, find the maximum and minimum element in the array. Input [54, 546, 548, 60] Output 548 54 package InterviewProgram; public class MaxMinElement {     public static void main(String[] args ) {         int [] arr = {54, 546, 548, 60};         System. out .println( "input = {54, 546, 548, 60}" );         int max = arr [0]; // Initialize max with the first element of the array         int min = arr [0]; // Initialize min with the first element of the array         // Iterate through the array to find the maximum and minimum elements         for ( int i = 1; i < arr . length ; i ++) {             if ( arr [ i ] > max ) {    ...

Given a number N, print reverse of number N ?

 *** Given a number N, print reverse of number N. Input 988 Output 889 Note Do not print leading zeros in output. For example N = 100 Reverse of N should be 1 not 001. Constraints 1<=N<=10000 package InterviewProgram; import java.util.Scanner; public class ReverseNumber {     public static void main(String[] args ) {         Scanner scanner = new Scanner(System. in );         System. out .print( "Input  : " );             int N = scanner .nextInt();            int   reversedNumber  =  reverseNumber ( N );         System. out .println( "Output : " + reversedNumber );     }      public   static   int  reverseNumber( int   number ) {          // Convert the number to a string ...

Given an array of numbers, arrange them in a way that it forms the largest value ?

 *** Given an array of numbers, arrange them in a way that it forms the largest value. Input [54, 546, 548, 60] Output 6054854654 Note The arrang ote ement 6054854654 gives the largest value. Constraints 1<=N<=1000 1<=Number<=1000000 package InterviewProgram; import java.util.Arrays; import java.util.Comparator; public class LargestValueArrangement {    public static void main(String[] args )     {    int [] arr = {54, 546, 548, 60};         System. out .println( "input   = {54, 546, 548, 60} " );         String largestValue = arrangeLargestValue ( arr );         System. out .println( "output = " + largestValue );     }     private static String arrangeLargestValue( int [] arr )     {    String[] strArr = new String[ arr . length ]; ...