-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 32d4085
Showing
233 changed files
with
5,842 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
/PYTHON | ||
output.exe |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# Binary Search in 2D array | ||
arr = [[1,4,8,13],[16,19,20,25],[28,30,33,37]] | ||
target = 370 | ||
|
||
## optimized approach = O(log(m*n)) | ||
def binary_search_2d(arr,target): | ||
# number of rows | ||
m = len(arr) | ||
if m==0: | ||
return False | ||
# number of colmns | ||
n = len(arr[0]) | ||
left,right=0,(m*n)-1 | ||
# binary search implementation | ||
while left <= right: | ||
mid = left + (right-left)//2 | ||
# row_number = mid // n | ||
# col_number = mid % n | ||
mid_element = arr[mid//n][mid%n] | ||
if target==mid_element: | ||
return True | ||
elif target < mid_element: | ||
right = mid - 1 | ||
else: | ||
left = mid + 1 | ||
return False | ||
print(binary_search_2d(arr,target)) | ||
|
||
## brute force approach = O(m*n) | ||
def linear_search_2d(arr,target): | ||
if len(arr)==0: | ||
return False | ||
for row in range(len(arr)): | ||
for col in range(len(arr[0])): | ||
if arr[row][col] == target: | ||
return True | ||
return False | ||
print(linear_search_2d(arr,target)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# print all sub arrays of give array | ||
|
||
arr = [89,34,12,9,3,42,99] | ||
def print_subarray(arr): | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
''' | ||
You are a product manager and currently leading a team to develop a new product. | ||
Unfortunately, the latest version of your product fails the quality check. Since each | ||
version is developed based on the previous version, all the versions after a bad version | ||
are also bad. Suppose you have n version and you want to find out the first bad one, | ||
which causes all the following ones to be bad. Also, talk about the time complexity of | ||
your code. | ||
Test Cases: | ||
Input: [0,0,0,1,1,1,1,1,1] | ||
Output: 3 | ||
Explanation: 0 indicates a good version and 1 indicates a bad version. So, the index of | ||
the first 1 is at 3. Thus, the output is 3 | ||
''' | ||
arr = [0,0,0,0,1,1,1,1,1] | ||
|
||
left,right=0,len(arr)-1 | ||
def first_bad_version(arr,left,right): | ||
mid = left + (right-left)//2 | ||
while left <= right: | ||
if arr[mid]==1: | ||
if arr[mid-1]==1: | ||
return first_bad_version(arr,left,mid-1) | ||
return mid | ||
elif arr[mid]!=1: | ||
if arr[mid+1]!=1: | ||
return first_bad_version(arr,mid+1,right) | ||
return mid+1 | ||
return -1 | ||
|
||
print(first_bad_version(arr,left,right)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
|
||
# Best time to buy and sell the stocks | ||
|
||
prices = [7,1,4,6,2,8,10] | ||
|
||
def find_max_profit(prices): | ||
if not prices: | ||
return 0 | ||
min_price = float('inf') | ||
max_profit = 0 | ||
for price in prices: | ||
if price < min_price: | ||
min_price = price | ||
elif price - min_price > max_profit: | ||
max_profit = price - min_price | ||
return max_profit | ||
|
||
|
||
print(find_max_profit(prices)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
from collections import Counter | ||
|
||
nums = [1, 2, 3, 4, 1, 2, 2, 3, 42, 3, 4, 2, 3, 3] | ||
def count_most_repeated(nums): | ||
hash = {} | ||
for item in nums: | ||
if item in hash: | ||
hash[item] += 1 | ||
else: | ||
hash[item] = 1 | ||
|
||
ans, max_count = 0, 0 | ||
for key, value in hash.items(): | ||
if value > max_count: | ||
max_count = value | ||
ans = key | ||
return [ans, max_count] | ||
|
||
def count_most_repeated2(nums): | ||
count = Counter(nums) | ||
most_common = count.most_common(1)[0] | ||
return [most_common[0], most_common[1]] | ||
|
||
print(count_most_repeated(nums)) | ||
print(count_most_repeated2(nums)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
students = ["Abhi", "Ankit", "Aman", "Rohan"] | ||
def find_student(students, name): | ||
if name in students: | ||
return f"Student {name} is present in the list" | ||
else: | ||
return f"Student {name} is not present in the list" | ||
|
||
print(find_student(students, "Rahul")) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# Find the first occurrence of zero. Size of array is given. (Facebook Technical Round) | ||
arr = [1,90,23,14,45,76,88,0,0,0,0,0,0,0,0,0,0] | ||
n=len(arr) # 17 | ||
|
||
# First Approach = O(n) | ||
def first_zero_serach1(arr): | ||
for i in range(len(arr)): | ||
if arr[i] == 0: | ||
return i | ||
return -1 | ||
# print(first_zero_serach1(arr)) | ||
|
||
# Second Approach = O(logn) | ||
start,end=0,n-1 | ||
def first_zero_serach2(arr,start,end): | ||
mid = start + (end - start)//2 | ||
while start <= end: | ||
if arr[mid] == 0: | ||
if arr[mid-1]==0: | ||
return first_zero_serach2(arr,start,mid-1) | ||
return mid | ||
elif arr[mid] != 0: | ||
if arr[mid+1]==0: | ||
return mid+1 | ||
return first_zero_serach2(arr,mid+1,end) | ||
return -1 | ||
print(first_zero_serach2(arr,start,end)) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
#include<iostream> | ||
using namespace std; | ||
int min_value(int arr[], int n){ | ||
int ans=INT_MAX; | ||
for (int i = 0; i < n; i++) | ||
{ | ||
if(arr[i]<ans){ | ||
ans=arr[i]; | ||
} | ||
} | ||
return ans; | ||
} | ||
int max_value(int arr[], int n){ | ||
int ans = INT_MIN; | ||
for (int i = 0; i < n; i++) | ||
{ | ||
if(arr[i]>ans){ | ||
ans=arr[i]; | ||
} | ||
} | ||
return(ans); | ||
} | ||
int main(){ | ||
int arr[5] = {3,10,2,45,76}; | ||
|
||
cout<<min_value(arr,5)<<endl; | ||
cout<<max_value(arr,5)<<endl; | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
|
||
arr = [23,45,22,52,10,49,90,214,1] | ||
i = 0 | ||
j = len(arr)-1 | ||
|
||
# Time Complexity is O(n) | ||
def min_max_element(arr): | ||
max_elem=0 | ||
for elem in arr: | ||
if elem > max_elem: | ||
max_elem = elem | ||
min_elem = float('inf') | ||
for elem in arr: | ||
if elem < min_elem: | ||
min_elem=elem | ||
return min_elem,max_elem | ||
|
||
## method definition = O(n) | ||
def find_max_min(arr,i,j): | ||
## small problem | ||
# single element | ||
if i == j: | ||
max_val=arr[i] | ||
min_val=arr[i] | ||
# two element | ||
elif i == j-1: | ||
if arr[i] < arr[j]: | ||
max_val=arr[j] | ||
min_val=arr[i] | ||
else: | ||
max_val=arr[i] | ||
min_val=arr[j] | ||
## big problem | ||
else: | ||
# divide and conquer approach | ||
mid = i + (j-i)//2 | ||
# recursion : conquer | ||
max1,min1=find_max_min(arr,i,mid) | ||
max2,min2=find_max_min(arr,mid+1,j) | ||
# combine | ||
## to find the final maxima | ||
if max1 > max2: | ||
max_val=max1 | ||
else: | ||
max_val=max2 | ||
## to find the final minima | ||
if min1 < min2: | ||
min_val=min1 | ||
else: | ||
min_val=min2 | ||
return max_val,min_val | ||
|
||
max,min = find_max_min(arr,i,j) | ||
print(f"Max value: {max} and Min value: {min}") | ||
|
||
|
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
#include<stdio.h> | ||
#include<stdlib.h> | ||
#include<conio.h> | ||
int getOccurrence(int arr[], int n, int key){ | ||
int count; | ||
for (int i = 0; i < n; i++) | ||
{ | ||
if(arr[0] == key){ | ||
count++; | ||
} | ||
} | ||
|
||
} | ||
int main(int argc, char *argv[]){ | ||
int arr[] = {1,2,3,1,2,4,6,8}; | ||
int n = sizeof(arr)/sizeof(arr[0]); | ||
printf("%d\n",getOccurrence(arr,n,1)); | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# Amazon Interview Question : Find the power of a number | ||
# Time Complexity = O(logn) | ||
def power(a,n): | ||
# base case | ||
if n==0: | ||
return 1 | ||
if n==1: | ||
return a | ||
# body | ||
result = power(a,n//2) | ||
if n%2==0: | ||
return result * result | ||
else: | ||
return result * result * a | ||
|
||
|
||
print(power(2,10)) | ||
# print(power(2,11)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
arr = [3,5,20,45,2,-5,9] | ||
|
||
def prefix_sum(arr): | ||
ans=[0]*len(arr) | ||
ans[0]=arr[0] | ||
for i in range(1,len(arr)): | ||
ans[i]=ans[i-1]+arr[i] | ||
return ans | ||
|
||
print("Orginal Array: ", arr) | ||
print("Prefix Sum Array: ", prefix_sum(arr)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
1. Find First and Last Position of Element in Sorted Array https://leetcode.com/problems/find-first-and-last-position-of-element-in-sorted-array/description/ | ||
2. Find the Peaks https://leetcode.com/problems/find-the-peaks/description/ | ||
3. Remove One Element to Make the Array Strictly Increasing https://leetcode.com/problems/remove-one-element-to-make-the-array-strictly-increasing/description/ | ||
4. Move Zeroes https://leetcode.com/problems/move-zeroes/description/ | ||
5. Apply Operations to an Array https://leetcode.com/problems/apply-operations-to-an-array/description/ | ||
5. Find if Digit Game Can Be Won https://leetcode.com/problems/find-if-digit-game-can-be-won/description/ | ||
6. Find Minimum Operations to Make All Elements Divisible by Three https://leetcode.com/problems/find-minimum-operations-to-make-all-elements-divisible-by-three/description/ | ||
7. Richest Customer Wealth https://leetcode.com/problems/richest-customer-wealth/description/ | ||
8. Number of Employees Who Met the Target https://leetcode.com/problems/number-of-employees-who-met-the-target/description/ | ||
9. Kids With the Greatest Number of Candies https://leetcode.com/problems/kids-with-the-greatest-number-of-candies/description/ | ||
10. Count Pairs Whose Sum is Less than Target https://leetcode.com/problems/count-pairs-whose-sum-is-less-than-target/description/ | ||
11. Running Sum of 1d Array https://leetcode.com/problems/running-sum-of-1d-array/description/ | ||
12. Maximum Subarray https://leetcode.com/problems/maximum-subarray/description/ | ||
13. Kadane's Algorithm https://www.geeksforgeeks.org/problems/kadanes-algorithm-1587115620/1 | ||
14. Longest Substring Without Repeating Characters https://leetcode.com/problems/longest-substring-without-repeating-characters/description/ | ||
15. Height Checker https://leetcode.com/problems/height-checker/submissions/1370271025/ | ||
16. Find Indices of Stable Mountains https://leetcode.com/problems/find-indices-of-stable-mountains/description/ | ||
17. Sort Colors https://leetcode.com/problems/sort-colors/description/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
''' | ||
Given a positive integer num, write a program that returns True if num is a perfect | ||
square else return False. Do not use built-in functions like sqrt. Also, talk about the time | ||
complexity of your code. | ||
Test Cases: | ||
Input: 16 | ||
Output: True | ||
Input: 14 | ||
Output: False | ||
''' | ||
import math | ||
|
||
def check_perfect_number(n): | ||
if int(math.sqrt(n)) * int(math.sqrt(n))== n: | ||
return True | ||
return False | ||
|
||
print(check_perfect_number(64)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
#include<stdio.h> | ||
#include<stdlib.h> | ||
#include<conio.h> | ||
void getSubarray(int nums[], int length){ | ||
int total_subarray = 0; | ||
for (int i = 0; i < length; i++) | ||
{ | ||
int start = i; | ||
for (int j = i; j < length; j++) | ||
{ | ||
int end = j; | ||
for (int k = start; k <= end; k++) | ||
{ | ||
printf("%d ", nums[k]); | ||
} | ||
total_subarray+=1; | ||
printf("\n"); | ||
|
||
} | ||
printf("\n"); | ||
|
||
} | ||
printf("Total no. of subarray is = %d", total_subarray); | ||
|
||
} | ||
int main(int argc, char *argv[]){ | ||
int nums[] = {1,4,5,7,10}; | ||
int length = sizeof(nums)/sizeof(nums[0]); | ||
getSubarray(nums, length); | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
arr = [3,5,20,45,2,-5,9] | ||
|
||
def suffix_sum(arr): | ||
n = len(arr) | ||
ans=[0]*n | ||
ans[n-1]=arr[n-1] | ||
# print(ans) | ||
for i in range(n-2,-1,-1): | ||
ans[i]=ans[i+1]+arr[i] | ||
return ans | ||
|
||
print("Orginal Array: ", arr) | ||
print(suffix_sum(arr)) |
Oops, something went wrong.