Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
//Problem Statement :
/*
Given an unsorted array Arr of size N of positive integers. One number 'A' from set {1, 2, …N} is missing and one number 'B' occurs twice in array. Find these two numbers.

Example 1:

Input:
N = 2
Arr[] = {2, 2}
Output: 2 1
Explanation: Repeating number is 2 and
smallest positive missing number is 1.
Example 2:

Input:
N = 3
Arr[] = {1, 3, 3}
Output: 3 2
Explanation: Repeating number is 3 and
smallest positive missing number is 2.
*/

//Function to solve the problem
int *findTwoElement(int *arr, int n) {
int *ans = new int[2];
unordered_map<int, int> mp;
for(int i = 0; i < n; i++)
mp[arr[i]]++ ;


for(int i = 1; i < n + 1; i++)
if(mp[arr[i]] > 1)
ans[0] = arr[i];
// m n
for(int i = 1; i <= n; i++)
if(mp[i] == 0){
ans[1] = i;
break;
}

return ans;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
//Problem Statement :
/*
Given a sorted array arr containing n elements with possibly duplicate elements, the task is to find indexes of first
and last occurrences of an element x in the given array.

Difficulty : Basic
*/


/*
Example 1:
Input:
n=9, x=5
arr[] = { 1, 3, 5, 5, 5, 5, 67, 123, 125 }
Output: 2 5
Explanation: First occurrence of 5 is at index 2 and last occurrence of 5 is at index 5.


Example 2:
Input:
n=9, x=7
arr[] = { 1, 3, 5, 5, 5, 5, 7, 123, 125 }
Output: 6 6


/*
Expected Time Complexity: O(logN)
Expected Auxiliary Space: O(1).
*/


// Solution Explained -
/*
1) Iterate the array from both forward and backward directions with 2 pointers.
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.
3) Return the vector.

*/


//Solution--

// { Driver Code Starts
#include<bits/stdc++.h>
using namespace std;
vector<int> find(int a[], int n , int x );

int main()
{
int t;
cin>>t;
while(t--)
{
int n,x;
cin>>n>>x;
int arr[n],i;
for(i=0;i<n;i++)
cin>>arr[i];
vector<int> ans;
ans=find(arr,n,x);
cout<<ans[0]<<" "<<ans[1]<<endl;
}
return 0;
}
// } Driver Code Ends

vector<int> find(int arr[], int n , int x )
{
int i = 0, j = n -1;
vector<int> v;
while(i <= j){
if(arr[i] != x)
i++ ;

if(arr[j] != x)
j-- ;

if(arr[i] == x && arr[j] == x){
v.push_back(i);
v.push_back(j);
break;
}
}
if(i > j){
v.push_back(-1);
v.push_back(-1);
}

return v;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
//LEETCODE PROBLEM

//Problem Statement :
/*
There is an integer array nums sorted in ascending order (with distinct values).

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
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
pivot index 3 and become [4,5,6,7,0,1,2].

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.

You must write an algorithm with O(log n) runtime complexity.

Difficulty : Medium
*/


/*
Example 1:

Input: nums = [4,5,6,7,0,1,2], target = 0
Output: 4
Example 2:

Input: nums = [4,5,6,7,0,1,2], target = 3
Output: -1
Example 3:

Input: nums = [1], target = 0
Output: -1
*/

//Solution-
class Solution {
public:
int search(vector<int>& nums, int target) {
int s=0;
int e=nums.size()-1;
while(s<=e)
{
int m = s + (e-s)/2;
if(nums[m]==target || nums[s]==target)
{
if(nums[s]==target) return s;

return m;
}
if((target<nums[s]&&target<nums[m])||(target>nums[s]&&target>nums[m]))
{
if(nums[s]>nums[m])
e = m-1;

else
s = m+1;
}
else
{
if(nums[s]>nums[m])
s = m+1;

else
e = m-1;
}
}
return -1;
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
//Problem Statement :
/*
Given an array Arr of N positive integers. Your task is to find the elements whose value
is equal to that of its index value ( Consider 1-based indexing ).

Difficulty : School
*/


/*
Example 1:
Input:
N = 5
Arr[] = {15, 2, 45, 12, 7}
Output: 2
Explanation: Only Arr[2] = 2 exists here.
*/


//Solution Explained
//Easy School level problem. Added just for the sake of sequence...


// Solution-

#include<bits/stdc++.h>
using namespace std;

class Solution{
public:
vector<int> valueEqualToIndex(int arr[], int n) {
int i = 0;
vector<int> v;
while(i <= n){
if(arr[i] == i+1){
v.push_back(arr[i]);
i++ ;
}
else
i++ ;
}
return v;
}
};

// { Driver Code Starts.
int main() {
int t;
cin >> t;
while (t--) {
int n, i;
cin >> n;
int arr[n];
for (i = 0; i < n; i++) {
cin >> arr[i];
}
Solution ob;
auto ans = ob.valueEqualToIndex(arr, n);
if (ans.empty()) {
cout << "Not Found";
} else {
for (int x: ans) {
cout << x << " ";
}
}
cout << "\n";
}
return 0;
}
// } Driver Code Ends