Skip to content

Commit 83a78f3

Browse files
Merge branch 'smv1999:master' into determine-if-max-heap
2 parents c75d23f + 21edf8c commit 83a78f3

33 files changed

+1858
-28
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/Kadane's_Algorithm.cpp

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
2+
/* Given an array arr of N integers. Find the contiguous sub-array with maximum sum.
3+
4+
5+
6+
Example 1:
7+
8+
Input:
9+
N = 5
10+
arr[] = {1,2,3,-2,5}
11+
Output:
12+
9
13+
Explanation:
14+
Max subarray sum is 9
15+
of elements (1, 2, 3, -2, 5) which
16+
is a contiguous subarray.*/
17+
#include<bits/stdc++.h>
18+
using namespace std;
19+
20+
21+
// } Driver Code Ends
22+
23+
24+
// Function to find subarray with maximum sum
25+
// arr: input array
26+
// n: size of array
27+
int maxSubarraySum(int arr[], int n){
28+
29+
// Your code here
30+
int i,maxCur=arr[0];
31+
int totalsum=arr[0];
32+
for(i=1;i<n;i++)
33+
{
34+
maxCur=max(arr[i],arr[i]+maxCur);
35+
if(maxCur>totalsum)
36+
{
37+
totalsum=maxCur;
38+
}
39+
}
40+
return totalsum;
41+
42+
}
43+
44+
// { Driver Code Starts.
45+
46+
int main()
47+
{
48+
int t,n;
49+
50+
cin>>t; //input testcases
51+
while(t--) //while testcases exist
52+
{
53+
54+
cin>>n; //input size of array
55+
56+
int a[n];
57+
58+
for(int i=0;i<n;i++)
59+
cin>>a[i]; //inputting elements of array
60+
61+
cout << maxSubarraySum(a, n) << endl;
62+
}
63+
}
64+
// } Driver Code Ends

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)

Bit Manipulation/Bit_Difference.cpp

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
2+
/*
3+
You are given two numbers A and B. The task is to count the number of bits needed to be flipped to convert A to B.
4+
5+
Example 1:
6+
7+
Input: A = 10, B = 20
8+
Output: 4
9+
Explanation:
10+
A = 01010
11+
B = 10100
12+
As we can see, the bits of A that need
13+
to be flipped are 01010. If we flip
14+
these bits, we get 10100, which is B.*/
15+
//Initial Template for C++
16+
17+
#include<bits/stdc++.h>
18+
using namespace std;
19+
20+
// Function to find number of bits to be flip
21+
// to convert A to B
22+
int countBitsFlip(int a, int b){
23+
24+
// Your logic here
25+
int ans=a^b;
26+
int c=0;
27+
while(ans>0)
28+
{
29+
ans &=(ans-1);
30+
c++;
31+
}
32+
return c;
33+
34+
}
35+
36+
// { Driver Code Starts.
37+
38+
// Driver Code
39+
int main()
40+
{
41+
int t;
42+
cin>>t;// input the testcases
43+
while(t--) //while testcases exist
44+
{
45+
int a,b;
46+
cin>>a>>b; //input a and b
47+
48+
cout<<countBitsFlip(a, b)<<endl;
49+
}
50+
return 0;
51+
} // } Driver Code Ends
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
2+
/*
3+
You are given a number N. Find the total count of set bits for all numbers from 1 to N(both inclusive).
4+
5+
Example 1:
6+
7+
Input: N = 4
8+
Output: 5
9+
Explanation:
10+
For numbers from 1 to 4.
11+
For 1: 0 0 1 = 1 set bits
12+
For 2: 0 1 0 = 1 set bits
13+
For 3: 0 1 1 = 2 set bits
14+
For 4: 1 0 0 = 1 set bits
15+
Therefore, the total set bits is 5.*/
16+
17+
#include<bits/stdc++.h>
18+
using namespace std;
19+
20+
// Function to count set bits in the given number x
21+
// n: input to count the number of set bits
22+
int largestpowerof2inrange(int n)
23+
{
24+
int x=0;
25+
while((1<<x) <=n)
26+
{
27+
x++;
28+
}
29+
return x-1;
30+
}
31+
int countSetBits(int n)
32+
{
33+
// Your logic
34+
if(n==0) return 0;
35+
int x=largestpowerof2inrange(n);
36+
int first= x*(1<<(x-1));
37+
int second= n-(1<<x)+1;
38+
int third=n-(1<<x);
39+
int ans=first+second+countSetBits(third);
40+
return ans;
41+
}
42+
43+
// Driver code
44+
int main()
45+
{
46+
int t;
47+
cin>>t;// input testcases
48+
while(t--) //while testcases exist
49+
{
50+
int n;
51+
cin>>n; //input n
52+
53+
cout << countSetBits(n) << endl;// print the answer
54+
}
55+
return 0;
56+
}
57+
// } Driver Code Ends
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
2+
/*
3+
Given a number N having only one ‘1’ and all other ’0’s in its binary representation, find position of the only set bit. If there are 0 or more than 1 set bit the answer should be -1. Position of set bit '1' should be counted starting with 1 from LSB side in binary representation of the number.
4+
5+
Example 1:
6+
7+
Input:
8+
N = 2
9+
Output:
10+
2
11+
Explanation:
12+
2 is represented as "10" in Binary.
13+
As we see there's only one set bit
14+
and it's in Position 2 and thus the
15+
Output 2.*/
16+
#include <bits/stdc++.h>
17+
using namespace std;
18+
19+
20+
class Solution {
21+
public:
22+
23+
int isPowerOfTwo(unsigned N)
24+
{
25+
return N && (!(N & (N - 1)));
26+
}
27+
int findPosition(int N) {
28+
// code here
29+
if (!isPowerOfTwo(N))
30+
return -1;
31+
32+
unsigned i = 1, pos = 1;
33+
34+
35+
while (!(i & N)) {
36+
37+
i = i << 1;
38+
39+
// increment position
40+
++pos;
41+
}
42+
43+
return pos;
44+
45+
}
46+
};
47+
48+
// { Driver Code Starts.
49+
int main() {
50+
int t;
51+
cin >> t;
52+
while (t--) {
53+
int N;
54+
55+
cin>>N;
56+
57+
Solution ob;
58+
cout << ob.findPosition(N) << endl;
59+
}
60+
return 0;
61+
} // } Driver Code Ends

0 commit comments

Comments
 (0)