├── probset1.py ├── reverse.py ├── sum_of_arr.py ├── rot_str.py ├── sum_if_dig_rec.py ├── arrary_divisors.py ├── small_ele_array.py ├── pattern11.py ├── checkifArrayEqual.py ├── middle_term.py ├── factorial.py ├── cube_n_terms.py ├── ZerosToEnd.py ├── print_ele.py ├── sort_arr_asc.py ├── pattern678.py ├── cornerdig_sum.py ├── longestcommonPrefix_str.py ├── bubble_sort.py ├── rot_arr_1.py ├── RemoveDupElements.py ├── Kth_Rotation_arr.py ├── dutch_flag_algo.py ├── anagram_string.py ├── MovNegEle2.py ├── MoveNegElements1.py ├── palindrome_string.py ├── RemoveDupEle2.py └── checkEqual_array.py /probset1.py: -------------------------------------------------------------------------------- 1 | a=2 2 | b="name" 3 | c='y' 4 | print(a) 5 | print(b) 6 | print(c) 7 | -------------------------------------------------------------------------------- /reverse.py: -------------------------------------------------------------------------------- 1 | n=int(input()) 2 | rev=0 3 | while n>0: 4 | ld=n%10 5 | rev=rev*10+ld 6 | n=n//10 7 | print(rev) 8 | -------------------------------------------------------------------------------- /sum_of_arr.py: -------------------------------------------------------------------------------- 1 | def sum_array(a,n,i): 2 | if i >=n: 3 | return 0 4 | return a[i]+sum_array(a,n,i+1) 5 | a=[1,3,2,4,5] 6 | print(sum_array(a,5,0)) 7 | -------------------------------------------------------------------------------- /rot_str.py: -------------------------------------------------------------------------------- 1 | def rot_str(s,goal): 2 | if len(s)!=len(goal): 3 | return False 4 | return goal in s+s 5 | s='abcde' 6 | goal='cdeab' 7 | print(rot_str(s,goal)) 8 | -------------------------------------------------------------------------------- /sum_if_dig_rec.py: -------------------------------------------------------------------------------- 1 | def cum_of_dig(n): 2 | if n==0: 3 | return 0 4 | if n<10: 5 | return n 6 | return n%10+cd(n//10) 7 | print(sum_of_dig(256)) 8 | -------------------------------------------------------------------------------- /arrary_divisors.py: -------------------------------------------------------------------------------- 1 | def print_divisors(n,i=1): 2 | if i>n: 3 | return n 4 | if n%i==0: 5 | print(i,end="") 6 | print_divisors(n,i+1) 7 | num=6 8 | print_divisors(num) 9 | -------------------------------------------------------------------------------- /small_ele_array.py: -------------------------------------------------------------------------------- 1 | n=5 2 | arr=[] 3 | for i in range(n): 4 | arr.append(int(input())) 5 | small=arr[0] 6 | for i in range(1,n): 7 | if small>arr[i]: 8 | small=arr[i] 9 | print(arr[i]) 10 | -------------------------------------------------------------------------------- /pattern11.py: -------------------------------------------------------------------------------- 1 | #pattern --> 11 2 | n=4 3 | for i in range(n+1): 4 | for j in range(i-1): 5 | print(" ",end=" "); 6 | for j in range((2*n-2*i-1)): 7 | print("*",end=" "); 8 | print() 9 | -------------------------------------------------------------------------------- /checkifArrayEqual.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def arraySortedOrNot(self, arr) -> bool: 3 | for i in range(len(arr) - 1): 4 | if arr[i] > arr[i + 1]: 5 | return False 6 | return True 7 | -------------------------------------------------------------------------------- /middle_term.py: -------------------------------------------------------------------------------- 1 | #https://www.geeksforgeeks.org/problems/middle-of-three2926/1 2 | class Solution: 3 | def middle(self, a, b, c): 4 | #code here 5 | return (a + b + c) - min(a, b, c) - max(a, b, c) 6 | 7 | -------------------------------------------------------------------------------- /factorial.py: -------------------------------------------------------------------------------- 1 | #FACTORIAL IN GFG 2 | class Solution: 3 | def factorial (self, n): 4 | # code here for factorial 5 | res=1 6 | while n>0: 7 | res=res*n 8 | n=n-1 9 | return res 10 | -------------------------------------------------------------------------------- /cube_n_terms.py: -------------------------------------------------------------------------------- 1 | #https://www.geeksforgeeks.org/problems/sum-of-first-n-terms5843/1 2 | class Solution: 3 | def sumOfSeries(self,n): 4 | #code here 5 | sum=0 6 | for i in range(1,n+1): 7 | sum=sum+i**3 8 | return sum 9 | -------------------------------------------------------------------------------- /ZerosToEnd.py: -------------------------------------------------------------------------------- 1 | def ZerosToEnd(arr): 2 | n=len(arr) 3 | j=0 4 | for i in range(n): 5 | if arr[i]!=0: 6 | arr[j]=arr[i] 7 | j+=1 8 | for i in range(j,n): 9 | arr[i]=0 10 | return arr 11 | arr=[1,0,6,9,7,0] 12 | print(ZerosToEnd(arr)) 13 | -------------------------------------------------------------------------------- /print_ele.py: -------------------------------------------------------------------------------- 1 | def printrec(a, n, i, target): 2 | if i >= n: 3 | return -1 4 | if a[i] == target: 5 | return i 6 | return printrec(a, n, i + 1, target) 7 | a = [1, 2, 389, 4, 5] 8 | result = printrec(a, 5, 0, 4) 9 | print(result) 10 | -------------------------------------------------------------------------------- /sort_arr_asc.py: -------------------------------------------------------------------------------- 1 | def sum_array(a,n,i): 2 | if i >=n: 3 | return 0 4 | return a[i]+sum_array(a,n,i+1) 5 | def check_sort(a,n,i): 6 | if i >= n-1: 7 | return True 8 | if a[i]>a[i+1]: 9 | return False 10 | a=[89,0,2,5,4] 11 | print(check_sort(a,5,0)) 12 | -------------------------------------------------------------------------------- /pattern678.py: -------------------------------------------------------------------------------- 1 | #PATTERN --> 6 2 | n=4 3 | for i in range(1,n+1): 4 | for j in range(1,n-i+2): 5 | print("*",end=""); 6 | print() 7 | 8 | #PATTERN -->8 9 | n=6 10 | for i in range(1,n+1): 11 | for j in range(1,i+1): 12 | print("*",end=""); 13 | print() 14 | 15 | #PATTERN -->7 16 | -------------------------------------------------------------------------------- /cornerdig_sum.py: -------------------------------------------------------------------------------- 1 | 2 | #https://www.geeksforgeeks.org/problems/corner-digits1317/1----->CORNER DIGIT SUM 3 | class Solution: 4 | def corner_digitSum(self, n): 5 | # Code here 6 | ld=n%10 7 | if n<10: 8 | return n 9 | 10 | while n>0: 11 | fd=n%10 12 | n=n//10 13 | 14 | return(fd+ld) 15 | -------------------------------------------------------------------------------- /longestcommonPrefix_str.py: -------------------------------------------------------------------------------- 1 | def lcp(strs): 2 | if not strs: 3 | return " " 4 | prefix=strs[0] 5 | for word in strs[1:]: 6 | while not word.startswith(prefix): 7 | prefix=prefix[:-1] 8 | if not prefix: 9 | return " " 10 | return prefix 11 | words=["flow","flight","flower"] 12 | print(lcp(words)) 13 | -------------------------------------------------------------------------------- /bubble_sort.py: -------------------------------------------------------------------------------- 1 | def bub_sort(arr): 2 | n=len(arr) 3 | for i in range(n): 4 | swapped=False 5 | for j in range(0,n-i-1): 6 | if arr[j]>arr[j+1]: 7 | arr[j],arr[j+1]=arr[j+1],arr[j] 8 | swapped=True 9 | if not swapped: 10 | break 11 | return arr 12 | arr=[56,90,23,1,5] 13 | sort_arr=bub_sort(arr) 14 | print(sort_arr) 15 | -------------------------------------------------------------------------------- /rot_arr_1.py: -------------------------------------------------------------------------------- 1 | def rotate_clockwise(arr): 2 | if len(arr) > 1: # Step 1: Check if array has more than 1 element 3 | last_element = arr[-1] # Step 2: Store last element 4 | 5 | for i in range(len(arr) - 1, 0, -1): # Step 3: Loop from last to first 6 | arr[i] = arr[i - 1] # Step 4: Shift elements right 7 | 8 | arr[0] = last_element # Step 5: Place last element at front 9 | 10 | # Example usage 11 | arr = [1, 2, 3, 4, 5] 12 | rotate_clockwise(arr) 13 | print(arr) # Output: [5, 1, 2, 3, 4] 14 | -------------------------------------------------------------------------------- /RemoveDupElements.py: -------------------------------------------------------------------------------- 1 | def removeDuplicates(arr): 2 | if not arr: 3 | return 0 # if array is empty 4 | 5 | j = 0 # Start with the first element 6 | 7 | for i in range(1, len(arr)): 8 | if arr[i] != arr[j]: 9 | j += 1 10 | arr[j] = arr[i] 11 | return j + 1 # Total number of unique elements 12 | 13 | # Test array 14 | arr = [1, 2, 2, 3, 3, 4] 15 | 16 | # Call the function 17 | new_length = removeDuplicates(arr) 18 | 19 | # Print the results 20 | print(new_length) 21 | print(arr[:new_length]) 22 | -------------------------------------------------------------------------------- /Kth_Rotation_arr.py: -------------------------------------------------------------------------------- 1 | def findKRotation(self, arr): 2 | 3 | low=0 4 | high=len(arr)-1 5 | while low<=high: 6 | if arr[low]<=arr[high]: 7 | return low 8 | mid=(low+high)//2 9 | next_=(mid+1)%len(arr) 10 | prev=(mid-1+len(arr)%len(arr)) 11 | if arr[mid]<=arr[next_] and arr[mid]<=arr[prev]: 12 | return mid 13 | if arr[mid]>=arr[low]: 14 | low=mid+1 15 | else: 16 | high=mid-1 17 | 18 | -------------------------------------------------------------------------------- /dutch_flag_algo.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def sort012(self, arr): 3 | # Three pointers: low for 0s, mid for 1s, and high for 2s 4 | low, mid, high = 0, 0, len(arr) - 1 5 | 6 | while mid <= high: 7 | if arr[mid] == 0: 8 | arr[low], arr[mid] = arr[mid], arr[low] 9 | low += 1 10 | mid += 1 11 | elif arr[mid] == 1: 12 | mid += 1 13 | else: # arr[mid] == 2 14 | arr[mid], arr[high] = arr[high], arr[mid] 15 | high -= 1 16 | -------------------------------------------------------------------------------- /anagram_string.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | # Function to check whether two strings are anagram of each other or not. 3 | def areAnagrams(self, s1, s2): 4 | # code here 5 | if len(s1) != len(s2): 6 | return False 7 | 8 | freq = [0] * 256 # To store frequency of characters 9 | for char in s1: 10 | freq[ord(char)] += 1 11 | for char in s2: 12 | freq[ord(char)] -= 1 13 | 14 | for count in freq: 15 | if count != 0: 16 | return False 17 | return True 18 | -------------------------------------------------------------------------------- /MovNegEle2.py: -------------------------------------------------------------------------------- 1 | def moveNegativesToEnd(arr): 2 | n = len(arr) 3 | j = 0 # Index for placing the next non-negative number 4 | 5 | # Step 1: Move non-negative numbers to the front 6 | for i in range(n): 7 | if arr[i] >= 0: 8 | arr[j] = arr[i] 9 | j += 1 10 | 11 | 12 | # Step 2: Move negative numbers to the end 13 | for i in range(n): 14 | if arr[i] < 0: 15 | arr[j] = arr[i] 16 | j += 1 17 | 18 | 19 | # Example usage: 20 | arr = [1, -2, 3, -4, 5, -6] 21 | moveNegativesToEnd(arr) 22 | print(arr) # Output: [1, 3, 5, -2, -4, -6] 23 | -------------------------------------------------------------------------------- /MoveNegElements1.py: -------------------------------------------------------------------------------- 1 | #https://www.geeksforgeeks.org/problems/move-all-negative-elements-to-end1813/1 2 | class Solution: 3 | def segregateElements(self, arr): 4 | n = len(arr) 5 | temp = [] 6 | 7 | # Collect non-negative numbers 8 | for i in range(n): 9 | if arr[i] >= 0: 10 | temp.append(arr[i]) 11 | 12 | # Collect negative numbers 13 | for i in range(n): 14 | if arr[i] < 0: 15 | temp.append(arr[i]) 16 | 17 | # Copy the result back to the original array 18 | for i in range(n): 19 | arr[i] = temp[i] 20 | 21 | -------------------------------------------------------------------------------- /palindrome_string.py: -------------------------------------------------------------------------------- 1 | # It skips commas, spaces, and colons........Compares letters like 'A' and 'a' as equal (case-insensitive)....All characters match → returns True. 2 | class Solution: 3 | def isPalindrome(self, s: str) -> bool: 4 | start=0 5 | end=len(s)-1 6 | while start < end: 7 | if not s[start].isalnum(): 8 | start+=1 9 | continue 10 | if not s[end].isalnum(): 11 | end-=1 12 | continue 13 | if s[start].lower()!=s[end].lower(): 14 | return False 15 | start+=1 16 | end-=1 17 | return True 18 | -------------------------------------------------------------------------------- /RemoveDupEle2.py: -------------------------------------------------------------------------------- 1 | def removeDuplicates(arr): 2 | seen = set() # Set to track unique elements 3 | j = 0 # Pointer to place unique elements 4 | 5 | # Loop through each element of the array 6 | for i in range(len(arr)): 7 | if arr[i] not in seen: 8 | seen.add(arr[i]) # Add the element to the set (unique elements) 9 | arr[j] = arr[i] # Place unique element at index j 10 | j += 1 # Move j forward 11 | 12 | # Return the new length of the array with unique elements 13 | return j 14 | 15 | # Example 16 | arr = [4, 5, 4, 2, 3, 5, 1] 17 | new_length = removeDuplicates(arr) 18 | 19 | # Print the result 20 | print(new_length) 21 | print(arr[:new_length]) 22 | -------------------------------------------------------------------------------- /checkEqual_array.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def checkEqual(self, a, b) -> bool: 3 | # If lengths differ, they can't be equal 4 | if len(a) != len(b): 5 | return False 6 | 7 | # Create dictionaries to count frequency manually 8 | freq_a = {} 9 | freq_b = {} 10 | 11 | # Count frequency in array a 12 | for num in a: 13 | if num in freq_a: 14 | freq_a[num] += 1 15 | else: 16 | freq_a[num] = 1 17 | 18 | # Count frequency in array b 19 | for num in b: 20 | if num in freq_b: 21 | freq_b[num] += 1 22 | else: 23 | freq_b[num] = 1 24 | 25 | # Compare frequency dictionaries 26 | return freq_a == freq_b 27 | --------------------------------------------------------------------------------