[go: up one dir, main page]

0% found this document useful (0 votes)
3 views4 pages

Sample Question

The document contains C++ examples demonstrating various algorithms including candy distribution based on ratings, prime number checks, sorting algorithms like bubble sort and insertion sort, and searching algorithms such as linear and binary search. Each example is accompanied by code snippets and explanations of their functionality. The content serves as a practical guide for understanding and implementing these algorithms in C++.

Uploaded by

fsbking22
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)
3 views4 pages

Sample Question

The document contains C++ examples demonstrating various algorithms including candy distribution based on ratings, prime number checks, sorting algorithms like bubble sort and insertion sort, and searching algorithms such as linear and binary search. Each example is accompanied by code snippets and explanations of their functionality. The content serves as a practical guide for understanding and implementing these algorithms in C++.

Uploaded by

fsbking22
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/ 4

C++ Solved Examples

Candy Distribution Problem

Example 1: Minimum candies based on ratings

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int minCandies(vector<int>& ratings) {


int n = ratings.size();
vector<int> candies(n, 1);

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


if (ratings[i] > ratings[i - 1])
candies[i] = candies[i - 1] + 1;

for (int i = n - 2; i >= 0; --i)


if (ratings[i] > ratings[i + 1])
candies[i] = max(candies[i], candies[i + 1] + 1);

int total = 0;
for (int c : candies)
total += c;

return total;
}

int main() {
vector<int> ratings = {1, 0, 2};
cout << "Minimum candies required: " << minCandies(ratings) << endl;
return 0;
}

Example 2: All ratings equal

#include <iostream>
#include <vector>
using namespace std;

int main() {
vector<int> ratings = {3, 3, 3};
int n = ratings.size();
vector<int> candies(n, 1);

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


if (ratings[i] > ratings[i - 1])
candies[i] = candies[i - 1] + 1;
for (int i = n - 2; i >= 0; --i)
if (ratings[i] > ratings[i + 1])
candies[i] = max(candies[i], candies[i + 1] + 1);

int total = 0;
for (int c : candies)
total += c;

cout << "Minimum candies: " << total << endl;


return 0;
}

Prime Numbers

Example 1: Check if a number is prime

#include <iostream>
#include <cmath>
using namespace std;

bool isPrime(int n) {
if (n <= 1) return false;
for (int i = 2; i <= sqrt(n); ++i)
if (n % i == 0) return false;
return true;
}

int main() {
int num = 29;
cout << num << (isPrime(num) ? " is Prime" : " is not Prime") << endl;
return 0;
}

Example 2: Print prime numbers up to 50

#include <iostream>
#include <cmath>
using namespace std;

bool isPrime(int n) {
if (n <= 1) return false;
for (int i = 2; i <= sqrt(n); ++i)
if (n % i == 0) return false;
return true;
}

int main() {
for (int i = 2; i <= 50; ++i)
if (isPrime(i)) cout << i << " ";
return 0;
}
Sorting

Example 1: Bubble Sort

#include <iostream>
using namespace std;

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


for (int i = 0; i < n - 1; ++i)
for (int j = 0; j < n - i - 1; ++j)
if (arr[j] > arr[j + 1])
swap(arr[j], arr[j + 1]);
}

int main() {
int arr[] = {5, 1, 4, 2, 8};
int n = sizeof(arr) / sizeof(arr[0]);
bubbleSort(arr, n);
for (int i = 0; i < n; ++i)
cout << arr[i] << " ";
return 0;
}

Example 2: Insertion Sort

#include <iostream>
using namespace std;

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


for (int i = 1; i < n; ++i) {
int key = arr[i];
int j = i - 1;
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j--;
}
arr[j + 1] = key;
}
}

int main() {
int arr[] = {9, 5, 1, 4, 3};
int n = sizeof(arr) / sizeof(arr[0]);
insertionSort(arr, n);
for (int i = 0; i < n; ++i)
cout << arr[i] << " ";
return 0;
}

Searching

Example 1: Linear Search


#include <iostream>
using namespace std;

int linearSearch(int arr[], int n, int x) {


for (int i = 0; i < n; ++i)
if (arr[i] == x) return i;
return -1;
}

int main() {
int arr[] = {2, 4, 0, 1, 9};
int n = sizeof(arr) / sizeof(arr[0]);
int x = 1;
int index = linearSearch(arr, n, x);
cout << "Element found at index: " << index << endl;
return 0;
}

Example 2: Binary Search

#include <iostream>
using namespace std;

int binarySearch(int arr[], int left, int right, int x) {


while (left <= right) {
int mid = left + (right - left) / 2;
if (arr[mid] == x)
return mid;
else if (arr[mid] < x)
left = mid + 1;
else
right = mid - 1;
}
return -1;
}

int main() {
int arr[] = {1, 3, 5, 7, 9};
int n = sizeof(arr) / sizeof(arr[0]);
int x = 7;
int index = binarySearch(arr, 0, n - 1, x);
cout << "Element found at index: " << index << endl;
return 0;
}

You might also like