top of page

coding

  • Writer: Anand Nerurkar
    Anand Nerurkar
  • 4 days ago
  • 2 min read

segreage 0 and 1 in array


import java.util.Arrays;


public class segragate01 {

public class Segregate0And1 {

public static void segregate0and1(int[] arr) {

int lo = 0;

int hi = arr.length - 1;


while (lo < hi) {

if (arr[lo] == 1) {

if (arr[hi] != 1) {

int temp = arr[lo];

arr[lo] = arr[hi];

arr[hi] = temp;

lo++;

hi--;

} else {

hi--;

}

} else {

lo++;

}

}

}


public static void main(String[] args) {

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


segregate0and1(arr);


System.out.print("Array after segregation is ");

System.out.println(Arrays.toString(arr));

}

}

}


segregate even and odd no in array

===

import java.util.Arrays;


public class EvenOddSegregator {

public static void segregateEvenOdd(int[] arr) {

int left = 0, right = arr.length - 1;


while (left < right) {

// Move left forward if current is even

while (arr[left] % 2 == 0 && left < right) {

left++;

}


// Move right backward if current is odd

while (arr[right] % 2 == 1 && left < right) {

right--;

}


// Swap if left is odd and right is even

if (left < right) {

int temp = arr[left];

arr[left] = arr[right];

arr[right] = temp;

left++;

right--;

}

}

}


public static void main(String[] args) {

int[] arr = {12, 17, 70, 15, 22, 65, 21, 90};

System.out.println("Original Array: " + Arrays.toString(arr));


segregateEvenOdd(arr);


System.out.println("Segregated Array: " + Arrays.toString(arr));

}

}




right rotate/left rotate array by k size

===





let us roate it by one as below.


static void rotatebyk(int a[],int k){

k=k%a.length;

if(k<0){

k=k+a.length;

}


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

rotate(a);

}


}


public static void main(String arg[]){

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


rotatebyk(a,2);

}


}


2--- more efficnet way is

rotateby k

===

divide array into 2 part

1--- 0-----k-1


2 --- k ---a.lenth


then reverse 1st part, 2nd part then reverse wholw part


below is the code


static void reverse(int a[],int start, int end){

while(start < end){

int temp=a[start];

a-start]=a[end];

a[end]=temp;

start++;

end--;


}

}


statci void rotatebyk(int a[],int k){

k=k%a.length;

if(k < o) k=k+a.length;

reverse(a,o,k-1);

reverse(a.k,a.length-1);

reverse(a,0,a.lenght-1);

}


public static void main(String arg[]){

int a[]={1,2,3,4,5|;

rotatebyk (a,3);

}



move all zero to end

===





find subarray if array

===

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


subaaray is





find maz continous subarray sum

----



class sumaaary{

public static void main(String arg[]){

}

static int getMaxSubarraySum(int a[]){

int sum=a[0];

int maxSum=a[0];


for(int i=1;i<a.length;i++){

if (sum>=0){

sum+=a[i];

}else{

sum=a[i];

}

if(sum >maxsum){

maxSum=sum;

}

}

return maxSum;

}

}



maxsubaaray sum with window size k

====




how to remove duplicate from array

===




is array sorted

===




min platform needed

==

arrival[] = {900, 940, 950, 1100, 1500, 1800}

departure[] = {910, 1200, 1120, 1130, 1900, 2000}


import java.util.Arrays;


public class PlatformCounter {

public static int findMinimumPlatforms(int[] arrival, int[] departure) {

Arrays.sort(arrival);

Arrays.sort(departure);


int n = arrival.length;

int platformNeeded = 1, maxPlatforms = 1;


int i = 1, j = 0;


while (i < n && j < n) {

if (arrival[i] <= departure[j]) {

platformNeeded++;

i++;

} else {

platformNeeded--;

j++;

}

maxPlatforms = Math.max(maxPlatforms, platformNeeded);

}


return maxPlatforms;

}


public static void main(String[] args) {

int[] arrival = {900, 940, 950, 1100, 1500, 1800};

int[] departure = {910, 1200, 1120, 1130, 1900, 2000};


System.out.println("Minimum number of platforms required: " +

findMinimumPlatforms(arrival, departure));

}

}

 
 
 

Recent Posts

See All
Ops Efficiency 30 % improvement

how did you achieve 30 % operational efficiency Achieving 30% operational efficiency in a BFSI-grade, microservices-based personal...

 
 
 
Mutual Fund Architecture Q & A

🔹 Q1: How did you design for 100K+ concurrent users and 5K+ TPS? Structured Answer: To support 100K+ concurrent users and 5K+ TPS, I...

 
 
 

Comments

Rated 0 out of 5 stars.
No ratings yet

Add a rating
  • Facebook
  • Twitter
  • LinkedIn

©2024 by AeeroTech. Proudly created with Wix.com

bottom of page