Recursion | Linear Search | Find all indices | Lecture 33 | Java and DSA Course

 J     A         A              C     O     D      E

           1. Write a program to check  (x)  is exist or not in Array           

      </>    RecArrayExistenceCheck.java       

  1. // METHOD (1) :
  2. // FINDING EXISTENCE OF AN ELEMENT :

  3. package Recursion;
  4. import java.util.Scanner;

  5. public class RecArrayExistenceCheck {
  6. public static void check(int []arr,int n,int x){
  7. if(arr[n]==x){
  8. System.out.println("YES");
  9. return ;
  10. }
  11. if(n==0){
  12. System.out.println("NO");
  13. return ;
  14. }
  15. check(arr,n-1,x);
  16. }
  17. public static void main(String[] args) {
  18. Scanner sc=new Scanner(System.in);
  19. System.out.println("Enter number of elements: ");
  20. int n=sc.nextInt();
  21. int []arr=new int[n];
  22. for(int i=0;i<n;i++)
  23. arr[i]=sc.nextInt();
  24. System.out.println("Enter which element you want to check :");
  25. int x=sc.nextInt();

  26. check(arr,n-1,x);

  27. sc.close();
  28. }
  29. }

           2. Write a program to check  (x)  is exist or not in Array           

      </>    RecArrayExistByFreq.java       

  1. // METHOD (2) :
  2. // CHECKING THE EXISTENCE OF AN ELEMENT IN AN ARRAY :
  3. // WE DONE IT BY THE CONCEPT OF FREQUENCY ARRAY :

  4. package Recursion;
  5. import java.util.Scanner;
  6. public class RecArrayExistByFreq {
  7. public static void checkFreq(int []arr,int n,int x,int []freq){
  8. freq[arr[n]]++;
  9. if(n==0){
  10. if(freq[x]!=0) System.out.println("YES");
  11. else System.out.println("NO");
  12. return ;
  13. }
  14. checkFreq(arr,n-1,x,freq);
  15. }
  16. public static void main(String[] args) {
  17. Scanner sc=new Scanner(System.in);
  18. System.out.println("Enter number of elements in array: ");
  19. int n=sc.nextInt();
  20. int []arr=new int[n];
  21. for(int i=0;i<n;i++)
  22. arr[i]=sc.nextInt();
  23. System.out.println("Enter which element you want to check: ");
  24. int x=sc.nextInt();
  25. int []freq=new int [100005];
  26. checkFreq(arr,n-1,x,freq);


  27. sc.close();
  28. }
  29. }

           3. Write a program to print at which index number that element is present          

      </>    RecArrayExistIndex.java       

  1. // FINDING AT WHICH INDEX THAT ELEMENT EXIST :

  2. package Recursion;
  3. import java.util.Scanner;
  4. public class RecArrayExistIndex {
  5. public static void check(int []arr,int n,int x,int key){

  6. if(arr[n]==x){
  7. System.out.println(x+" present at index : "+n);
  8. key=1;
  9. }
  10. if(n==0){
  11. if(key==0) System.out.println(-1);
  12. return ;
  13. }
  14. check(arr,n-1,x,key);
  15. }
  16. public static void main(String[] args) {
  17. Scanner sc = new Scanner(System.in);
  18. System.out.println("Enter no. of element : ");
  19. int n=sc.nextInt();
  20. int []arr=new int[n];
  21. for(int i=0;i<n;i++)
  22. arr[i]=sc.nextInt();
  23. System.out.println("Enter which element you want to check: ");
  24. int x=sc.nextInt();
  25. check(arr,n-1,x,0);

  26. sc.close();
  27. }
  28. }

        4. Same as above Question BUT , In this the return type is ArrayList          

      </>    RecArrayListExistence.java       

  1. // SAME AS ABOVE QUESTION :
  2. // NOW THE RETURN TYPE IS ARRAY LIST :

  3. package Recursion;
  4. import java.util.Scanner;
  5. import java.util.ArrayList;

  6. public class RecArrayListExistence {
  7. public static ArrayList<Integer> checking(int []arr,int n,int x,int key,ArrayList<Integer>lst){
  8. if(arr[n]==x){
  9. lst.add(n);
  10. key=1;
  11. }
  12. if(n==0){
  13. if(key==0) System.out.println("NONE");
  14. return lst;
  15. }
  16. return checking(arr,n-1,x,key,lst);
  17. }
  18. public static void main(String[] args) {
  19. Scanner sc=new Scanner(System.in);

  20. System.out.println("Enter no. of elements: ");
  21. int n=sc.nextInt();
  22. int []arr=new int[n];
  23. for(int i=0;i<n;i++) arr[i]=sc.nextInt();

  24. System.out.println("Enter that element that you want to check: ");
  25. int x=sc.nextInt();

  26. ArrayList<Integer>lst=new ArrayList<>();
  27. ArrayList<Integer>ans=checking(arr,n-1,x,0,lst);

  28. System.out.println(ans);

  29. sc.close();
  30. }
  31. }

        5. Write a program to check weather the Array is sorted or not           

      </>    RecArrayCheckSort.java       

  1. // CHECKING ARRAY IS SORTED OR NOT :

  2. package Recursion;
  3. import java.util.Scanner;

  4. public class RecArrayCheckSort {
  5. public static void checkSort(int []arr,int n){
  6. if(arr[n]<arr[n+1]){
  7. if(n==0){
  8. System.out.println("SORTED");
  9. return ;
  10. }
  11. }
  12. else{
  13. System.out.println("NOT SORTED");
  14. return ;
  15. }
  16. checkSort(arr,n-1);

  17. }
  18. public static void main(String[] args) {
  19. Scanner sc=new Scanner(System.in);
  20. System.out.println("Enter no. of elements: ");
  21. int n=sc.nextInt();
  22. int []arr=new int[n];
  23. for(int i=0;i<n;i++) arr[i]=sc.nextInt();
  24. checkSort(arr,n-2);

  25. sc.close();
  26. }
  27. }

        6. Write a program to check weather the element is exist or not           

      </>    RecArrayTarget.java       

  1. // CHECKING EXISTENCE OF AN ELEMENT IN AN ARRAY :

  2. package Recursion;
  3. import java.util.Scanner;

  4. public class RecArrayTarget {
  5. public static void check(int []arr,int n,int x){
  6. if(n==0){
  7. System.out.println("ELEMENT NOT EXIST");
  8. return ;
  9. }
  10. if(arr[n]==x){
  11. System.out.println("ELEMENT EXIST AT LAST INDEX : "+n);
  12. return ;
  13. }
  14. check(arr,n-1,x);
  15. }
  16. public static void main(String[] args) {
  17. Scanner sc=new Scanner(System.in);
  18. System.out.println("Enter number of elements: ");
  19. int n=sc.nextInt();
  20. int []arr=new int[n];
  21. for(int i=0;i<n;i++) arr[i]=sc.nextInt();
  22. System.out.println("Enter element that you want to check: ");
  23. int x=sc.nextInt();
  24. check(arr,n-1,x);

  25. sc.close();
  26. }
  27. }

 SEE YOU  AGAIN  !!

Comments

Popular posts from this blog

Recursion | GCD | Euclids Algorithm | Lecture 31 | Java & DSA Course

S O M E | B A S I C | M E T H O D S | O F | S t r i n g | I N | J A V A :