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

Array Practice Questions CPP

The document contains C++ practice questions and solutions related to array manipulation. Key operations include left and right rotation, finding pivot and peak elements, binary search, moving zeros, merging sorted arrays, and rotating arrays by K. Each function is implemented with code snippets demonstrating the respective operations.

Uploaded by

devvv.2218
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)
6 views3 pages

Array Practice Questions CPP

The document contains C++ practice questions and solutions related to array manipulation. Key operations include left and right rotation, finding pivot and peak elements, binary search, moving zeros, merging sorted arrays, and rotating arrays by K. Each function is implemented with code snippets demonstrating the respective operations.

Uploaded by

devvv.2218
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
You are on page 1/ 3

C++ Array Practice Questions with Solutions

1. Left Rotate by 1

void leftRotateByOne(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;
}

2. Right Rotate by 1

void rightRotateByOne(int arr[], int n) {


int temp = arr[n - 1];
for (int i = n - 1; i > 0; i--)
arr[i] = arr[i - 1];
arr[0] = temp;
}

3. Pivot Element

int findPivot(int arr[], int n) {


int total = 0, leftSum = 0;
for (int i = 0; i < n; i++) total += arr[i];

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


if (leftSum == total - leftSum - arr[i]) return i;
leftSum += arr[i];
}

return -1;
}

4. Peak Element

int findPeak(int arr[], int n) {


for (int i = 1; i < n - 1; i++) {
if (arr[i] > arr[i - 1] && arr[i] > arr[i + 1])
return i;
}
return -1;
}
C++ Array Practice Questions with Solutions

5. Binary Search

int binarySearch(int arr[], int n, int key) {


int low = 0, high = n - 1;

while (low <= high) {


int mid = (low + high) / 2;

if (arr[mid] == key)
return mid;
else if (arr[mid] < key)
low = mid + 1;
else
high = mid - 1;
}

return -1;
}

6. Move All 0s to Left

void moveZerosToLeft(int arr[], int n) {


int j = n - 1;
for (int i = n - 1; i >= 0; i--) {
if (arr[i] != 0)
arr[j--] = arr[i];
}
while (j >= 0)
arr[j--] = 0;
}

7. Move All 0s to Right

void moveZerosToRight(int arr[], int n) {


int j = 0;
for (int i = 0; i < n; i++) {
if (arr[i] != 0)
arr[j++] = arr[i];
}
while (j < n)
arr[j++] = 0;
}
C++ Array Practice Questions with Solutions

8. Merge Two Sorted Arrays

void mergeSortedArrays(int a[], int n, int b[], int m, int merged[]) {


int i = 0, j = 0, k = 0;
while (i < n && j < m) {
if (a[i] < b[j])
merged[k++] = a[i++];
else
merged[k++] = b[j++];
}
while (i < n)
merged[k++] = a[i++];
while (j < m)
merged[k++] = b[j++];
}

9. Rotate Array by K (Right Rotation)

void reverse(int arr[], int start, int end) {


while (start < end) {
swap(arr[start], arr[end]);
start++;
end--;
}
}

void rotateRight(int arr[], int n, int k) {


k = k % n;
reverse(arr, 0, n - 1);
reverse(arr, 0, k - 1);
reverse(arr, k, n - 1);
}

You might also like