MCPcopy Create free account
hub / github.com/Manvityagi/PW-Skills-Java-Course-Codes / Questions

Class Questions

Lecture 15 Arrays/src/Questions.java:4–66  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

2import java.util.Scanner;
3
4public class Questions {
5 static int[] smallestAndLargestElement(int[] arr){ //1, 2, 3, 4, 5
6 Arrays.sort(arr);
7 int[] ans = {arr[0], arr[arr.length-1]};
8 return ans;
9 }
10
11 static boolean isSorted(int[] arr){
12 boolean check = true;
13 for(int i = 1; i < arr.length; i++){
14 if(arr[i] < arr[i-1]){
15 //not sorted
16 check = false;
17 break;
18 }
19 }
20 return check;
21 }
22
23 static int lastOccurrence(int[] arr, int x){
24 int lastIndex = -1;
25 for(int i = 0; i < arr.length; i++){
26 if(arr[i] == x){
27 lastIndex = i;
28 }
29 }
30 return lastIndex;
31 }
32 static int countOccurrences(int[] arr, int x){
33 int count = 0;
34 for(int i = 0; i < arr.length; i++){
35 if(arr[i] == x){
36 count++;
37 }
38 }
39 return count;
40 }
41
42 static int a;
43
44 public static void main(String[] args) {
45 System.out.println(a);
46 Scanner sc = new Scanner(System.in);
47 System.out.println("Enter size of array: ");
48 int n = sc.nextInt();
49
50 int[] arr = new int[n];
51 System.out.println("Enter " + n + " elements: ");
52 for(int i = 0; i < arr.length; i++){
53 arr[i] = sc.nextInt();
54 }
55
56// System.out.println("Enter x");
57// int x = sc.nextInt();
58
59// System.out.println("COUNT OF X: " + countOccurrences(arr, x));
60// System.out.println("LAST OCCURRENCE OF X: " + lastOccurrence(arr, x));
61// System.out.println("Is Sorted: " + isSorted(arr));

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected