[Go to site: main page, start]

0% found this document useful (0 votes)
2 views3 pages

Array Manipulation Techniques 24-37

Uploaded by

cglprep9
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)
2 views3 pages

Array Manipulation Techniques 24-37

Uploaded by

cglprep9
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

Array Programs Part 3 (24–37)

------------------------------

// 24. Check if an array is sorted or not


class CheckSorted {
static boolean isSorted(int[] arr) {
for (int i = 1; i < [Link]; i++) {
if (arr[i] < arr[i - 1]) return false;
}
return true;
}
}

// 25. Common elements in two arrays


class CommonElements {
static void printCommon(int[] a, int[] b) {
for (int i = 0; i < [Link]; i++) {
for (int j = 0; j < [Link]; j++) {
if (a[i] == b[j]) [Link](a[i] + " ");
}
}
}
}

// 26. Left shift all elements


class LeftShift {
static void shiftLeft(int[] arr) {
int first = arr[0];
for (int i = 0; i < [Link] - 1; i++) arr[i] = arr[i + 1];
arr[[Link] - 1] = first;
}
}

// 27. Right shift all elements


class RightShift {
static void shiftRight(int[] arr) {
int last = arr[[Link] - 1];
for (int i = [Link] - 1; i > 0; i--) arr[i] = arr[i - 1];
arr[0] = last;
}
}

// 28. 1st half reverse, 2nd half normal


class HalfReverse {
static void process(int[] arr) {
int mid = [Link] / 2;
for (int i = mid - 1; i >= 0; i--) [Link](arr[i] + " ");
for (int i = mid; i < [Link]; i++) [Link](arr[i] + " ");
}
}

// 29. Left shift 1st half only


class LeftShiftHalf {
static void leftShiftHalf(int[] arr) {
int mid = [Link] / 2;
int first = arr[0];
for (int i = 0; i < mid - 1; i++) arr[i] = arr[i + 1];
arr[mid - 1] = first;
}
}

// 30. Right shift 2nd half only


class RightShiftHalf {
static void rightShiftHalf(int[] arr) {
int mid = [Link] / 2;
int last = arr[[Link] - 1];
for (int i = [Link] - 1; i > mid; i--) arr[i] = arr[i - 1];
arr[mid] = last;
}
}

// 31. Frequency of all elements


class FrequencyAll {
static void countFreq(int[] arr) {
boolean[] visited = new boolean[[Link]];
for (int i = 0; i < [Link]; i++) {
if (visited[i]) continue;
int count = 1;
for (int j = i + 1; j < [Link]; j++) {
if (arr[i] == arr[j]) {
count++;
visited[j] = true;
}
}
[Link](arr[i] + " -> " + count);
}
}
}

// 32. Count primes in array


class PrimeInArray {
static boolean isPrime(int n) {
if (n < 2) return false;
for (int i = 2; i <= n / 2; i++) if (n % i == 0) return false;
return true;
}
static int countPrime(int[] arr) {
int count = 0;
for (int n : arr) if (isPrime(n)) count++;
return count;
}
}

// 33. Nth largest element


import [Link];
class NthLargest {
static int findNthLargest(int[] arr, int n) {
[Link](arr);
return arr[[Link] - n];
}
}

// 34. Unique elements


class UniqueElements {
static void printUnique(int[] arr) {
for (int i = 0; i < [Link]; i++) {
boolean unique = true;
for (int j = 0; j < [Link]; j++) {
if (i != j && arr[i] == arr[j]) unique = false;
}
if (unique) [Link](arr[i] + " ");
}
}
}

// 35. Duplicate elements


class DuplicateElements {
static void printDuplicates(int[] arr) {
for (int i = 0; i < [Link]; i++) {
for (int j = i + 1; j < [Link]; j++) {
if (arr[i] == arr[j]) {
[Link](arr[i] + " ");
break;
}
}
}
}
}

// 36. Element repeated highest times


class MaxRepeated {
static int mostFrequent(int[] arr) {
int maxCount = 0, maxElement = arr[0];
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 > maxCount) { maxCount = count; maxElement = arr[i]; }
}
return maxElement;
}
}

// 37. Shift all zeros to end


class ShiftZeros {
static void shiftZeros(int[] arr) {
int index = 0;
for (int i = 0; i < [Link]; i++) if (arr[i] != 0) arr[index++] = arr[i];
while (index < [Link]) arr[index++] = 0;
}
}

You might also like