Posts

Showing posts from May, 2021

Diamond of Stars

Image
  Pattern for N = 5 import java.util.Scanner; public class DiamondOfStars { public static void main(String[] args) { Scanner s = new Scanner(System.in); int n= s.nextInt(); int f=n/2; int l=n-f; int sp=f; for(int i=1;i<=f;i++) { for(int k=1;k<=sp;k++) { System.out.print(" "); } sp--; for(int j=1;j<=2*i-1;j++) { System.out.print("*"); } System.out.println(); } int space = 1; for(int i=l;i>=1;i--) { for(int k=1;k<space;k++) { System.out.print(" "); } space++; for(int j=1;j<=2*i-1;j++) { System.out.print("*"); } System.out.println(); } } }

Triangle of Numbers

Image
Pattern for N = 4 import java.util.Scanner; public class TriangleOfNum { public static void main(String[] args) { Scanner s = new Scanner(System.in); int n= s.nextInt(); int p=0; int q=0; for(int i=1;i<=n;i++) { for(int j=1;j<=n-i;j++) { System.out.print(" "); } p=i; for(int k=1;k<=i;k++) { System.out.print(p); p++; } q=2*i-2;             for(int d = i-1; d>=1;d--)             {                         System.out.print(q);             q--;             } System.out.println(); } } }

Star Pattern

Image
  Pattern for N = 4 import java.util.Scanner; public class StarPattern { public static void main(String[] args) { Scanner s = new Scanner(System.in); int n= s.nextInt(); for(int i=1;i<=n;i++) { for(int j=1;j<=n-i;j++) { System.out.print(" "); } for(int k=1;k<=2*i-1;k++) { System.out.print("*"); } System.out.println(); } } }

Inverted Number Pattern

  Pattern for N = 4 4444 333 22 1 import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int k; for(int i=n; i>=1;i--) { for(int j=1; j<=i;j++) { System.out.print(i); } System.out.println(); } } }

Mirror Image Number Pattern

Image
  Pattern for N = 4 import java.util.Scanner; public class MirrorImagePattern { public static void main(String[] args) { Scanner s = new Scanner(System.in); int n= s.nextInt(); for(int i=1;i<=n;i++) { for(int j=1;j<=n-i;j++) { System.out.print(" "); } for(int k=1;k<=i;k++) { System.out.print(k); } System.out.println(); } } }

Interesting Alphabets Pattern

  Pattern for N = 5 E DE CDE BCDE ABCDE import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); for(int i=n; i>=1;i--) { char c = (char)('A' + i - 1); for(int j=i ;j<=n;j++) { System.out.print(c);                c = (char)(c + 1); } System.out.println(); } } }

Character Pattern

  Sample Input 1: 5 Sample Output 1: A BC CDE DEFG EFGHI import java.util.Scanner; public class CharacterPattern { public static void main(String[] args) { Scanner s = new Scanner(System.in); int n= s.nextInt(); for(int i=1;i<=n;i++) { char c=(char)('A'+ i -1); for(int j=1;j<=i;j++) { System.out.print(c); c++; } System.out.println(); } } }

Alphabets Pattern

  Sample Input 1: 7 Sample Output 1: A BB CCC DDDD EEEEE FFFFFF GGGGGGG import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); for(int i=1; i<=n;i++) { for(int j=1 ;j<=i;j++) { char c = (char)('A' + i -1); System.out.print(c); } System.out.println(); } } }

Square Root

  Given a number N, find its square root. You need to find and print only the integral part of square root of N. Sample Input 1 : 10 Sample Output 1 : 3 import java.util.Scanner; public class SquareRoot { public static void main(String[] args) { Scanner s = new Scanner(System.in); int n= s.nextInt(); int op=0; while(op*op<=n) { op++; } op--; System.out.println(op); } }

Decimal to Binary

  Given a decimal number (integer N), convert it into binary and print. Sample Input 1 : 12 Sample Output 1 : 1100 import java.util.Scanner; public class DecToBin { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int binary[] = new int[100]; int index = 0; if(n==0) { System.out.println("0"); } while(n > 0) { binary[index++] = n%2; n = n/2; } for(int i = index-1;i >= 0;i--) { System.out.print(binary[i]); } } }

Binary to Decimal

  Given a binary number as an integer N, convert it into decimal and print. Sample Input 1 : 1100 Sample Output 1 : 12 import java.util.Scanner; import java.io.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int i=0; int s=0; while(n!=0) { int d=n%10; int b= d* (int)Math.pow(2,i); s=s+b; n=n/10; i++; } System.out.println(s); } }

Reverse of a Number

  Write a program to generate the reverse of a given number N. Print the corresponding reverse number. Sample Input 1 : 1234 Sample Output 1 : 4321 import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int r=0; while(n>0) { int d= n%10; r=r*10+d; n=n/10; } System.out.println(r); } }

Terms of AP

  Write a program to print first x terms of the series 3N + 2 which are not multiples of 4. Sample Input 1 : 10 Sample Output 1 : 5 11 14 17 23 26 29 35 38 41 import java.util.*; public class TermsOfAP { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int sum=0; int p=n; for (int i=1; i<=p; i++) { sum=3*i+2; if (sum % 4 != 0) { System.out.print(sum+ " "); } else { //Increasing p in case sum is divisible by 4 so that we get n terms p++; } } } }

Sum or Product

  Write a program that asks the user for a number N and a choice C. And then give them the possibility to choose between computing the sum and computing the product of all integers in the range 1 to N (both inclusive). If C is equal to - 1, then print the sum 2, then print the product Any other number, then print '-1' (without the quotes) Sample Input 1 : 10 1 Sample Output 1 : 55 import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt();           int c = sc.nextInt(); if(c==1) { int s=0; for(int i=1;i<=n;i++) { s=s+i; } System.out.println(s); } else if(c==2) { int m=1; for(int j=1;j<=n;j++) { m=m*j; } System.out.println(m); } else {

All Prime Numbers

  Given an integer N, print all the prime numbers that lie in the range 2 to N (both inclusive). Sample Input 1: 9 Sample Output 1: 2 3 5 7 import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); for( int i=2;i<=n;i++) { int c=0; for(int j=1;j<=i;j++) { if(i%j==0) { c++; } } if(c==2) { System.out.println(i); } } } }

Nth Fibonacci Number

  Nth term of Fibonacci series F(n), where F(n) is a function, is calculated using the following formula - F(n) = F(n-1) + F(n-2), Where, F(1) = F(2) = 1 Provided N you have to find out the Nth Fibonacci Number. Sample Input 1: 6 Sample Output 1: 8 import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int c=0; int a =0; int b= 1; if(n==1 || n==2) { System.out.println(1); } else { for(int i=2;i<=n;i++) { c=a+b; a=b; b=c; } System.out.println(c); } } }

Armstrong Number

  Write a Program to determine if the given number is Armstrong number or not. Print true if number is armstrong, otherwise print false. An Armstrong number is a number (with digits n) such that the sum of its digits raised to nth power is equal to the number itself. For example, 371, as 3^3 + 7^3 + 1^3 = 371 1634, as 1^4 + 6^4 + 3^4 + 4^4 = 1634 Sample Input 1 : 103 Sample Output 1 : false import java.util.Scanner; import java.math.*; public class Armstrong { public static void main(String[] args) { Scanner s = new Scanner(System.in); int n=s.nextInt(); int t=n; int num=n; int c=0; // d=0; int sum=0; while(num>0) { c++; num=num/10; } while(n>0) { sum=(int)(sum+(Math.pow(n%10, c))); n=n/10; } if(t==sum) { System.out.println("true"); } else { System.out.println("false"); } } }

Find Duplicate

  You have been given an integer array/list(ARR) of size N which contains numbers from 0 to (N - 2). Each number is present at least once. That is, if N = 5, the array/list constitutes values ranging from 0 to 3 and among these, there is a single integer value that is present twice. You need to find and return that duplicate number present in the array. Sample Input 1: 9 0 7 2 5 4 7 1 3 6 Sample Output 1: 7 public class Solution{ public static int duplicateNumber(int arr[]) { for(int i=0;i<arr.length;i++) { for(int j=0;j<arr.length;j++) { if(i!=j && arr[i]==arr[j]) { return arr[i]; } } } return -1; } }

Find Unique

  You have been given an integer array/list(ARR) of size N. Where N is equal to [2M + 1]. Now, in the given array/list, 'M' numbers are present twice and one number is present only once. You need to find and return that number which is unique in the array/list. Sample Input 1: 7 2 3 1 6 3 6 2 Sample Output 1: 1 Approach 1 - > (Using XOR) package CodingNinjas; import java.util.Scanner; public class FindUnique { public static void main(String[] args) { Scanner s = new Scanner(System.in); int n=s.nextInt(); int a[]=new int[n]; for(int i=0;i<n;i++) { a[i]=s.nextInt(); } int b= findUnique(a); System.out.println(b); } public static int findUnique(int[] arr){ int l=arr.length; int ele=0; for(int i=0;i<l;i++) {                     // if we do XOR of same elements we get 0 i.e 5^5 => 101 ^ 101 = 000 ele^=arr[i]; } return ele; } } Approach 2 -  public static int findUnique(int[] arr){ int l= arr.length; for(int i=0;i&l

Array Intersection

  You have been given two integer arrays/list(ARR1 and ARR2) of size M and N, respectively. You need to print their intersection; An intersection for this problem can be defined when both the arrays/lists contain a particular value or to put it in other words, when there is a common value that exists in both the arrays/lists. Sample Input 1 : 4 2 6 1 2 5 1 2 3 4 2 Sample Output 1 : 2 1 2 public class Solution{ public static void intersections(int arr1[], int arr2[]) { for(int i=0;i<arr1.length;i++) { for(int j=0;j<arr2.length;j++) { if(arr1[i]==arr2[j]) { System.out.print(arr1[i] + " ");                         //values should not get repeated arr2[j]=Integer.MIN_VALUE; break; } } } } }

Pair Sum

  You have been given an integer array/list(ARR) and a number X. Find and return the total number of pairs in the array/list which sum to X. Sample Input 1: 9 1 3 6 2 5 4 3 2 4 7 Sample Output 1: 7 public class Solution { public static int pairSum(int arr[], int x) { int l=arr.length; int s=0; int c=0; for(int i=0;i<l;i++) { for(int j=i+1;j<l;j++) { s=arr[i]+arr[j]; if(s==x) c++; } } return c; } }

Triplet Sum

  You have been given a random integer array/list(ARR) and a number X. Find and return the number of triplets in the array/list which sum to X. Sample Input 1: 7 1 2 3 4 5 6 7 12 Sample Output 1: 5 public class Solution { public static int findTriplet(int[] arr, int x) { int l=arr.length; int s=0; int c=0; for(int i=0;i<l;i++) { for(int j=i+1;j<l;j++) { for(int k=j+1;k<l;k++) { s=arr[i]+arr[j]+arr[k]; if(s==x) { c++; } } } } return c; } }

Sort 0 1

  You have been given an integer array/list(ARR) of size N that contains only integers, 0 and 1. Write a function to sort this array/list. Think of a solution which scans the array/list only once and don't require use of an extra array/list. Sample Input 1: 7 0 1 1 0 1 0 1 Sample Output 1: 0 0 0 1 1 1 1 public class Solution { public static void sortZeroesAndOne(int[] arr) { int l=arr.length; int c=0; for(int i=0;i<l;i++) { if(arr[i]==0) { c++; } } for(int i=0;i<c;i++) { arr[i]=0; } for(int j=c;j<l;j++) { arr[j]=1; } } }

Swap Alternate

  You have been given an array/list(ARR) of size N. You need to swap every pair of alternate elements in the array/list. You don't need to print or return anything, just change in the input array itself. Sample Input 1: 1 6 9 3 6 12 4 32 Sample Output 1 : 3 9 12 6 32 4 public class Solution { public static void swapAlternate(int arr[]) { int n=arr.length; int t=0; for(int i=0;i<n-1;i+=2) { t=arr[i]; arr[i]=arr[i+1]; arr[i+1]=t; } } }

Arrange Numbers In Array

You have been given an empty array(ARR) and its size N. The only input taken from the user will be N and you need not worry about the array. Your task is to populate the array using the integer values in the range 1 to N(both inclusive) in the order - 1,3,.......4,2. Sample Input 1 : 6 Sample Output 1 : 1 3 5 6 4 2 public class Solution { public static void arrange(int[] arr, int n) { int left=0, right=n-1, counter=1; while(left<=right) { if(counter%2==1)                { arr[left++]=counter;           }                else                { arr[right--]=counter;    } counter++; } } }

Linear Search

  You have been given a random integer array/list(ARR) of size N, and an integer X. You need to search for the integer X in the given array/list using 'Linear Search'. Sample Input 1: 7 2 13 4 1 3 6 28 3 Sample Output 1: 4 public class Solution { public static int linearSearch(int arr[], int x) { int l=arr.length; int pos=0; for(int i=0;i<l;i++) { if(arr[i]==x) { pos=i; } } if(pos==0) { return -1; } return pos; } }

Sum of Two Arrays Element by Element

  Two random integer arrays/lists have been given as ARR1 and ARR2 of size N and M respectively. Both the arrays/lists contain numbers from 0 to 9(i.e. single digit integer is present at every index). The idea here is to represent each array/list as an integer in itself of digits N and M. You need to find the sum of both the input arrays/list treating them as two integers and put the result in another array/list i.e. output array/list will also contain only single digit at every index. Sample Input 1: 3 6 2 4 3 7 5 6 Sample Output 1: 1 3 8 0 import java.util.Scanner; public class SumTwoArrays { public static void main(String[] args) { Scanner s = new Scanner(System.in); int n=s.nextInt(); int a[]=new int[n]; for(int i=0;i<n;i++) { a[i]=s.nextInt(); } int m=s.nextInt(); int b[]=new int[m]; for(int i=0;i<m;i++) { b[i]=s.nextInt(); } int[] c = new int[1 + Math.max(a.length, b.length)]; sumOfTwoArrays(a, b, c); print(c); } public static