Skip to content
Open
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#include<bits/stdc++.h>
using namespace std ;

map<int,int> value;

void answer(vector<int> x , int m )
{
int ans1 = -1 , ans2 = -1 ;

for(int i = 0 ; i<x.size() ; i++)
{
int r=m-x[i] ;//remain
if ( r!=x[i] )
{
if(value.count(r) && value.count(x[i]))
{
ans1 = x[i] ;
ans2 = r ;
break ;
}
}
else
{
if(value[x[i]]>1)
{
ans1 = x[i] ;
ans2 = x[i] ;
break ;
}
}


}
set<int> s;
for(int i =0 ;i<x.size() ;i++ )
{
if(x[i]==ans1 || x[i]==ans2)
s.insert(i) ;
}

for(int i:s)
cout<<i+1<<" ";

}

int main()
{
int t,m,n;
cin>>t ;
while(t--)
{
value.clear() ;
cin>> m ;
cin>> n ;
vector<int> a(n);
for(int i =0 ; i<n ;i++)
{
cin>>a[i] ;
value[a[i]]++ ;
}
answer(a,m) ;
cout<<endl ;
}

}
125 changes: 125 additions & 0 deletions Coding Solutions/Search & Sort/KEYROTATE.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;

long Search(vector<long> arr ,long l,long r,long x)
{
if(l > r )
return -1 ;

long mid = l+(r-l)/2 ;

if(arr[mid] == x)
return mid ;

else if(arr[mid]<x)
return Search(arr,mid+1,r,x) ;

else
return Search(arr,l,mid-1,x) ;
}

long findPivot(vector<long>arr, long low, long high)
{
// Base cases
if (high < low)
return -1;
if (high == low)
return low;

// low + (high - low)/2;
long mid = (low + high) / 2;
if (mid < high && arr[mid] > arr[mid + 1])
return mid;

if (mid > low && arr[mid] < arr[mid - 1])
return (mid - 1);

if (arr[low] >= arr[mid])
return findPivot(arr, low, mid - 1);

return findPivot(arr, mid + 1, high);
}
int main()
{
int t;
cin >> t;

while(t--)
{
long n , x ,ans;

long pivot ;

cin>> n ;
vector<long> arr(n) ;

for(int i=0 ;i<n; i++)
{
cin>>arr[i] ;
}
cin>>x ;

pivot =findPivot(arr,0,n-1) ;
//cout<<"PIVOT = "<<pivot<<"\n" ;

if(pivot==-1) // no rotation
ans = Search(arr,0,n-1,x) ;

else if(arr[pivot]==x)
ans= pivot ;
else
{
if(x>=arr[0])
ans= Search(arr,0,pivot-1,x) ;
else ans=Search(arr,pivot+1,n-1,x) ;
}

cout<<ans<<"\n" ;


}
}
/*
My method
int main()
{
int t;
cin >> t;

while(t--)
{
long n , x ,ans;

long big= 0, ind = -1 ;

cin>> n ;
vector<long> arr(n) ;

for(int i=0 ;i<n; i++)
{
cin >>arr[i] ;
if(arr[i]>big)
{
big = arr[i] ;
ind = i ;
}
}
sort(arr.begin(),arr.end()) ;
cin>>x;
ans =Search(arr,0,n-1,x) ;


if(ans==-1) cout<<-1 ;

else
{
ans=ans-n+ind+1 ;

cout<<ans<<"\n" ;
}
}
return 0;
}
*/
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Competitive-coding
Solutions to coding problems
Solutions to coding problems I have did,

Nice properties:
1. gcd(P·N, P·M) = P·gcd(N, M)
2. lcm(P·N, P·M) = P·lcm(N, M)
3. lcm(N,M) * gcd(N,M) = N*M