Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

efficient algorithms for general tasks with good time complexity.

License

NotificationsYou must be signed in to change notification settings

Tanmay-901/python-algorithms

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

73 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

efficient algorithms for general tasks with good time complexity.

To send a Pull-request:

  • Check our contribution guidelines inCONTRIBUTING.md
  • Star and Fork this repo.
  • Clone the repo on your local machine usinggit clone https://github.com/{your-username}/python-algorithms.git.
  • Create a branchgit checkout -b your_new_branch_name
  • Make required changes then add and commit
    git add.
    git commit -m "your commit message"
  • Push your changesgit push origin your_new_branch_name
  • Merge and send a Pull-request.

Referance: Competetive programming with python


bitwise operator not {~} : (a = 10 => 1010 (Binary) => ~a = ~1010 = -(1010 + 1) = -(1011) = -11)

bitwise operator xor {^} : (n^n = 0), (n^0 = n)

bitwise operator rightshift {>>} : (100 >> 2 = 25)

bitwise operator leftshift {<<} : (100 << 2 = 400)


sum of first n numbers:O(1)

defsum_total(n):returnint(n*(n+1)/2)

LCM/GCD:(Euclid's algorithm)

defgcd(a,b):ifa==0:returnbreturngcd(b%a,a)deflcm(a,b):prod=a*bhcf=gcd(a,b)returnprod//hcf

Odd-Even:O(1)

ifn&1==1:print('odd')else:print('even')

Leftshift(multiply) / Rightshift(divide) by 2n:O(1)

defmultpow(x,y):returnx<<y# x*(2^y)defdivpow(x,y):returnx>>y# x/(2^y)

Check if a number is power of 2:O(1)

defispow(n):ifn<=0:returnFalsex=ny=not(n& (n-1))returnxandy

count 1's in binary representation:O(log(n))

defcntbits(n):cnt=0whilen:cnt+=1n=n& (n-1)returncnt

convert int to binary / binary to int:O(1)

definttobin(n):returnstr(bin(n))[2:]defbintoint(m):returnint(m,2)

check which number occurs once(or odd number of times/doesn't has it's unique identical element) in an array:O(n)

defcheckpair(arr):# n -> araytemp=arr[0]foriinrange(1,len(arr)):temp=temp^arr[i]returntemp

Releases

No releases published

Packages

No packages published

Contributors8

Languages


[8]ページ先頭

©2009-2025 Movatter.jp