Skip to content

Commit bb505bd

Browse files
🎯Added cpp solution in searching and sorting. (#384)
* 🎯Added cpp solution Q1 in searching and sorting. 🌱Added the cpp solution passing all the test cases as of 15-01-2022 for the problem - "Find first and last positions of an element in a sorted array." in searching and sorting section of DSA-sheet by Love Babbar. * 🎯Added cpp solution Q2 in searching and sorting. 🌱Added the cpp solution passing all the test cases as of 15-01-2022 for the problem_2 - "Find a fixed point(value equal to index) in a given array" in the searching and sorting section of DSA-sheet by Love Babbar. * 🎯Added cpp solution Q3 in searching and sorting. 🌱Added the cpp solution passing all the test cases as of 15-01-2022 for the problem - "Search in a rotated sorted array." in the searching and sorting section of the DSA-sheet by Love Babbar.
1 parent 55f949e commit bb505bd

File tree

3 files changed

+228
-0
lines changed

3 files changed

+228
-0
lines changed
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
//Problem Statement :
2+
/*
3+
Given a sorted array arr containing n elements with possibly duplicate elements, the task is to find indexes of first
4+
and last occurrences of an element x in the given array.
5+
6+
Difficulty : Basic
7+
*/
8+
9+
10+
/*
11+
Example 1:
12+
Input:
13+
n=9, x=5
14+
arr[] = { 1, 3, 5, 5, 5, 5, 67, 123, 125 }
15+
Output: 2 5
16+
Explanation: First occurrence of 5 is at index 2 and last occurrence of 5 is at index 5.
17+
18+
19+
Example 2:
20+
Input:
21+
n=9, x=7
22+
arr[] = { 1, 3, 5, 5, 5, 5, 7, 123, 125 }
23+
Output: 6 6
24+
25+
26+
/*
27+
Expected Time Complexity: O(logN)
28+
Expected Auxiliary Space: O(1).
29+
*/
30+
31+
32+
// Solution Explained -
33+
/*
34+
1) Iterate the array from both forward and backward directions with 2 pointers.
35+
2) As soon as we find the required element in the array we will push_back the respective indices(values of those 2 pointers) in a vector.
36+
3) Return the vector.
37+
38+
*/
39+
40+
41+
//Solution--
42+
43+
// { Driver Code Starts
44+
#include<bits/stdc++.h>
45+
using namespace std;
46+
vector<int> find(int a[], int n , int x );
47+
48+
int main()
49+
{
50+
int t;
51+
cin>>t;
52+
while(t--)
53+
{
54+
int n,x;
55+
cin>>n>>x;
56+
int arr[n],i;
57+
for(i=0;i<n;i++)
58+
cin>>arr[i];
59+
vector<int> ans;
60+
ans=find(arr,n,x);
61+
cout<<ans[0]<<" "<<ans[1]<<endl;
62+
}
63+
return 0;
64+
}
65+
// } Driver Code Ends
66+
67+
vector<int> find(int arr[], int n , int x )
68+
{
69+
int i = 0, j = n -1;
70+
vector<int> v;
71+
while(i <= j){
72+
if(arr[i] != x)
73+
i++ ;
74+
75+
if(arr[j] != x)
76+
j-- ;
77+
78+
if(arr[i] == x && arr[j] == x){
79+
v.push_back(i);
80+
v.push_back(j);
81+
break;
82+
}
83+
}
84+
if(i > j){
85+
v.push_back(-1);
86+
v.push_back(-1);
87+
}
88+
89+
return v;
90+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
//LEETCODE PROBLEM
2+
3+
//Problem Statement :
4+
/*
5+
There is an integer array nums sorted in ascending order (with distinct values).
6+
7+
Prior to being passed to your function, nums is possibly rotated at an unknown pivot index k (1 <= k < nums.length) such that the resulting
8+
array is [nums[k], nums[k+1], ..., nums[n-1], nums[0], nums[1], ..., nums[k-1]] (0-indexed). For example, [0,1,2,4,5,6,7] might be rotated at
9+
pivot index 3 and become [4,5,6,7,0,1,2].
10+
11+
Given the array nums after the possible rotation and an integer target, return the index of target if it is in nums, or -1 if it is not in nums.
12+
13+
You must write an algorithm with O(log n) runtime complexity.
14+
15+
Difficulty : Medium
16+
*/
17+
18+
19+
/*
20+
Example 1:
21+
22+
Input: nums = [4,5,6,7,0,1,2], target = 0
23+
Output: 4
24+
Example 2:
25+
26+
Input: nums = [4,5,6,7,0,1,2], target = 3
27+
Output: -1
28+
Example 3:
29+
30+
Input: nums = [1], target = 0
31+
Output: -1
32+
*/
33+
34+
//Solution-
35+
class Solution {
36+
public:
37+
int search(vector<int>& nums, int target) {
38+
int s=0;
39+
int e=nums.size()-1;
40+
while(s<=e)
41+
{
42+
int m = s + (e-s)/2;
43+
if(nums[m]==target || nums[s]==target)
44+
{
45+
if(nums[s]==target) return s;
46+
47+
return m;
48+
}
49+
if((target<nums[s]&&target<nums[m])||(target>nums[s]&&target>nums[m]))
50+
{
51+
if(nums[s]>nums[m])
52+
e = m-1;
53+
54+
else
55+
s = m+1;
56+
}
57+
else
58+
{
59+
if(nums[s]>nums[m])
60+
s = m+1;
61+
62+
else
63+
e = m-1;
64+
}
65+
}
66+
return -1;
67+
}
68+
};
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
//Problem Statement :
2+
/*
3+
Given an array Arr of N positive integers. Your task is to find the elements whose value
4+
is equal to that of its index value ( Consider 1-based indexing ).
5+
6+
Difficulty : School
7+
*/
8+
9+
10+
/*
11+
Example 1:
12+
Input:
13+
N = 5
14+
Arr[] = {15, 2, 45, 12, 7}
15+
Output: 2
16+
Explanation: Only Arr[2] = 2 exists here.
17+
*/
18+
19+
20+
//Solution Explained
21+
//Easy School level problem. Added just for the sake of sequence...
22+
23+
24+
// Solution-
25+
26+
#include<bits/stdc++.h>
27+
using namespace std;
28+
29+
class Solution{
30+
public:
31+
vector<int> valueEqualToIndex(int arr[], int n) {
32+
int i = 0;
33+
vector<int> v;
34+
while(i <= n){
35+
if(arr[i] == i+1){
36+
v.push_back(arr[i]);
37+
i++ ;
38+
}
39+
else
40+
i++ ;
41+
}
42+
return v;
43+
}
44+
};
45+
46+
// { Driver Code Starts.
47+
int main() {
48+
int t;
49+
cin >> t;
50+
while (t--) {
51+
int n, i;
52+
cin >> n;
53+
int arr[n];
54+
for (i = 0; i < n; i++) {
55+
cin >> arr[i];
56+
}
57+
Solution ob;
58+
auto ans = ob.valueEqualToIndex(arr, n);
59+
if (ans.empty()) {
60+
cout << "Not Found";
61+
} else {
62+
for (int x: ans) {
63+
cout << x << " ";
64+
}
65+
}
66+
cout << "\n";
67+
}
68+
return 0;
69+
}
70+
// } Driver Code Ends

0 commit comments

Comments
 (0)