Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added files for cpp and py for kth Largest element #3554

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 28 additions & 23 deletions search/binary_search/C/binarySearch.c
Original file line number Diff line number Diff line change
@@ -1,32 +1,37 @@
#include <stdio.h>
int main()
{
int c, first, last, middle, n, search, array[100];

int binarySearch(int arr[], int start, int end, int x) {
printf("Enter number of elements\n");
scanf("%d", &n);

if (start <= end) {
printf("Enter %d integers\n", n);

int mid = start + (end - start) / 2;
for (c = 0; c < n; c++)
scanf("%d", &array[c]);

if (arr[mid] == x)
return mid;
printf("Enter value to find\n");
scanf("%d", &search);

if (arr[mid] > x)
return binarySearch(arr, start, mid - 1, x);
first = 0;
last = n - 1;
middle = (first+last)/2;

return binarySearch(arr, mid + 1, end, x);
}
return -1;
}
while (first <= last) {
if (array[middle] < search)
first = middle + 1;
else if (array[middle] == search) {
printf("%d found at location %d.\n", search, middle+1);
break;
}
else
last = middle - 1;

int main()
{
int arr[] = { 2, 3, 7, 8, 78, 99, 102, 5555 };
int x = 9;
int result = binarySearch(arr, 0, sizeof(arr) / sizeof(arr[0]), x);

if (result == -1) {
printf("Element not present");
}
else {
printf("Element found at index %d", result);
}
middle = (first + last)/2;
}
if (first > last)
printf("Not found! %d isn't present in the list.\n", search);

return 0;
}
37 changes: 37 additions & 0 deletions search/kth_largest_in_array/C++/Solution_O(n).cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#include<bits/stdc++.h>
#define ll long long
using namespace std;

ll partition(ll a[],int l,int r){
int k=l+rand()%(r-l+1),i=l;
swap(a[l],a[k]);
ll key=a[l];
for(int j=l+1;j<=r;j++){
if(a[j]<=key){
i++;
swap(a[i],a[j]);
}
}
swap(a[l],a[i]);
return i;
}

ll kth_largest(ll a[],ll l,ll r,ll k){
if(k>r-l+1||k<=0) return LLONG_MAX;
int p=partition(a,l,r);
if(p==r-k+1) return a[p];
if(p>r-k+1) return kth_largest(a,l,p-1,k-r+p-1);
return kth_largest(a,p+1,r,k);
}

int main(){
int n,k;
cin>>n>>k;
srand(time_t(NULL));
ll a[n];
for(int i=0;i<n;i++) cin>>a[i];
ll ans=kth_largest(a,0,n-1,k);
if(ans==LLONG_MAX) cout<<"No Kth largest element\n";
else cout<<ans<<'\n';
return 0;
}
32 changes: 32 additions & 0 deletions search/kth_largest_in_array/Python/Solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import random as rd

INF =float('inf')

def partition(a, l, r):
k = l+rd.randint(0,r-l+1)
a[l],a[k] = a[k],a[l]
key,i = a[l],l
for j in range(l+1,r+1):
if a[j]<=key:
i+=1
a[i],a[j] = a[j],a[i]
a[l],a[i] = a[i],a[l]
return i

def kth_largest(a,l,r,k):
if k>r-l+1 or k<=0:
return INF
p = partition(a,l,r)
if p==r-k+1:
return a[p]
if p>r-k+1:
return kth_largest(a,l,p-1,k-r+p-1)
return kth_largest(a,p+1,r,k)

n,k = map(int,input().split())
a = list(map(int,input().split()))
ans = kth_largest(a,0,n-1,k)
if ans == INF:
print('No Kth largest element')
else:
print(ans)
Loading