Skip to content

Commit 9277f79

Browse files
committed
new problems added
1 parent 6af015c commit 9277f79

File tree

3 files changed

+65
-0
lines changed

3 files changed

+65
-0
lines changed

Arrays/all_subaaray_odd_length_sum.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
## most optimized one
2+
3+
def sumOddLengthSubarrays(arr):
4+
res = 0; freq = 0; n = len(arr)
5+
for i in range(n):
6+
freq = freq-(i+1)//2+(n-i+1)//2
7+
res += freq*arr[i]
8+
return res
9+
10+
def sub_lists (arr):
11+
k=0
12+
for i in range(len(arr) + 1):
13+
for j in range(i):
14+
if len(arr[j: i])%2!=0:
15+
k+=sum(arr[j: i])
16+
return k
17+
18+
l1 = [10,11,12]
19+
print(sub_lists(l1))

Arrays/good_pair.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
nums = [1,2,3,1,1,3]
2+
k=0
3+
for i in range(len(nums)):
4+
for j in range(len(nums)):
5+
if nums[i]==nums[j] and i<j:
6+
k+=1
7+
print(k)
8+
9+
10+
## java sol
11+
12+
# class Solution {
13+
# public int numIdenticalPairs(int[] nums) {
14+
# int count = 0;
15+
# for(int i=0;i<nums.length;i++){
16+
# for(int j=0;j<nums.length;j++){
17+
# if(nums[i]==nums[j] && i<j){
18+
# count++;
19+
# }
20+
# }
21+
# }
22+
# return count;
23+
24+
# }
25+
# }

Arrays/max_wealth.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
accounts = [[2,8,7],[7,1,3],[1,9,5]]
2+
temp = [sum(c) for c in accounts]
3+
print(max(temp))
4+
5+
6+
## java 100% faster
7+
8+
# class Solution {
9+
# public int maximumWealth(int[][] accounts) {
10+
# int wealth=0;
11+
# int max_wealth=0;
12+
# for(int i =0;i<accounts.length;i++){
13+
# for(int j=0;j<accounts[0].length;j++){
14+
# wealth=wealth+accounts[i][j];
15+
# max_wealth=Math.max(max_wealth,wealth);
16+
# }
17+
# wealth=0;
18+
# }
19+
# return max_wealth;
20+
# }
21+
# }

0 commit comments

Comments
 (0)