Skip to content

Commit 02ea338

Browse files
Merge branch 'smv1999:master' into issue#439
2 parents 72fae01 + 1d832a5 commit 02ea338

8 files changed

+572
-0
lines changed
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/* Given two sorted arrays, arr1[] and arr2[], the task is to find the
2+
median of these sorted arrays different size. */
3+
4+
#include<bits/stdc++.h>
5+
using namespace std;
6+
7+
/* Function to find Median of two Sorted array of different size */
8+
int solve()
9+
{
10+
/* Input n1 = size of array 1
11+
Input n2 = size of array 2 */
12+
int n1,n2;
13+
cout<<"Accept size of 1st Array : ";
14+
cin >> n1 ;
15+
cout<<"Accept size of 2nd Array : ";
16+
cin>>n2;
17+
18+
int arr1[n1],arr2[n2];
19+
20+
/* Input values in array 1 */
21+
cout<<"Accept the values of 1st Array : ";
22+
for (int i = 0; i < n1; ++i)
23+
{
24+
cin >> arr1[i];
25+
}
26+
27+
/* Input values in array 2 */
28+
cout<<"Accept the values of 2nd Array : ";
29+
for (int i = 0; i < n2; ++i)
30+
{
31+
cin >> arr2[i];
32+
}
33+
34+
/* Create a vector */
35+
vector<int> v;
36+
37+
/* Push array 1 element in vector */
38+
for (int i = 0; i < n1; ++i)
39+
{
40+
v.push_back(arr1[i]);
41+
}
42+
43+
/* Push array 2 element in vector */
44+
for (int i = 0; i < n2; ++i)
45+
{
46+
v.push_back(arr2[i]);
47+
}
48+
49+
/* Sort the given vector */
50+
sort(v.begin(), v.end());
51+
52+
/* Find medina using this formula */
53+
int mid = v[v.size()/2];
54+
55+
/* Return Calculated median */
56+
return mid;
57+
}
58+
59+
int main()
60+
{
61+
int Median = solve();
62+
63+
cout << "Median is : " << Median <<endl;
64+
return 0;
65+
}
66+
67+
/*
68+
Test cases :
69+
70+
Input :
71+
Accept size of 1st Array : 1
72+
Accept size of 2nd Array : 4
73+
Accept the values of 1st Array : 900
74+
Accept the values of 2nd Array : 5 8 10 20
75+
76+
Output :
77+
Median is : 10
78+
79+
Time complexity: O(nlogn)
80+
Space Complexity: O(n)
81+
*/

Arrays/PeakElement.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import java.io.*;
2+
public class PeakElement {
3+
// TC : O(logn)
4+
// SC : O(1)
5+
public static void main(String args[])throws IOException
6+
{
7+
InputStreamReader read=new InputStreamReader(System.in);
8+
BufferedReader in=new BufferedReader(read);
9+
int n,i;
10+
System.out.println("Enter the size of the array");
11+
n=Integer.parseInt(in.readLine());
12+
int nums[]=new int[n];
13+
System.out.println("Enter the elements of the array");
14+
for(i=0;i<n;i++)
15+
{
16+
nums[i]=Integer.parseInt(in.readLine());
17+
}
18+
int low = 0;
19+
int high = n-1;
20+
21+
while(low< high){
22+
int mid = low + (high-low)/2;
23+
if(nums[mid]<nums[mid+1]){
24+
low = mid + 1;
25+
} else {
26+
high = mid;
27+
}
28+
}
29+
System.out.println("The Peak Element is = "+nums[low]);
30+
}
31+
}

Arrays/containerWithMostWater.cpp

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
Given n non-negative integers a1, a2, ..., an , where each represents a point at coordinate (i, ai). n
3+
vertical lines are drawn such that the two endpoints of the line i is at (i, ai) and (i, 0).
4+
Find two lines, which, together with the x-axis forms a container, such that the container contains the most water.
5+
6+
Notice that you may not slant the container.
7+
8+
9+
Input: height = [1,8,6,2,5,4,8,3,7]
10+
Output: 49
11+
Explanation: The above vertical lines are represented by array [1,8,6,2,5,4,8,3,7].
12+
In this case, the max area of water the container can contain is 49.
13+
14+
Constraints:
15+
n == height.length
16+
2 <= n <= 105
17+
0 <= height[i] <= 104
18+
19+
*/
20+
21+
/*
22+
Time Complexity: O(N), where N is the size of the array
23+
Space Complexity: O(1), Constant Space
24+
*/
25+
26+
#include <bits/stdc++.h>
27+
using namespace std;
28+
29+
int maxArea(vector<int> &height)
30+
{
31+
int n = height.size();
32+
int i = 0; // one pointer to the start of the array
33+
int j = n - 1; // one poninter to the end of the array
34+
int mx = min(height[i], height[j]) * (j - i); // contains the max value of maximum water that can be stored
35+
while (i < j)
36+
{
37+
if (height[i] < height[j]) // Checking condition if height pointed by i is less than that pointed by j if yes => incrementing i and updating value of mx
38+
{
39+
i++;
40+
mx = max(mx, (min(height[i], height[j]) * (j - i)));
41+
}
42+
else // otherwise => decrementing j and updating value of mx
43+
{
44+
j--;
45+
mx = max(mx, (min(height[i], height[j]) * (j - i)));
46+
}
47+
}
48+
return mx;
49+
}
50+
51+
int main() // driver function
52+
{
53+
vector<int> heights = {1,8,6,2,5,4,8,3,7};
54+
int ans = maxArea(heights);
55+
cout<<ans;
56+
return 0;
57+
}

Arrays/searchInRotatedSortedArray.cpp

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*
2+
There is an integer array nums sorted in ascending order (with distinct values).
3+
4+
Prior to being passed to your function, nums is rotated at an unknown pivot index k (0 <= k < nums.length) such that
5+
the resulting array is [nums[k], nums[k+1], ..., nums[n-1], nums[0], nums[1], ..., nums[k-1]] (0-indexed).
6+
For example, [0,1,2,4,5,6,7] might be rotated at pivot index 3 and become [4,5,6,7,0,1,2].
7+
8+
Given the array nums after the rotation and an integer target, return the index of target if it is in nums, or -1
9+
if it is not in nums.
10+
11+
You must write an algorithm with O(log n) runtime complexity.
12+
13+
Input: nums = [4,5,6,7,0,1,2], target = 0
14+
Output: 4
15+
16+
Constraints:
17+
1 <= nums.length <= 5000
18+
-104 <= nums[i] <= 104
19+
All values of nums are unique.
20+
nums is guaranteed to be rotated at some pivot.
21+
-104 <= target <= 104
22+
23+
*/
24+
25+
/*
26+
Time Complexity: O(log(N)), where N is the size of the array
27+
Space Complexity: O(1), Constant Space
28+
*/
29+
30+
#include <bits/stdc++.h>
31+
using namespace std;
32+
33+
int binarySearch(vector<int> arr, int l, int r, int x) // Function to perform Binary Search
34+
{
35+
while(l<=r)
36+
{
37+
int m=(l+r)/2;
38+
if(arr[m]==x)
39+
return m;
40+
else if(arr[m]<x)
41+
l=m+1;
42+
else
43+
r=m-1;
44+
}
45+
return -1;
46+
}
47+
int search(const vector<int> &A, int B)
48+
{
49+
int l=0; // lower bound
50+
int n=A.size();
51+
int u=n-1; // upper bound
52+
int index=-1; // to know the index upto which array is sorted i.e left,right of this will be sorted array
53+
while(l<=u)
54+
{
55+
int m=(l+u)/2;
56+
int prev=(m-1+n)%n;
57+
int next=(m+1)%n;
58+
if(A[l]<=A[u]) // If array is already sorted return lower bound
59+
{
60+
index=l;
61+
break;
62+
}
63+
if(A[m]<=A[prev]&&A[m]<=A[next]) // if value at middle is less than or equal to value at previous and value at middle is also less than or equal to value at next => we have found our match
64+
{
65+
index=m;
66+
break;
67+
}
68+
else if(A[l]<=A[m]) // if value at lower bound is less than value at middle then shift lower bound
69+
l=next;
70+
else// if above condition is not true shift upper bound
71+
u=prev;
72+
}
73+
int i1=binarySearch(A,0,index-1,B); // performing binary search in either halves of the sorted array
74+
if(i1!=-1)
75+
return i1;
76+
i1=binarySearch(A,index,n-1,B);
77+
return i1;
78+
}
79+
80+
int main() // driver function
81+
{
82+
vector<int> array = {4,5,6,7,0,1,2};
83+
int ans = search(array,0);
84+
cout << ans;
85+
return 0;
86+
}

Arrays/sum_of_all_subarrays.cpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/* Q. Given an integer array ‘arr[]’ of size n, find sum of all sub-arrays of given array.
2+
Input : a[] = {1, 2, 3}
3+
Output: 20
4+
5+
Explanation: We have to generate all the subarrays using two for loops and then compute their sum.
6+
subarrays --> {1} {12} {123} {2} {23} {3}
7+
subarrays sum --> 1 + 3 + 6 + 2 + 5 + 3 --> 20
8+
9+
Time Complexity: O(n^2)
10+
*/
11+
12+
#include <iostream>
13+
using namespace std;
14+
int main()
15+
{
16+
int n, sum=0, result = 0;
17+
cin>>n;
18+
int a[n];
19+
for( int i=0; i<n; i++)
20+
{
21+
cin>>a[i];
22+
}
23+
24+
for( int i=0; i<n; i++)
25+
{
26+
sum = 0;
27+
for( int j=i; j<n; j++)
28+
{
29+
sum = sum + a[j];
30+
result = result + sum;
31+
}
32+
}
33+
34+
cout<<"Sum of all Subarrays : "<<result;
35+
36+
return 0;
37+
}

0 commit comments

Comments
 (0)