Daa Questions
Daa Questions
#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;
}