0% found this document useful (0 votes)
10 views5 pages

Daa Questions

The document contains code snippets for linear search, binary search, finding triplets with a given sum from a sorted array, and finding a pair of elements with a given sum from an unsorted array.
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)
10 views5 pages

Daa Questions

The document contains code snippets for linear search, binary search, finding triplets with a given sum from a sorted array, and finding a pair of elements with a given sum from an unsorted array.
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/ 5

//Linear search

#include <stdio.h>
int linearSearch(int arr[], int n, int key, int *comparisons) {
for (int i = 0; i < n; i++) {
(*comparisons)++;
if (arr[i] == key)
return i;
}
return -1;
}
int main() {
int n, key;
printf("\nEnter the size of the array: ");
scanf("%d", &n);
int arr[n];
printf("Enter %d space-separated non-negative integers: ", n);
for (int i = 0; i < n; i++)
scanf("%d", &arr[i]);
printf("Enter the key to search for: ");
scanf("%d", &key);
int comparisons = 0;
int result = linearSearch(arr, n, key, &comparisons);
if (result != -1) {
printf("%d is Present and Comparisons is %d times\n",key, comparisons);
}
else
printf("Not Present %d\n", comparisons);
return 0;
}
//binary search
#include <stdio.h>
int binarySearch(int arr[], int n, int key, int *comparisons) {
int left = 0;
int right = n - 1;
while (left <= right) {
int mid = left + (right - left) / 2;
if (arr[mid] == key) {
(*comparisons)++;
return mid;
} else if (arr[mid] < key)
left = mid + 1;
else
right = mid - 1;
(*comparisons)++;
}
return -1;
}
int main() {
int n, key;
printf("\nEnter the size of the sorted array: ");
scanf("%d", &n);
int arr[n];
printf("Enter %d sorted space-separated integers: ", n);
for (int i = 0; i < n; i++)
scanf("%d", &arr[i]);
printf("Enter the key to search for: ");
scanf("%d", &key);
int comparisons = 0;
int result = binarySearch(arr, n, key, &comparisons);
if (result != -1)
printf("Present %d\n", comparisons);
else
printf("Not Present %d\n", comparisons);
return 0;
}
// question arr[i] + arr[j] = arr[k]
#include <stdio.h>
void findTripletIndices(int arr[], int n) {
for (int k = n - 1; k >= 0; k--) {
int i = 0;
int j = k - 1;
while (i < j) {
int sum = arr[i] + arr[j];
if (sum == arr[k]) {
printf("%d, %d, %d\n", i+1, j+1, k+1);
return;
} else if (sum < arr[k]) {
i++;
} else {
j--;
}
}
}
printf("No sequence found\n");
}
int main() {

int n;
printf("\nEnter the size of the sorted array: ");
scanf("%d", &n);
int arr[n];
printf("Enter %d sorted space-separated integers: ", n);
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
findTripletIndices(arr, n);
return 0;
}
//two element sum == key
#include <stdio.h>
#include <stdlib.h>
struct Element {
int value;
int index;
};
int compare(const void *a, const void *b) {
return ((struct Element *) a)->value - ((struct Element *) b)->value;
}
void findTwoElementsWithSum(int arr[], int n, int key) {
struct Element elements[n];
for (int i = 0; i < n; i++) {
elements[i].value = arr[i];
elements[i].index = i;
}
qsort(elements, n, sizeof(struct Element), compare);
int left = 0;
int right = n - 1;
while (left < right) {
int currentSum = elements[left].value + elements[right].value;
if (currentSum == key) {
printf("%d %d\n", elements[left].value, elements[right].value);
return;
} else if (currentSum < key)
left++;
else
right--;
}
printf("No Such Elements Exist\n");
}
int main() {
int n, key,arr[100];
printf("\nEnter the size of the array: ");
scanf("%d", &n);
printf("Enter %d space-separated integers: ", n);
for (int i = 0; i < n; i++)
scanf("%d", &arr[i]);
printf("Enter the key: ");
scanf("%d", &key);
findTwoElementsWithSum(arr, n, key);
return 0;
}

You might also like