Posts

Check if a number is a power of another number

Check if a number is a power of another number Input: x = 10, y = 1 Output: True x^0 = 1 Input: x = 10, y = 1000 Output: True x^3 = 1 Input: x = 10, y = 1001 Output: False Code: package sap; public class Power {         public static void main(String[] args){                int x = 27, y = 729;                 int val = (int)Math.log(y)/ (int)Math.log(x);                 double res = Math.log(y)/Math.log(x);                 if(val == res){             System.out.println(x+" can be expressed as a power of "+y);         }                 else{             System.out.println(x +" cannot be expressed as a power of "+y);         }             } } Output: 27 can be expressed as a power of 729

Reverse words in a given string

Given a String of length N reverse the whole string without reversing the individual words in it. Words are separated by dots. Code: package sap; public class ReverseWords {         public static void main(String[] args){                 String word = "I am done";         StringBuilder result = new StringBuilder("");                 String[] arr = word.split("\\s");                 for(int i=arr.length-1; i>=0; i--){                         result.append(arr[i]);             result.append(" ");                     }                 System.out.println(result.toString());                     } }   Output: done am I  

Keys

Image

C++ Interview Questions

https://www.geeksforgeeks.org/commonly-asked-c-interview-questions-set-1/ https://www.geeksforgeeks.org/commonly-asked-c-interview-questions-set-2/

Second Largest Element

Given an array of integers, our task is to write a program that efficiently finds the second largest element present in the array. Example: Input : arr[] = {12, 35, 1, 10, 34, 1} Output : The second largest element is 34. Code: package sap; public class SecondLargest {         public static void main(String[] args){                 int[] a = {89, 24, 75, 11, 23};         int max = Integer.MIN_VALUE, secondMax = Integer.MIN_VALUE;                 for(int i=0; i<a.length; i++){                         if(a[i] > max){                                 secondMax = max;                 max = a[i];                             }             else if(a[i] > secondMax){                 secondMax = a[i];             }                     }                 System.out.println("The second maximum element is "+secondMax);             } } Output: The second maximum element is 75

Remove Spaces in a String

Code: package sap; public class RemoveSpaces {         public static void main(String[] args){                 String str = "Hi nivetha";         System.out.println("Input String: "+str);         str = str.replaceAll("\\s", "");                 System.out.println("Output String: "+str);        } } Ouput: Input String: Hi nivetha Output String: Hinivetha

Quick Sort

refer Code: package sorting_algorithms; import java.util.Arrays; public class QuickSort {     static void quickSort(int[] a, int p, int r){         if(p<r){             int pivot = partition(a, p, r);             quickSort(a, p, pivot -1);             quickSort(a, pivot+1, r);         }     }     static int partition(int[] a,int p,int r){         int pivotelem = a[r];         int i = p-1;         for(int j= p; j<r; j++){             if(a[j] < pivotelem){                 i++;                 int temp = a[i];                 a[i] = a[j];                 a[j] = temp;             }         }         int temp = a[i+1];         a[i+1] = a[r];         a[r] = temp;         return i+1;             }     public static void main(String args[])     {         int arr[] = {12, 11, 13, 5, 6, 7};         System.out.println("Given Array");         System.out.println(Arrays.toString(arr));         quickSort(arr, 0, arr.length-1);         System.out.println("\nSorted array&q