1) Java program to find the pangram stringimport java.util.*;public class Main{ public static void main (String[] args) { Scanner sc=new Scanner(System.in); String str=sc.nextLine(); boolean[] alphaList=new boolean[26]; int index=0; int flag=1; for(int i=0;i<str.length();i++) { if(str.charAt(i)>='A'&&str.charAt(i)<='Z') { index=str.charAt(i)-'A'; } else if(str.charAt(i)>='a'&&str.charAt(i)<='z') { index=str.charAt(i)-'a'; } alphaList[index]=true; } for(int i=0;i<=25;i++) { if(alphaList[i]==false) { flag=0; } } System.out.format("%s",str); if(flag==1) { System.out.print(" is a pangram"); } else { System.out.print(" is not a pangram"); } }}2) Java program to find the possible decoding ways of an integer.import java.util.*;public class Hello { static int countDecoding(char[] digits, int n) { if (n == 0 || n == 1) return 1; if(digits[0]=='0') return 0; int count = 0; count = countDecoding(digits, n - 1); if (digits[n - 2] == '1' || (digits[n - 2] == '2' && digits[n - 1] < '7')) count += countDecoding(digits, n - 2); return count; } public static void main(String[] args) { Scanner sc=new Scanner(System.in); int a=sc.nextInt(); char[] d=(""+a).toCharArray(); int n = d.length; System.out.printf("In %d ways, the given integer can be Decoded", countDecoding(d, n)); } } 3)Java program to find the missing number in the array.import java.util.*;class Main { // Function to ind missing number static int getMissingNo(int a[], int n) { int i, total; total = (n + 1) * (n + 2) / 2; for (i = 0; i < n; i++) total -= a[i]; return total; } /* program to test above function */ public static void main(String args[]) { int a[] = { 1, 2, 4, 5, 6 }; int miss = getMissingNo(a, 5); System.out.println(miss); } } sample output:34)Java program to search an element in the sorted and rotated array.import java.util.*;class Main { static int pivotedBinarySearch(int arr[], int n, int key) { int pivot = findPivot(arr, 0, n-1); if (pivot == -1) return binarySearch(arr, 0, n-1, key); if (arr[pivot] == key) return pivot; if (arr[0] <= key) return binarySearch(arr, 0, pivot-1, key); return binarySearch(arr, pivot+1, n-1, key); } static int findPivot(int arr[], int low, int high) { // base cases if (high < low) return -1; if (high == low) return low; int mid = (low + high)/2; if (mid < high && arr[mid] > arr[mid + 1]) return mid; if (mid > low && arr[mid] < arr[mid - 1]) return (mid-1); if (arr[low] >= arr[mid]) return findPivot(arr, low, mid-1); return findPivot(arr, mid + 1, high); } static int binarySearch(int arr[], int low, int high, int key) { if (high < low) return -1; int mid = (low + high)/2; if (key == arr[mid]) return mid; if (key > arr[mid]) return binarySearch(arr, (mid + 1), high, key); return binarySearch(arr, low, (mid -1), key); } public static void main(String args[]) { // Let us search 3 in below array int arr1[] = {5, 6, 7, 8, 9, 10, 1, 2, 3}; int n = arr1.length; int key = 3; System.out.println("Index of the element is : "+ pivotedBinarySearch(arr1,n,key)); } } Input : arr[] = {5, 6, 7, 8, 9, 10, 1, 2, 3}; key = 3Output : Found at index 8Input : arr[] = {5, 6, 7, 8, 9, 10, 1, 2, 3}; key = 30Output : Not foundInput : arr[] = {30, 40, 50, 10, 20} key = 10 Output : Found at index 35) Find the median of two arrays.import java.util.*;class Main { static int getMedian(int ar1[], int ar2[], int n) { int i = 0; int j = 0; int count; int m1 = -1, m2 = -1; for (count = 0; count <= n; count++) { if (i == n) { m1 = m2; m2 = ar2[0]; break; } else if (j == n) { m1 = m2; m2 = ar1[0]; break; } if (ar1[i] < ar2[j]) { m1 = m2; m2 = ar1[i]; i++; } else { m1 = m2; m2 = ar2[j]; j++; } } return (m1 + m2)/2; } public static void main (String[] args) { int ar1[] = {1, 12, 15, 26, 38}; int ar2[] = {2, 13, 17, 30, 45}; int n1 = ar1.length; int n2 = ar2.length; if (n1 == n2) System.out.println("Median is " + getMedian(ar1, ar2, n1)); else System.out.println("arrays are of unequal size"); } } sample output:Median is 16Explanation:after merging two arrays, we get{1,2,12,13,15,17,26,30,38,45}Middle elements are 15 and 17median=(15+17)/2=16.6) Java program to find the two elements in the array whose sum is closest to zero.import java.util.*; import java.lang.*; class Main { static void minAbsSumPair(int arr[], int arr_size) { int inv_count = 0; int l, r, min_sum, sum, min_l, min_r; if(arr_size < 2) { System.out.println("Invalid Input"); return; } min_l = 0; min_r = 1; min_sum = arr[0] + arr[1]; for(l = 0; l < arr_size - 1; l++) { for(r = l+1; r < arr_size; r++) { sum = arr[l] + arr[r]; if(Math.abs(min_sum) > Math.abs(sum)) { min_sum = sum; min_l = l; min_r = r; } } } System.out.println(" The two elements whose "+ "sum is minimum are "+ arr[min_l]+ " and "+arr[min_r]); } public static void main (String[] args) { int arr[] = {1, 60, -10, 70, -80, 85}; minAbsSumPair(arr, 6); } } sample output:The two elements whose sum is minimum are -80 and 857) Java program to find the smallest and second smallest element in the array.import java.util.*; class SecondSmallest { static void print2Smallest(int arr[]) { int first, second, arr_size = arr.length; if (arr_size < 2) { System.out.println(" Invalid Input "); return; } first = second = Integer.MAX_VALUE; for (int i = 0; i < arr_size ; i ++) { if (arr[i] < first) { second = first; first = arr[i]; } else if (arr[i] < second && arr[i] != first) second = arr[i]; } if (second == Integer.MAX_VALUE) System.out.println("There is no second" + "smallest element"); else System.out.println("The smallest element is " + first + " and second Smallest" + " element is " + second); } public static void main (String[] args) { int arr[] = {12, 13, 1, 10, 34, 1}; print2Smallest(arr); } } sample output:The smallest element is 1 and second Smallest element is 108) Java program to find the Maximum and minimum of an array using minimum number of comparisons.import java.util.*;public class GFG { static class Pair { int min; int max; } static Pair getMinMax(int arr[], int n) { Pair minmax = new Pair(); int i; if (n == 1) { minmax.max = arr[0]; minmax.min = arr[0]; return minmax; } if (arr[0] > arr[1]) { minmax.max = arr[0]; minmax.min = arr[1]; } else { minmax.max = arr[1]; minmax.min = arr[0]; } for (i = 2; i < n; i++) { if (arr[i] > minmax.max) { minmax.max = arr[i]; } else if (arr[i] < minmax.min) { minmax.min = arr[i]; } } return minmax; } public static void main(String args[]) { int arr[] = {1000, 11, 445, 1, 330, 3000}; int arr_size = 6; Pair minmax = getMinMax(arr, arr_size); System.out.printf("\nMinimum element is %d", minmax.min); System.out.printf("\nMaximum element is %d", minmax.max); } } sample output:Minimum element is 1Maximum element is 30009) Java program to find the minimum swaps to sort the array in ascending order.import java.io.*;import java.math.*;import java.util.*;public class Hello { static int minimumSwaps(int[] arr) { int swap=0; boolean visited[]= new boolean[arr.length]; for(int i=0;i<arr.length;i++) { int j=i,cycle=0; while(!visited[j]) { visited[j]=true; j=arr[j]-1; cycle++; } if(cycle!=0) { swap+=cycle-1; } } return swap; } public static void main(String[] args) { Scanner sc=new Scanner(System.in); int a=sc.nextInt(); int[] arr=new int[a]; for(int i=0;i<a;i++) { arr[i]=sc.nextInt(); } int res=minimumSwaps(arr); System.out.println(res); }}sample input:52 4 5 1 3sample output:310) Java program to find the minimum swaps to sort the array in descending order.import java.util.*;class Main{public static int minimumSwaps(int arr_count, int arr[]) {long long int i,count=0,j,temp,min,min_index;for(i=0;i<arr_count;i++){ min=arr[i]; min_index=i; for(j=i+1;j<arr_count;j++) { if(arr[j]>min) { min=arr[j]; min_index=j; } } if(min_index!=i) { count++; temp=arr[min_index]; arr[min_index]=arr[i]; arr[i]=temp; } }return count;}public static void main(String[] args) { Scanner sc=new Scanner(System.in); int a=sc.nextInt(); int[] b=new int[a]; for(int i=0;i<a;i++) { b[i]=sc.nextInt(); } int d=minimumSwaps(a,b); System.out.println(d); }}sample input:72 3 6 5 1 7 4sample output:5Java program to find the Longest Common SubString amoung the two given stringsimport java.util.*; public class Hello { static void printLCSubStr(String X, String Y, int m, int n) { int[][] LCSuff = new int[m + 1][n + 1]; int len = 0; int row = 0, col = 0; for (int i = 0; i <= m; i++) { for (int j = 0; j <= n; j++) { if (i == 0 || j == 0) LCSuff[i][j] = 0; else if (X.charAt(i - 1) == Y.charAt(j - 1)) { LCSuff[i][j] = LCSuff[i - 1][j - 1] + 1; if (len < LCSuff[i][j]) { len = LCSuff[i][j]; row = i; col = j; } } else LCSuff[i][j] = 0; } } if (len == 0) { System.out.println("No Common Substring"); return; } String resultStr = ""; while (LCSuff[row][col] != 0) { resultStr = X.charAt(row - 1) + resultStr; --len; row--; col--; } System.out.println(resultStr); } public static void main(String args[]) { Scanner sc=new Scanner(System.in); String X=sc.nextLine(); String Y=sc.nextLine(); int m = X.length(); int n = Y.length(); printLCSubStr(X, Y, m, n); } }sample input:skillrackSkillRacksample output:killJava program to find the substring with maximum lengthimport java.util.*;class Main{public static void main(String[] args){ Scanner sc=new Scanner(System.in); int i,j,k,l,m,n,pos=0,count=0,max=0; String a=sc.nextLine(); String b=sc.nextLine(); for(i=0;i<a.length();i++) { if(a[i]==b[i]) { count=0; for(j=i;a[j]==b[j] && j<a.length();j++) { count++; } if(count>max) { max=count; pos=i; } i=j; } } for(j=pos;a[j]==b[j] && j<strlen(a);j++) { System.out.prinr(a[j]); }}}sample input 1:skillrackskillracksample output 1:killexplanation:the common substrings in both the strings with same positions are kill and ack. the maximum length is kill so kill is printed.sample input 2:abefhcdxyosxykjxabfuicdiesgxylezsample output 2:abexplanation:the common substrings in both the string with same position are ab,cd,xy . since they are with same length and it is maximum, the first occured substring is printed as ab.Java program to print alternate uppercase and lowercase sorting of lettersimport java.util.*; import java.lang.*; public class GfG{ private final static int MAX = 100; public static String alternateSort(String s1) { int n = s1.length(); char[] s = s1.toCharArray(); int[] lCount = new int[MAX]; int[] uCount = new int[MAX]; for (int i = 0; i < n; i++) { if (Character.isUpperCase(s[i])) uCount[s[i] - 'A']++; else lCount[s[i] - 'a']++; } int i = 0, j = 0, k = 0; while (k < n) { while (i < MAX && uCount[i] == 0) i++; if (i < MAX) { s[k++] = (char)('A' + i); uCount[i]--; } while (j < MAX && lCount[j] == 0) j++; if (j < MAX) { s[k++] = (char)('a' + j); lCount[j]--; } } return (new String(s)); } public static void main(String argc[]){ Scanner sc=new Scanner(System.in); String str = sc.nextLine(); System.out.println(alternateSort(str)); } }sample input:TraDItIoNsample output:DaIoIrNtTjava program to check fibonacci sequence or not.
import java.util.*;public class Hello { public static void main(String[] args) { Scanner sc=new Scanner(System.in); String str=sc.next(); int l=str.length(); if(l==1) { System.out.print("YES"); System.exit(0); } Map<Character,Integer> hm=new LinkedHashMap<Character,Integer>(); int count=0; for(int index=0;index<l;index++) { count=0; if(hm.containsKey(str.charAt(index))) { count=hm.get(str.charAt(index)); hm.put(str.charAt(index),++count); } else { hm.put(str.charAt(index),1); } } int count1=0; List<Integer> al=new ArrayList<Integer>(); for(Map.Entry<Character,Integer> m:hm.entrySet()) { count1++; al.add(m.getValue()); } Collections.sort(al); int[] dp=new int[count1]; dp[0]=1; dp[1]=1; for(int index=2;index<count1;index++) { dp[index]=dp[index-1]+dp[index-2]; } int flag=0; for(int index=0;index<count1;index++) { if(al.get(index)==dp[index]||dp[0]==0) { flag++; } } if(flag==count1) { System.out.print("YES"); } else { System.out.print("NO"); } }}sample input:abbcccdsample output:YESRecursive addition until it becomes a single digitimport java.util.*; public class Main { static int digSum(int n) { int sum = 0; while (n > 0 || sum > 9) { if (n == 0) { n = sum; sum = 0; } sum += n % 10; n /= 10; } return sum; } public static void main(String argc[]) { Scanner sc=new Scanner(System.in); int n = sc.nextInt(); System.out.println(digSum(n)); } }input 1:78912365output 1:5input 2:1243output 2:1input 3:54678output 3:3Two Integers - Factorial:import java.util.Scanner;import java.math.BigInteger;public class Main{ static BigInteger factorial(int num) { BigInteger fact=BigInteger.ONE; for(int i=1;i<=num;i++) { fact=fact.multiply(BigInteger.valueOf(i)); } return fact; } public static void main(String[] args) { Scanner sc=new Scanner(System.in); int num1=sc.nextInt(); int num2=sc.nextInt(); BigInteger fact1=BigInteger.ONE; BigInteger fact2=BigInteger.ONE; fact1=factorial(num1); fact2=factorial(num2); System.out.print(fact1.divide(fact2)); }}input 1:4 2output 1:12explanation:Factorial of 4 is 24 and factorial of 2 is 2. hence 24/2 is 12 and is printed as output. input 2:999 997 output 2:997002Integers with atleast two repeating digits in a given range:import java.util.*;public class Main{ public static boolean valid(long n) { String str=String.valueOf(n); int l=str.length(); int count[]=new int[10]; for(int index=0;index<l;index++) { count[ str.charAt(index)-'0' ]++; } int c=0; for(int index=0;index<l;index++) { if(count[str.charAt(index)-'0' ]>=2 ) { c++; count[str.charAt(index)-'0' ]=0; } } if(c>=2) return true; return false; } public static void main(String args[]) { Scanner sc=new Scanner(System.in); long a=sc.nextLong(); long b=sc.nextLong(); int flag=0; for(long index=a;index<=b;index++) { if(valid(index)) { flag=1; System.out.print(index+" "); } } if(flag==0) System.out.print("-1"); }}input 1:1000 1025output 1:1001 1010input 2:18564 19678output 2:18581 18585 18616 18618 18661 18668 18681 18686 18717 18718 18771 18778 18781 18787 18800 18801 18810 18811 18812 18813 18814 18815 18816 18817 18818 18819 18821 18822 18831 18833 18841 18844 18851 18855 18861 18866 18871 18877 18881 18891 18899 18918 18919 18981 18989 18991 18998 19001 19009 19010 19019 19090 19091 19100 19109 19119 19122 19129 19133 19139 19144 19149 19155 19159 19166 19169 19177 19179 19188 19189 19190 19191 19192 19193 19194 19195 19196 19197 19198 19199 19212 19219 19221 19229 19291 19292 19313 19319 19331 19339 19391 19393 19414 19419 19441 19449 19491 19494 19515 19519 19551 19559 19591 19595 19616 19619 19661 19669 Simple Calculator Command:import java.util.*;public class Main{ static void calculation(int num1,int num2,char c) { if(c=='a'||c=='A') System.out.print(num1+num2); else if(c=='S'||c=='s') System.out.print(num1-num2); else if(c=='M'||c=='m') System.out.print(num1*num2); else if(c=='d'||c=='D') System.out.print(num1/num2); } public static void main(String args[]) { Scanner sc=new Scanner(System.in); String str=sc.next(); int l=str.length(); String str1="",str2=""; char c='\0'; for(int index=0;index<l;index++) { char ch=str.charAt(index); if(!(ch>='0'&&ch<='9')) { str1=str.substring(0,index); str2=str.substring(index+1,l); c=ch; break; } } calculation(Integer.parseInt(str1),Integer.parseInt(str2),c); }}input 1:1405d10output 1:140input 2:5A11output 2:16Series Team Score:import java.util.*;public class Hello { public static void main(String[] args) { Scanner sc=new Scanner(System.in); List<Integer> al=new ArrayList<>(); List<Integer> TeamA=new ArrayList<>(); List<Integer> TeamB=new ArrayList<>(); while(sc.hasNext()) { al.add(sc.nextInt()); } for(int index=0;index<al.size();index++) { if(index<al.size()/2) TeamA.add(al.get(index)); else TeamB.add(al.get(index)); } int score1=0,score2=0; for(int index=0;index<TeamA.size();index++) { if(TeamA.get(index)==TeamB.get(index)) { score1++; score2++; } else if(TeamA.get(index)>TeamB.get(index)) { score1+=3; } else if(TeamA.get(index)<TeamB.get(index)) { score2+=3; } } System.out.print(score1+" "+score2); }}input 1:3 5 13 2 0output 1:7 1explanation:TeamA and TeamB plays a series of match. If both teams draw the match, both the scores increases by one. If TeamA score is more than TeamB, the score of TeamA is added by 3, else score of TeamB is added by 3. Tower Line of sight issues:Tower A is to communicate with tower C.Tower B is to communicate with tower D.Line of sight issue can occur under the following conditions- when tower B or D is in the straight line connecting A and C- when tower A or C is in the straight line connecting B and DThe program must accept the co-ordinates of all four towers and print yes or no depending on whether Line of sight issue will occur or not.import java.util.*;public class Main { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int ax=sc.nextInt(); int ay=sc.nextInt(); int bx=sc.nextInt(); int by=sc.nextInt(); int cx=sc.nextInt(); int cy=sc.nextInt(); int dx=sc.nextInt(); int dy=sc.nextInt(); int flag=0; if(ax==cx) { if(bx==ax) { if((by>ay&&by>cy)||(by>=cy&&by<=ay)) flag=1; } else if(dx==ax) { if((dy>=ay&&dy<=cy)||(dy>=cy&&dy<=ay)) flag=1; } } else if(ay==cy) { if(by==ay) { if((bx>=ax&&bx<=cx)||(bx>=cx&&bx<=ax)) flag=1; } else if(dy==ay) { if((dx>=ax&&dx<=cx)||(dx>=cx&&dx<=ax)) flag=1; } } if(bx==dx) { if(ax==bx) { if((ay>=by&&ay<=dy)||(ay>=dy&&ay<=by)) flag=1; } else if(cx==ax) { if((cy>=by&&cy<=dy)||(cy>=dy&&cy<=by)) flag=1; } } else if(cy==ay) { if(ay==by) { if((ax>=bx&&ax<=dx)||(ax>=dx&&ax<=bx)) flag=1; } else if(cy==ay) { if((cx>=bx&&cx<=dx)||(cx>=dx&&cx<=bx)) flag=1; } } if(flag==1) System.out.print("yes"); else System.out.print("no"); }}Input 1:0 00 -22 00 2Output 1:yesInput 2:0 00 -22 00 -5Output 2:noMaximum repeating Integer:import java.util.*;public class Main { public static void main(String[] args) { Scanner sc=new Scanner(System.in); Map<Integer,Integer> hm=new LinkedHashMap<>(); List<Integer> al=new ArrayList<>(); int count=0; while(sc.hasNext()) { int n=sc.nextInt(); count=0; if(hm.containsKey(n)) { count=hm.get(n); hm.put(n,++count); } else { hm.put(n,1); } } int max=0; for(Map.Entry<Integer,Integer> m:hm.entrySet()) { if(m.getValue()>max) { max=m.getValue(); } } for(Map.Entry<Integer,Integer> p:hm.entrySet()) { if(p.getValue()==max) { al.add(p.getKey()); } } Collections.sort(al); System.out.print(al.get(0)); }}input 1:10 25 15 3 4 6 15 7 89 15 3output 1:15explanation:The maximum repeating number is printed as output. If more than two number are repeated maximum times, then the smallest number is printedNext Number Palindrome:import java.util.*;public class Main { public static void main(String[] args) { Scanner sc=new Scanner(System.in); String str=sc.next(); int n=Integer.parseInt(str); for(int index=n+1;index<=n+1000;index++) { String str1=Integer.toString(index); StringBuilder sb=new StringBuilder(str1); sb=sb.reverse(); if(str1.equals(sb.toString())) { System.out.print(str1); break; } } }}input 1:900output 1:909input 2:12567output 2:12621input 3:123output 3:131Number series arithmetic progression - value at kth position:import java.util.Scanner;public class Main{ public static void main(String[] args) { Scanner sc=new Scanner(System.in); int a=sc.nextInt(); int b=sc.nextInt(); int c=sc.nextInt(); int d=sc.nextInt(); int k=sc.nextInt(); int diff1=c-a; int diff2=d-b; int num1=a,num2=b; for(int index=1;index<=k-2;index++) { if(index%2==1) { num1=num1+diff1; } else { num2=num2+diff2; } } if(k%2==0) System.out.print(num2); else System.out.print(num1); }}input 1:1 1 5 410output 1:13input 2:2 1 4 77output 2:5Sum of digits - concatenated :import java.util.*;public class Main { public static void main(String[] args) { Scanner sc=new Scanner(System.in); char arr[]=sc.next().toCharArray(); int l=arr.length; long ans=0; for(int i=0;i<l;i++) { String k=""; int num=arr[i]-'0'; while(num != -1) { k+=num+""; num--; } long val=Long.parseLong(k); ans+=val; } System.out.print(ans); }}input 1:4093output 1:9876589630 explanation:(43210) + 0 + (9876543210) + (3210) = 9876589630 the number is concatenated till 0 and then added seperately to print the outputStrict Class Teacher - class cancellation:import java.util.*;public class Main{ public static void main(String[] args) { Scanner sc=new Scanner(System.in); int count=0; int a=sc.nextInt(); int b=sc.nextInt(); String str1=sc.next(); String[] arr=str1.split(":"); int n=Integer.parseInt(arr[0]); int m=Integer.parseInt(arr[1]); for(int i=1;i<=a;i++) { String str[]=sc.next().split(":"); int c=Integer.parseInt(str[0]); int d=Integer.parseInt(str[1]); if(c==a&&d<=m) { count++; } else if(c<n) { count++; } } if(count>=b) System.out.print("NO"); else System.out.print("YES"); }}input 1:6 39.309.30 9.33 9.35 9.30 9.28 9.40output 1:NOexplanation:if the count of students who are within the given time is more than or equal to the estimated count a 3, the class will not be cancelled. hence NO is printed.input 2:5 210.2010.30 10.20 10.21 10.35 10.24output 1:YESexplanation:here only one student comes within te time. hence the class will be cancelled and YES is printedCount number of flips to convert A to B:import java.util.*;public class Main{ public static void main(String[] args) { Scanner sc=new Scanner(System.in); int a=sc.nextInt(); int b=sc.nextInt(); int num=a^b,count=0; while(num!=0) { count+=num&1; num>>=1; } System.out.print(count); }}input 1:10 20output 1:4input 2:12 10output 2:2explanation:binary representation of 12 is 1100 and 10 is 1010. hence two bits are to be flipped to convert A to B.Maximum Integer at Kth position:import java.util.*;public class Main{ public static void main(String[] args) { Scanner sc=new Scanner(System.in); int n=sc.nextInt(); int k=sc.nextInt(); List<Integer> al1=new ArrayList<>(); List<Integer> al2=new ArrayList<>(); for(int index=0;index<n;index++) { al1.add(sc.nextInt()); } Collections.sort(al1,Collections.reverseOrder()); int max=Collections.max(al1); al1.remove(0); int j=0; for(int index=0;index<n;index++) { if(index==k-1) { al2.add(max); } else al2.add(al1.get(j++)); } for(Integer num:al2) System.out.print(num+" "); }}input 1:5 310 21 75 50 48output 1:50 48 75 21 10 explanation:the maximum num is 75. at kth position, max is placed and for remaining index, the rest elements are stired in descending order.input 2:8 210 45 17 28 19 89 9 34output 2:45 89 34 28 19 17 10 9Sum - power of number of digits:import java.util.*;public class Hello { public static void main(String[] args) { Scanner sc=new Scanner(System.in); String str=sc.next(); int size=str.length(); long n=Long.parseLong(str); long digit=0; for(int i=0;i<size;i++) { long num=Character.getNumericValue(str.charAt(i)); long ans=1; for(int j=0;j<size;j++) { ans=ans*num; } digit+=ans; } System.out.print(digit); }}input 154output 190explanation:The number of digits is 3. hence the sum is printed as (1*1*1) + (5*5*5) + (4*4*4) = 190.Sum of digits of a given number to its given power:import java.util.*;public class Main{ static int calculate(int n, int power) { int sum = 0; int bp = (int)Math.pow(n, power); while (bp != 0) { int d = bp % 10; sum += d; bp /= 10; } return sum; } public static void main(String[] args) { Scanner sc=new Scanner(System.in); int num = sc.nextInt(); int power = sc.nextInt(); System.out.println(calculate(num, power)); } }input 1:5 4output 1:13input 2:9 5output 2:27Integer formed by three digits:import java.util.*;public class Main { public static void main(String[] args) { Scanner sc=new Scanner(System.in); String str=sc.next(); int l=str.length(); Set<Character> set=new LinkedHashSet<>(); for(int index=0;index<l;index++) { set.add(str.charAt(index)); } if(set.size()==3) System.out.print("yes"); else System.out.print("no"); }}input 1:1024output 1:noinput 2:12212515output 2:yesexplaantion:the integers should be only formed by any of the 3 digits. if it is formed, then print yes, else noIntegers sum of first and third digits present in it or not:import java.util.*;public class Main{ public static void main(String[] args) { Scanner sc=new Scanner(System.in); String str=sc.next(); int sum=Character.getNumericValue(str.charAt(0))+Character.getNumericValue(str.charAt(2)); if(str.contains(Integer.toString(sum))) System.out.print("yes"); else System.out.print("no"); }}input 1:9567815output 1:yesinput 2:956746output 2:noUndo keystroke using stack:import java.util.*;public class Main{ static int top=-1; static List<Character> al=new ArrayList<>(); static class Stack { boolean isEmpty() { if(top==-1) return true; else return false; } void push(char ch) { al.add(ch); top++; } char pop() { char c=al.get(top); al.remove(top); top--; return c; } } public static void main(String[] args) { Scanner sc=new Scanner(System.in); Stack st=new Stack(); String str=sc.nextLine(); int l=str.length(); for(int i=0;i<l;i++) { char ch=str.charAt(i); if(ch=='*') { if(!st.isEmpty()) st.pop(); } else { st.push(ch); } } StringBuilder sb=new StringBuilder(); while(!st.isEmpty()) { sb.append(st.pop()); } System.out.print(sb.reverse().toString()); }}input 1:tt***uoutput 1:uinput 2:lucky * draty**woutput :lucky drawStory book stack:import java.util.*;public class Main { static class stack { int top=-1; List<String> al=new ArrayList<>(); boolean isEmpty() { if(top==-1) return true; else return false; } void push(String str) { top++; al.add(str); } String pop() { String s=al.get(top); al.remove(top); top--; return s; } String peek() { String s=al.get(top); return s; } } public static void main(String args[]) { Stack st=new Stack(); Scanner sc=new Scanner(System.in); while(sc.hasNext()) { int n=sc.nextInt(); if(n==1) { String str=sc.nextLine(); st.push(str); } else if(n==2) { if(!st.isEmpty()) System.out.println(st.peek()); } else if(n==-1) { if(!st.isEmpty()) st.pop(); } else if(n==0) { System.exit(0); } } }}input 1:1 hello 1 hi1 book 2-12-121 feels for the words2-1-11 welcome to code jamm1 learn your lessons2-120output 1:book hihello feels for the wordslearn your lessonswelcome to code jammfind HCF of two largest numbers:import java.util.*;public class Main{ static long findHCF(long X,long Y) { return (Y==0)?X:findHCF(Y,X%Y); } public static void main(String[] args) { Scanner sc=new Scanner(System.in); long X=sc.nextLong(); long Y=sc.nextLong(); System.out.print(findHCF(X,Y)); }}input 1:15 6output 1:3input 2:826784684526398 127835278457846output 2:2input 3:2438385333725472 13475999945958174output 3:6Prime factors of an integer:import java.util.*;public class Main{ static boolean primeFactor(int n) { int flag=0; for(int i=2;i<=n/2;i++) { if(n%i==0) { flag=1; break; } } if(flag==0) return true; else return false; } public static void main(String[] args) { Scanner sc=new Scanner(System.in); int n=sc.nextInt(); for(int index=2;index<=n;index++) { if(n%index==0) { if(primeFactor(index)) System.out.print(index+" "); } } }}input 1:100output 1:2 5input 2:65 output 2:5 13Average of two innings of a cricket player:import java.util.*;public class Main{ public static void main(String[] args) { Scanner sc=new Scanner(System.in); String str1[]=sc.nextLine().split(" "); String str2[]=sc.nextLine().split(" "); int l1=str1.length; int l2=str2.length; int sum1=0,sum2=0,avg1=0,avg2=0; for(int i=0;i<l1;i++) { if(Integer.valueOf(str1[i])<0) { System.out.print("INVALIDINPUT"); System.exit(0); } sum1+=Integer.valueOf(str1[i]); } for(int i=0;i<l2;i++) { if(Integer.valueOf(str2[i])<0) { System.out.print("INVALIDINPUT"); System.exit(0); } sum2+=Integer.valueOf(str2[i]); } avg1=sum1/l1; avg2=sum2/l2; if(avg1>avg2) System.out.print(sum1); else if(avg1<avg2) System.out.print(sum2); else if(avg1==avg2) { if(sum1>sum2) System.out.print(sum1); else System.out.print(sum2); } }}input 1:40 60 80 50 30 output 1:180input 2:60 -20 7090 50 67output 2:INVALIDINPUTNumber of coins Denomination:import java.util.*;public class Main{ public static void main(String[] args) { Scanner sc = new Scanner(System.in); long amt = sc.nextLong(); int[] a ={1000,500,100,50,20,10,5,1}; long notes =0; for(int i=0;i<a.length;i++) { long noOfNotes = amt/a[i]; notes+=noOfNotes; amt%=a[i]; } System.out.print(notes); }}input 1:24892output 1:33input 2:1593output 2:8explanation:(1000 * 1) + (500 * 1) + (50 * 1) + (20 * 2) + (1 * 3) = 8 coins required.Integer - count Pair sum count equals to n (solution 1):import java.util.*;public class Main{ public static void main(String[] args) { Scanner sc=new Scanner(System.in); long n=sc.nextLong(); long count=0; long start=1,end=n-1; while(start!=n&&end!=0) { if(start+end==n) { count++; } start++; end--; } System.out.print(count); }}input 1:6output 1:5input 2:158ouput 2:157input 3:27537124output 3:27537123Integer - count pairs sum equal to n (solution 2):import java.util.*;public class Hello { public static void main(String[] args) { Scanner sc=new Scanner(System.in); long num=sc.nextLong(); System.out.print(num-1); }}input 1:6output 1:5Nth term - product series:import java.util.*;import java.math.*;public class Main { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int n=sc.nextInt(); int num=2; double ans=1; for(int i=1;i<=n;i++) { int k=1; ans=1; while(k<=3) { ans=ans*num; k++; } ans=ans-num; System.out.print((long)ans+" "); num+=3; } }}input 1:3output 1:6 120 504input 2:9output 2:6 120 504 1320 2730 4896 7980 12144 17550Prime numbers ending with 7 - sum:import java.util.*;public class Main { public static void main(String[] args) { Scanner sc=new Scanner(System.in); long n=sc.nextLong(); long sum=0; boolean[] prime=new boolean[(int)n+1]; for(int i=0;i<n;i++) { prime[i]=true; } for(int p=2;p*p<=n;p++) { if(prime[p]==true) { for(int i=p*2;i<=n;i+=p) { prime[i]=false; } } } for(int i=2;i<=n;i++) { if(prime[i]==true&&i%10==7) { sum+=i; } } System.out.print(sum); }}input 1:42 output 1:61explanation:the sum of prime numbers within the given range ending with 7 are added and printed as output.input 2:158 output 2:800Seive of Eratosthenes algorithm - to find prime numbers efficiently:import java.util.*;public class Main{ static void isPrime(int n) { boolean prime[] = new boolean[n+1]; for(int index=0;index<n;index++) { prime[index] = true; } for(int index1 = 2; index1*index1 <=n; index1++) { if(prime[index1] == true) { for(int index2 = index1*index1; index2 <= n; index2 += index1) { prime[index2] = false; } } } for(int index = 2; index <= n; index++) { if(prime[index] == true) { System.out.print(index + " "); } } } public static void main(String args[]) { Scanner sc=new Scanner(System.in); int num=sc.nextInt(); isPrime(num); } } input 1:10output 1:2 3 5 7 input 2:160output 2:2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 101 103 107 109 113 127 131 137 139 149 151 15Smallest Integer - replace k digits with 0:import java.util.*;public class Main{ public static void main(String[] args) { Scanner sc=new Scanner(System.in); StringBuilder num=new StringBuilder(sc.next()); int X=sc.nextInt(); int i=0; while(X>0) { if(num.charAt(i)!='0') { num.setCharAt(i,'0'); X--; } i++; } System.out.print(Integer.parseInt(num.toString())); }}input 1:2001356 2output 1:356input 2:92output 2:0Sum and Product of digits - equal to same integer:import java.util.*;public class Main { public static void main(String[] args) { Scanner sc=new Scanner(System.in); String str=sc.next(); int l=str.length(); long sum=0,product=1; for(int index=0;index<l;index++) { sum+=Character.getNumericValue(str.charAt(index)); product*=Character.getNumericValue(str.charAt(index)); } if(sum+product==Long.parseLong(str)) System.out.print("yes"); else System.out.print("no"); }}input 1:59output 1:yesinput 2:96output 2:noSort a set in descending order:import java.util.*;public class Main { public static void main(String args[]) { Scanner sc=new Scanner(System.in); int n=sc.nextInt(); TreeSet<Integer> st=new TreeSet<>(); for(int index=0;index<n;index++) { st.add(sc.nextInt()); } NavigableSet<Integer> reverse=st.descendingSet(); Iterator<Integer> itr=reverse.iterator(); while(itr.hasNext()) { System.out.print(itr.next()+" "); } }}input 1:510 20 30 10 40output 1:40 30 20 10 input 2:836 47 29 563 36 54 47 209 output 2:563 209 54 47 36 29Smallest Possible number for a very long number sequence:import java.util.*;public class Main { public static void main(String[] args) { Scanner sc=new Scanner(System.in); String str=sc.next(); List<Character> al=new ArrayList<>(); for(int i=0;i<str.length();i++) al.add(str.charAt(i)); Collections.sort(al); String s=""; char c='\0'; if(al.get(0)=='0') { for(int i=0;i<al.size();i++) { if(al.get(i)!='0') { c=al.get(i); al.remove(i); break; } } s=s+c; for(int i=0;i<al.size();i++) { s=s+al.get(i); } } else { for(int i=0;i<al.size();i++) { s=s+al.get(i); } } System.out.print(s); }}input 1:538642581265758181111111111111100000000000000048715431747164781899000000000838167481output 1:100000000000000000000000011111111111111111111122333444444555556666777777888888888899input 2:5386425812657581811111111111111487154317471647818997486857327582357897838167481output 2:1111111111111111111111222233333444444455555555666667777777777788888888888888999Number of Ways - Sum of primes equal to N:import java.util.*;public class Main{ static boolean isPrime(int n) { int flag=0; for(int i=2;i<=n/2;i++) { if(n%i==0) { flag=1; break; } } if(flag==0) return true; else return false; } static int nextPrime(int n) { do { n++; } while(!isPrime(n)); return n; } public static void main(String[] args) { Scanner sc=new Scanner(System.in); int N=sc.nextInt(); int count=0; for(int index=2;index<=(N-index);index=nextPrime(index)) { if(isPrime(N-index)) count++; } System.out.print(count); }}input 1:18output 1:2input 2:100output 2:6input 3:14658output 3:317Numbers with exactly 3 factors:import java.util.*;import java.math.*;public class Main { static int findCount(int n) { int count=0; for(int index=1;index<=Math.sqrt(n);index++) { if(n%index==0) { if(n/index==index) count++; else count=count+2; } } return count; } public static void main(String[] args) { Scanner sc=new Scanner(System.in); int n=sc.nextInt(); int k=sc.nextInt(); int flag=0,count=0; for(int index=n;index<=k;index++) { count=findCount(index); if(count==3) { System.out.print(index+" "); flag++; } } if(flag==0) System.out.print("-1"); }}input 1:2 500output 1:4 9 25 49 121 169 289 361 input 2:10 15output 2:-1Print cycle integers till count equals k:import java.util.*;public class Main{ public static void main(String[] args) { Scanner sc=new Scanner(System.in); int m=sc.nextInt(); int n=sc.nextInt(); int k=sc.nextInt(); int count=0; OUTER:while(count<k) { for(int index=m;index<=n;index++) { System.out.print(index+" "); count++; if(count==k) break OUTER; } } }}input 1:2 5 15output 1:2 3 4 5 2 3 4 5 2 3 4 5 2 3 4 input 2:2 10 60output 2:2 3 4 5 6 7 8 9 10 2 3 4 5 6 7 8 9 10 2 3 4 5 6 7 8 9 10 2 3 4 5 6 7 8 9 10 2 3 4 5 6 7 8 9 10 2 3 4 5 6 7 8 9 10 2 3 4 5 6 Next greater number with same digits:import java.util.*;public class Main{ static void swap(char arr[],int a,int b) { char temp=arr[a]; arr[a]=arr[b]; arr[b]=temp; } static void findNextDigit(char[] arr,int n) { int index; for(index=n-1;index>0;index--) { if(arr[index]>arr[index-1]) break; } if(index!=0) { int x=arr[index-1],min=index; for(int i=index+1;i<n;i++) { if(arr[i]>x&&arr[i]<arr[min]) min=i; } swap(arr,index-1,min); Arrays.sort(arr,index,n); for(Character c:arr) System.out.print(c); } } public static void main(String[] args) { Scanner sc=new Scanner(System.in); char[] digits=sc.next().toCharArray(); int n=digits.length; findNextDigit(digits,n); }}input 1:195output 1:519input 2:12output 2:21First K integers sort:import java.util.*;public class Main { static boolean findSorted(List<Integer> al,int n) { int flag=0; List<Integer> al2=new ArrayList<>(); for(int i=0;i<n;i++) { al2.add(al.get(i)); } Collections.sort(al2); for(int index=0;index<n;index++) { if(al.get(index)==al2.get(index)) flag++; } if(flag==n) return true; else return false; } public static void main(String[] args) { Scanner sc=new Scanner(System.in); int n=sc.nextInt(); int[] arr=new int[n]; for(int index=0;index<n;index++) { arr[index]=sc.nextInt(); } int k=sc.nextInt(); List<Integer> al=new ArrayList<>(); for(int index=0;index<k;index++) { al.add(arr[index]); } Collections.sort(al); for(int index=k;index<n;index++) { al.add(arr[index]); } System.out.print((findSorted(al,n))?"YES":"NO"); }}input 1:939 23 19 27 41 55 69 88 974output 1:YESinput 2:939 23 19 27 41 78 23 88 974output 2:NOLargest number from K integers:import java.util.*;public class Main{ public static void main(String[] args) { Scanner sc=new Scanner(System.in); int n=sc.nextInt(); String[] arr=new String[n]; for(int index=0;index<n;index++) { arr[index]=sc.next(); } List<String> al=Arrays.asList(arr); Collections.sort(al,Collections.reverseOrder()); for(String str:al) System.out.print(str); }}input 1:412 19 546 60output 1:605461912input 2:546 28 5764 1929 245output 2:576446282451929Maximum possible odd integer formed by the given count of 1's and 0's:import java.util.*;public class Main{ public static void main(String[] args) { Scanner sc=new Scanner(System.in); int m=sc.nextInt(); int n=sc.nextInt(); if(n==0) System.out.print("-1"); else { StringBuilder num=new StringBuilder(); for(int i=0;i<n-1;i++) { num.append("1"); } for(int i=0;i<m;i++) { num.append("0"); } num.append("1"); int value=Integer.parseInt(num.toString(),2); if(value%2!=0) System.out.print(value); else System.out.print("-1"); } }}input 1:25 2output 1:67108865input 2:4 0output 2:-1input 3:2 3output 3:25K distinct odd integers with sum equal to n:import java.util.*;public class Main{ static boolean findSum(int n,int k) { List<Integer> al=new ArrayList<>(); if((k*k)<=n&&(n+k)%2==0) { int currSum=0,index1=1; for(int index2=1;index2<=k;index2++) { al.add(index1); currSum=currSum+index1; index1=index1+2; } al.add(n-currSum); return true; } return false; } public static void main(String[] args) { Scanner sc=new Scanner(System.in); int n=sc.nextInt(); int k=sc.nextInt(); System.out.print((findSum(n,k))?"YES":"NO"); }}input 1:4 2output 1:YESinput 2:9 2output 2:NOinput 3:736 19output 3:NOFind the term X and Y from the given series:import java.util.*;public class Main { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int[] count=new int[101]; int n=sc.nextInt(); for(int index=0;index<n;index++) { count[sc.nextInt()]++; } int x=0,y=0; for(int index=100;index>=1;index--) { if(count[index]!=0) { x=index; break; } } for(int index=1;index<=100;index++) { if(count[index]!=2) { y=index-1; break; } } System.out.print(x+" "+y); }}input 1:6 1 2 4 3 2 1 output 1:4 2explanation:the series contains 1-x & 1-y . find the value of x and y from the given series.Number series - N term sum:import java.util.*;public class Main{ public static void main(String[] args) { Scanner sc=new Scanner(System.in); int n=sc.nextInt(); int num=1,sum=0,index2=0; for(int index1=1;index1<=n;index1++) { sum=0; for(index2=num;index2<(num+index1);index2++) { sum+=index2; } System.out.print(sum+" "); num=index2; } }}input 1:3output 1:1 5 15input 2:10output 2:10 1 5 15 34 65 111 175 260 369 505