0% found this document useful (0 votes)
1 views13 pages

DSA_notes

The document contains multiple Java classes with main methods that demonstrate various algorithms and operations on arrays, such as finding floor and ceiling values, counting occurrences, identifying unique elements, and rotating arrays. Each class provides a different functionality, including searching for first and last occurrences of a number, counting duplicates, and checking if an array is sorted. The code snippets illustrate fundamental programming concepts and array manipulations in Java.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views13 pages

DSA_notes

The document contains multiple Java classes with main methods that demonstrate various algorithms and operations on arrays, such as finding floor and ceiling values, counting occurrences, identifying unique elements, and rotating arrays. Each class provides a different functionality, including searching for first and last occurrences of a number, counting duplicates, and checking if an array is sorted. The code snippets illustrate fundamental programming concepts and array manipulations in Java.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

import [Link].

*;

class Main {

public static void main(String[] args) {

int []nums ={3, 4, 4, 7, 8, 10};

int x= 5;

public static int[] floorandceil(int[] nums,int x){

int floor=-1;

int ceil=-1;

for(int i=0;i<[Link];i++){

if(nums[i]<=x){

floor=nums[i];

if(nums[i]>=x){

ceil=nums[i];

break;

return new int[]{floor,ceil};

}
class Main{

public static void main(String[] args){

int[] arr={5, 7, 7, 8, 8, 10};

int x=8;

int firstoccurence=-1;

int lastoccurence=-1;

for(int i=0;i<[Link];i++){

if(arr[i]==x){

firstoccurence=i;

break;

for(int i=[Link]-1;i>=0;i--){

if(arr[i]==x){

lastoccurence=i;

break;

[Link](firstoccurence+","+lastoccurence);

}
class Main{

public static void main(String[] args){

int[] arr={0, 0, 1, 1, 1, 2, 3};

int target=1;

int count=0;

for(int i=0;i<[Link];i++){

if(arr[i]==target){

count=count+1;

[Link](count);

}
class Main {

public static void main(String[] args) {

int[] arr = {1, 1, 2, 2, 3, 3, 4, 5, 5, 6, 6};

for (int i = 0; i < [Link]; i++) {

int count = 0;

for (int j = 0; j < [Link]; j++) {

if (arr[i] == arr[j]) {

count++;

if (count == 1) {

[Link](arr[i]);

break;

}
class Main{

public static void main(String[] args){

int[] arr={4, 5, 6, 7, 0, 1, 2, 3};

boolean found=false;

for(int i=0;i<[Link]-1;i++){

if(arr[i]>arr[i+1]){

[Link](i+1);

found=true;

break;

if(!found){

[Link](0);

}
class Main {

public static void main(String[] args) {

int[] arr = {1, 2, 1, 3, 5, 6, 4};

int n = [Link];

// Case 1: Only one element

if (n == 1) {

[Link](0);

return;

// Case 2: First element is a peak

if (arr[0] > arr[1]) {

[Link](0);

return;

// Case 3: Middle elements

for (int i = 1; i < n - 1; i++) {

if (arr[i] > arr[i - 1] && arr[i] > arr[i + 1]) {

[Link](i);

return;

}
// Case 4: Last element is a peak

if (arr[n - 1] > arr[n - 2]) {

[Link](n - 1);

// Online Java Compiler

// Use this editor to write, compile and run your Java code online

class Main {

public static void main(String[] args) {

int[] arr={8, 8, 7, 6, 5};

[Link](secondlargest(arr));

public static int secondlargest(int[] arr){

int largest=arr[0];

for(int i=1;i<[Link];i++){

if(arr[i]>largest){

largest=arr[i];

}
}

int secondlargest=Integer.MIN_VALUE;

for(int i=0;i<[Link];i++){

if(arr[i]>secondlargest && arr[i]<largest){

secondlargest=arr[i];

return secondlargest;

import [Link];

class Main{

public static void main(String[] args){

ArrayList<Integer>nums=new ArrayList<>();

[Link](1);

[Link](2);

[Link](3);

[Link](4);
[Link](5);

[Link](issorted(nums));

public static boolean issorted(ArrayList<Integer> nums){

boolean issorted=true;

for(int i=0;i<[Link]()-1;i++){

if([Link](i)<=[Link](i+1)){

issorted=true;

else{

issorted=false;

break;

if(issorted){

return true;

return false;

}
\

class Main{

public static void main(String[] args){

int[] arr={0, 0, 3, 3, 5, 6};

int k = removeDuplicates(arr);

[Link]("Number of unique elements: " + k);

[Link]("Array after removing duplicates: ");

for (int i = 0; i < k; i++) {

[Link](arr[i] + " ");

}
}

public static int removeDuplicates(int[] arr){

if([Link]==0){

return 0;

int writer=0;

for(int i=1;i<[Link];i++){

if(arr[writer]!=arr[i]){

writer++;

arr[writer]=arr[i];

return writer+1;

}
OUR APPROACH:

import [Link].*;

class Main{

public static void main(String[] args){

int[] arr={1, 2, 3, 4, 5};

for(int i=0;i<[Link]-1;i++){

int temp=arr[i];

arr[i]=arr[i+1];

arr[i+1]=temp;

[Link]([Link](arr));

STANDARD APPROACH:

import [Link].*;

class Main{

public static void main(String[] args){

int[] arr={1, 2, 3, 4, 5};

int length=[Link];

int temp=arr[0];

for(int i=0;i<[Link]-1;i++){

arr[i]=arr[i+1];

arr[length-1]=temp;

[Link]([Link](arr));

}
import [Link].*;

class Main{

public static void main(String[] args){

int[] arr={1, 2, 3, 4, 5};

int start=0;

int k=3;

rotate(arr,start,k-1);

rotate(arr,k,[Link]-1);

rotate(arr,start,[Link]-1);

[Link]([Link](arr));

public static int[] rotate(int[] arr,int start,int k){

while(start<k){

int temp=arr[start];

arr[start]=arr[k];

arr[k]=temp;

start++;

k--;

return arr;

You might also like