Skip to content

Commit 6adf9f3

Browse files
authored
Merge pull request #212 from dilroseR/dilroseR
added 4 new programs
2 parents 1737e6d + 6c87893 commit 6adf9f3

File tree

4 files changed

+186
-0
lines changed

4 files changed

+186
-0
lines changed

Arrays/KLargestpairs.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# K Largest Pairs
2+
3+
"""
4+
You are given two lists of integers nums0, nums1 and an integer k.
5+
Find k largest sum pairs where each pair contains one integer in nums0 and another in nums1,
6+
and return the sum of all of the pairs.
7+
8+
Constraints
9+
10+
0 ≤ n ≤ 100,000 where n is the length of nums0
11+
0 ≤ m ≤ 100,000 where m is the length of nums1
12+
0 ≤ k ≤ 100,000
13+
0 ≤ k ≤ n * m
14+
15+
16+
Example 1:
17+
18+
Input:-
19+
nums0 = [5, 3, 9]
20+
nums1 = [1, 2, 4]
21+
k = 2
22+
23+
Output:-
24+
24
25+
26+
Explanation:-
27+
The 2 largest pairs are (9,2) and (9,4)..therefore the sum of the pairs would be 9+2+9+4=24
28+
"""
29+
30+
31+
def solve(nums0, nums1, k):
32+
lst=[]
33+
tot=0
34+
for i in nums0:
35+
for j in nums1:
36+
sum=0
37+
sum=i+j
38+
lst.append(sum)
39+
lst=sorted(lst,reverse=True)
40+
for i in range(0,k):
41+
tot=tot+lst[i]
42+
return tot
43+
44+
n1=int(input(("Enter no. of elements in num list1: ")))
45+
n2=int(input(("Enter no. of elements in num list2: ")))
46+
k=int(input("Enter no. of largest pairs that you need: "))
47+
list1=[]
48+
list2=[]
49+
print("Enter elements of list1 one by one: ")
50+
for i in range(0,n1):
51+
52+
ele1=int(input())
53+
list1.append(ele1)
54+
55+
print("Enter elements of list2 one by one: ")
56+
for j in range(0,n2):
57+
58+
ele2=int(input())
59+
list2.append(ele2)
60+
61+
ans=solve(list1,list2,k)
62+
print("Sum of the largest " + str(k) + " pairs is",ans)

Arrays/ShortestContinuousSubarray.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Shortest Unsorted Continuous Subarray
2+
3+
"""
4+
Find one continuous subarray that if you only sort this subarray in ascending order, then the whole array will be sorted in ascending order.
5+
Return length of the shortest subarray.
6+
7+
Example 1: nums = [2,6,4,8,10,9,15]
8+
o/p:- 5
9+
Explanation: You need to sort [6, 4, 8, 10, 9] in ascending order to make the whole array sorted in ascending order.
10+
11+
Example 2: nums = [1,2,3,4]
12+
o/p:- 0
13+
"""
14+
15+
def findUnsortedSubarray(nums):
16+
res=[]
17+
ans=[]
18+
final=[]
19+
for i in nums:
20+
res.append(i)
21+
22+
res.sort()
23+
for i in range(0,len(nums)):
24+
if nums[i]!=res[i]:
25+
ans.append(i)
26+
27+
if len(ans)==0:
28+
return 0
29+
else:
30+
for i in range(ans[0],ans[len(ans)-1]+1):
31+
final.append(nums[i])
32+
33+
return len(final)
34+
35+
nums=[]
36+
n=int(input("Enter the no. of elements: "))
37+
print("Enter the elements of the array one by one: ")
38+
for i in range(0,n):
39+
40+
ele=int(input())
41+
nums.append(ele)
42+
43+
answer=findUnsortedSubarray(nums)
44+
print("The length of the shortest subarray:",answer)

Numbers/RomantoInteger.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# To convert Roman numeral to its equivalent integer
2+
3+
def romanToInt(s):
4+
romanToNum={
5+
'I':1,
6+
'V':5,
7+
'X':10,
8+
'L':50,
9+
'C':100,
10+
'D':500,
11+
'M':1000
12+
}
13+
14+
total=0
15+
i=0 # to iterate through each letter of the roman numeral
16+
17+
while i<len(s):
18+
index1=romanToNum[s[i]]
19+
if(i+1<len(s)):
20+
index2=romanToNum[s[i+1]]
21+
if index1>=index2:
22+
total=total+index1
23+
i=i+1
24+
else:
25+
total=total-index1
26+
i=i+1
27+
28+
else:
29+
total=total+index1
30+
i=i+1
31+
return total
32+
33+
print("Enter the Roman Numeral")
34+
s=input()
35+
st=romanToInt(s)
36+
print("The roman to integer of the given input is: ",st)
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import java.util.*;
2+
class Main{
3+
public static int removePalindromeSub(String s) {
4+
5+
char[] c=new char[s.length()];
6+
ArrayList<Character> al=new ArrayList<>();
7+
ArrayList<Character> bl=new ArrayList<>();
8+
9+
for(int i=0;i<s.length();i++)
10+
{
11+
char a=s.charAt(i);
12+
al.add(a);
13+
bl.add(a);
14+
15+
}
16+
17+
Collections.reverse(bl);
18+
19+
if(al.size()==0)
20+
{
21+
return 0;
22+
}
23+
24+
else if(al.equals(bl))
25+
{
26+
return 1;
27+
}
28+
else
29+
{
30+
return 2;
31+
}
32+
}
33+
34+
public static void main(String args[])
35+
{
36+
Scanner sc=new Scanner(System.in);
37+
System.out.println("Enter the string: ");
38+
int ans=0;
39+
String s=sc.next();
40+
ans=removePalindromeSub(s);
41+
System.out.println("Output: "+ans);
42+
43+
}
44+
}

0 commit comments

Comments
 (0)