Professional Documents
Culture Documents
of
Code:
#include <stdio.h>
i f(arr[i]==x)
i--;
else
break ;
while(i<=n)
if(arr[i+1]==x)
{
i++;
count++;
}
else
break ;
}
return count;
}
}
1. }
int main()
{
int arr[10],i,a,x;
printf("Enter the Array:");
for(i=0;i<10;i++)
scanf("%d",&arr[i]);
printf("Enter number duplicate count");
scanf("%d",&a);
x=count(arr,10,a);
printf(" here count %d",x);
return 0;
}
Output :
Time Complexity :
Best case: Ω(1)
Average case: θ(log n)
Worst case: O(n)
2. Given a 0-indexed integer array nums, find a peak element, and return its index. If
the array contains multiple peaks, return the index to any of the peaks.
*A peak element is an element that is strictly greater than its neighbors
Code :
#include <stdio.h>
{
int i;
for(i=0;i<n;i++)
{
if((arr[i+1]>arr[i])&&(arr[i+1]>arr[i+2]))
return i+1;
}
return -1;
}int main()
{
int nums[10],i,n;
printf("Enter the array size\n");
scanf("%d",&n);
printf("Enter the array elements");
for(i=0;i<n;i++)
scanf("%d",&nums[i]);
i=peak(nums,n);
printf("Peak at %d",i);
return 0;
} Output :
Time Complexity :
case: Ω(1)
Average case: θ(n)
Worst case: O(n)
3. Given an array arr of positive integers sorted in a strictly increasing order, and an
integer k.
Write a function to return the kth positive integer that is missing from this array.
Code :
#include <stdio.h>
int i,miss[10],j,k;
for(i=0,j=0,k=0;i<n;j++,k++)
if(arr[i]==j)
i++;
k--;
else
miss[k]=j;
for(i=0;i<ki;i++)
printf("%d ",miss[i]);
int main()
int nums[10],i,n,k;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&nums[i]);
scanf("%d",&k);
missing(nums,n,k);
return 0;
Output :
Time Complexity :
Best case: Ω(n)
Average case: θ(n)
Worst case: O(n)
4. Given an integer array nums and an integer k, return the kth largest element in the
array.
Note that it is the kth largest element in the sorted order, not the kth distinct element.
You must solve it in O(n) time complexity
Code :
#include <stdio.h>
int t = *a;
*a = *b;
*b = t;
i++;
swap(&array[i], &array[j]);
return (i + 1);
if(pi==k)
return pi;
else if(pi>k)
else
quickSort(array, pi + 1, high,k);
}}
int main() {
int data[10],k,i,n;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&data[i]);
scanf("%d",&k);
i= quickSort(data, 0, n - 1,n-k);
printf("Element is %d ",data[i]);
return 0;
Output :
Time Complexity :
Best case: Ω(1)
Average case: θ(n)
Worst case: O(nlogn)
5. Wiggle Sort 1:
Given an integer array nums, reorder it such that nums[0] <=nums[1] >=nums[2]
<=nums[3]....
Code :
#include <stdio.h>
int t = *a;
*a = *b;
*b = t; }
i++;
swap(&array[i], &array[j]);
}}
return (i + 1);
quickSort(array, pi + 1, high);
}}
int i;
swap(&arr[i],&arr[i+1]);
int main() {
int data[10],i,n;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&data[i]);
wiggle(data, 0, n-1);
printf("Array is :\n");
for(i=0;i<n;i++)
printf("%d ",data[i]);
return 0;
Output :
Time Complexity :
Best case: Ω(nlogn)
Average case: θ(nlogn)
Worst case: O(nlogn)
6. Given the weights and values of N items, in the form of {value, weight} put these
items in a knapsack of capacity W to get the maximum total value in the knapsack.
In Fractional Knapsack, we can break items for maximizing the total value of the
knapsack
# include<stdio.h>
float x[20], tp = 0;
int i, j, u
u = capacity;
x[i] = 0.0;
if (weight[i] > u)
break;
else {
x[i] = 1.0;
tp = tp + profit[i];
u = u - weight[i]; } }
if (i < n)
x[i] = u / weight[i];
tp = tp + (x[i] * profit[i]);
printf("%f\t", x[i]);
int main() {
int num, i, j;
scanf("%f", &capacity);
temp = ratio[j];
ratio[j] = ratio[i];
ratio[i] = temp;
temp = weight[j];
weight[j] = weight[i];
weight[i] = temp;
temp = profit[j];
profit[j] = profit[i];
profit[i] = temp; } }}
return(0); }
7: Given an array of jobs where every job has a deadline and associated
profit if the job is finished before the deadline. It is also given that every job
takes a single unit of time, so the minimum possible deadline for any job is 1.
Maximize the total profit if only one job can be scheduled at a time .
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
char id;
int dead;
int profit;
} Job;
int result[n];
bool slot[n];
slot[i] = false;
if (slot[j] == false) {
result[j] = i;
slot[j] = true;
break;} } }
int main() {
{ 'b', 1, 19 },
{ 'c', 2, 27 },
{ 'd', 1, 25 },
{ 'e', 3, 15 } };
printJobScheduling(arr, n);
return 0;
OUTPUT
#include <stdio.h>
int main() {
int n;
printf("enter the number of items\n");
scanf("%d",&n);
int W;
printf("enter the capacity of the Knapsack");
scanf("%d",&W);
int weight[n];
int profit[n];
for(int i=0;i<n;i++){
printf("enter the weight and profit for items I%d",i+1);
scanf("%d%d",&weight[i],&profit[i]); }
int ans=knapsack(weight,profit,W,n);
printf("%d",ans);
return 0; }
Time complexity :
Worst Case: 0(2^N)
#include <iostream>
#include<limits.h>
using namespace std;
int main() {
int nums[9]={-2,1,-3,4,-1,2,1,-5,4};
int maxi=INT_MIN;
for(int i=0;i<9;i++){
int sum=0;
for(int j=i;j<9;j++){
sum=sum+nums[j];
maxi=max(maxi,sum);
}
}
cout<<"maximum subarray sum is "<<maxi<<endl;
}
Time complexity:
Worst case: O(n*n)