0% found this document useful (0 votes)
15 views15 pages

Array Manipulation Techniques in Java

Uploaded by

rohitraj.18031
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views15 pages

Array Manipulation Techniques in Java

Uploaded by

rohitraj.18031
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

***largest element in an arr

static int findLargestElement(int arr[]) {

int max= arr[0];

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


if (arr[i] > max) {

max= arr[i];

return max;

***2nd largest

static private int secondLargest(int[] arr, int n)


{

if(n<2)

return -1;

int large = Integer.MIN_VALUE;

int second_large = Integer.MIN_VALUE;

int i;

for (i = 0; i < n; i++)

if (arr[i] > large)

second_large = large;

large = arr[i];

else if (arr[i] > second_large && arr[i] != large)

{
second_large = arr[i];

return second_large;
}

***Check if array is sorted

static boolean isSorted(int arr[], int n) {

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

if (arr[i] < arr[i - 1])

return false;

return true;

***Left rotate by 1

static void solve(int arr[], int n) {

int temp = arr[0];

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

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

arr[n - 1] = temp;

***Rotate using reverse

public static void Reverse(int[] arr, int start, int end) {


while (start < end) {

int temp = arr[start];

arr[start] = arr[end];

arr[end] = temp;
start++;

end--;

// Function to rotate k elements to the right

public static void RotateRight(int[] arr, int n, int k) {

k = k % n; // Handle cases where k is larger than the array size

if (k < 0) k += n; // Handle negative k values


// Reverse first n-k elements

Reverse(arr, 0, n - 1);

// Reverse last k elements

Reverse(arr, 0, k - 1);

// Reverse whole array

Reverse(arr, k, n - 1);

// Function to rotate k elements to the left

public static void RotateLeft(int[] arr, int n, int k) {

k = k % n; // Handle cases where k is larger than the array size

if (k < 0) k += n; // Handle negative k values

// Reverse first k elements

Reverse(arr, 0, k - 1);

// Reverse remaining n-k elements

Reverse(arr, k, n - 1);
// Reverse whole array

Reverse(arr, 0, n - 1);

// Utility function to print the array

public static void PrintArray(int[] arr) {

for (int num : arr) {

[Link](num + " ");

[Link]();

***Move all zeros to left/right


Left

public static int[] moveZeros(int n, int []a) {

int j = -1;

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

if (a[i] == 0) {

j++;

//swap a[i] & a[j]:

int tmp = a[i];

a[i] = a[j];

a[j] = tmp;

return a;

}
Right

public static int[] moveZeros(int n, int []a) {

int j = -1;
//place the pointer j:

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

if (a[i] == 0) {

j = i;

break;

//no non-zero elements:


if (j == -1) return a;

//Move the pointers i and j

//and swap accordingly:

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

if (a[i] != 0) {

//swap a[i] & a[j]:

int tmp = a[i];

a[i] = a[j];

a[j] = tmp;

j++;

return a;

}
***Binary array sorting

public static int[] sorting(int n, int []a) {

int j = -1;

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


if (a[i] == 0) {

j++;

//swap a[i] & a[j]:

int tmp = a[i];

a[i] = a[j];

a[j] = tmp;

return a;
}

***Arrays of 0s,1s,2s

public static void sortArray(ArrayList<Integer> arr, int n) {

int cnt0 = 0, cnt1 = 0, cnt2 = 0;

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

if ([Link](i) == 0) cnt0++;

else if ([Link](i) == 1) cnt1++;

else cnt2++;

//Replace the places in the original array:

for (int i = 0; i < cnt0; i++) [Link](i, 0); // replacing 0's

for (int i = cnt0; i < cnt0 + cnt1; i++) [Link](i, 1); // replacing 1's
for (int i = cnt0 + cnt1; i < n; i++) [Link](i, 2); // replacing 2's

public static void sortArray(ArrayList<Integer> arr, int n) {

int low = 0, mid = 0, high = n - 1; // 3 pointers

while (mid <= high) {

if ([Link](mid) == 0) {

// swapping arr[low] and arr[mid]

int temp = [Link](low);

[Link](low, [Link](mid));
[Link](mid, temp);

low++;

mid++;

} else if ([Link](mid) == 1) {

mid++;

} else {

// swapping arr[mid] and arr[high]

int temp = [Link](mid);

[Link](mid, [Link](high));

[Link](high, temp);

high--;

}
}

***Move all -ve


public static int[] moveZeros(int n, int []a) {

int j = -1;

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

if (a[i] < 0) {

j++;

//swap a[i] & a[j]:

int tmp = a[i];

a[i] = a[j];

a[j] = tmp;
}

return a;

***Remove duplicates

class Solution {

public int removeDuplicates(int[] nums) {

int j=0;

int n=[Link];

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

if(nums[i]!=nums[j]){

j++;

int temp=nums[i];

nums[i]=nums[j];

nums[j]=temp;
}

return j+1;
}

***Union and intersection of sorted array

UNION

static ArrayList<Integer> FindUnion(int arr1[], int arr2[], int n, int m) {

int i = 0, j = 0; // pointers

ArrayList<Integer > Union=new ArrayList<>(); // Uninon vector


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

if (arr1[i] <= arr2[j]) // Case 1 and 2

if ([Link]() == 0 || [Link]([Link]()-1) != arr1[i])

[Link](arr1[i]);

i++;

} else // case 3

if ([Link]() == 0 || [Link]([Link]()-1) != arr2[j])

[Link](arr2[j]);

j++;

while (i < n) // IF any element left in arr1

if ([Link]([Link]()-1) != arr1[i])
[Link](arr1[i]);

i++;

while (j < m) // If any elements left in arr2


{

if ([Link]([Link]()-1) != arr2[j])

[Link](arr2[j]);

j++;

return Union;

INTERSECTION

public static List<Integer> findIntersection(int[] arr1, int[] arr2) {

List<Integer> intersection = new ArrayList<>();

int i = 0, j = 0;

while (i < [Link] && j < [Link]) {

if (arr1[i] == arr2[j]) {

[Link](arr1[i]);

i++;

j++;

} else if (arr1[i] < arr2[j]) {

i++;

} else {

j++;

}
return intersection;

***2 sum

public static String twoSum(int n, int []arr, int target) {

[Link](arr);

int left = 0, right = n - 1;

while (left < right) {

int sum = arr[left] + arr[right];

if (sum == target) {

return "YES";
} else if (sum < target) left++;

else right--;

return "NO";

public static int[] twoSum(int n, int []arr, int target) {

int[] ans = new int[2];

ans[0] = ans[1] = -1;

HashMap<Integer, Integer> mpp = new HashMap<>();

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

int num = arr[i];

int moreNeeded = target - num;

if ([Link](moreNeeded)) {

ans[0] = [Link](moreNeeded);
ans[1] = i;

return ans;

[Link](arr[i], i);

return ans;

*** Missing element

public static int missingNumber(int []a, int N) {

int xor1 = 0, xor2 = 0;

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

xor2 = xor2 ^ a[i]; // XOR of array elements

xor1 = xor1 ^ (i + 1); //XOR up to [1...N-1]

xor1 = xor1 ^ N; //XOR up to [1...N]

return (xor1 ^ xor2); // the missing number

***Max consecutive one

static int findMaxConsecutiveOnes(int nums[]) {

int cnt = 0;

int maxi = 0;

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

if (nums[i] == 1) {
cnt++;

} else {

cnt = 0;

maxi = [Link](maxi, cnt);

return maxi;

***Apearing once in a twice array

public static int getSingleElement(int []arr) {

//size of the array:


int n = [Link];

// XOR all the elements:

int xorr = 0;

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

xorr = xorr ^ arr[i];

return xorr;

***Best time to buy or sell stock

static int maxProfit(int[] arr) {

int maxPro = 0;

int minPrice = Integer.MAX_VALUE;

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


minPrice = [Link](minPrice, arr[i]);

maxPro = [Link](maxPro, arr[i] - minPrice);

return maxPro;
}

***Max Subarray Sum

public static long maxSubarraySum(int[] arr, int n) {

long maxi = Long.MIN_VALUE; // maximum sum

long sum = 0;

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

sum += arr[i];

if (sum > maxi) {

maxi = sum;

// If sum < 0: discard the sum calculated

if (sum < 0) {

sum = 0;

***Max subarray product

static int maxProductSubArray(int arr[]) {


int prod1 = arr[0],prod2 = arr[0],result = arr[0];

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

int temp = [Link](arr[i],[Link](prod1*arr[i],prod2*arr[i]));


prod2 =. [Link](arr[i],[Link](prod1*arr[i],prod2*arr[i]));

prod1 = temp;

result = [Link](result,prod1);

return result;

You might also like