Skip to content

Commit 9188d03

Browse files
authored
Merge pull request thepranaygupta#328 from krishna6431/main
fixes thepranaygupta#317
2 parents 4ae828a + 9ae77f3 commit 9188d03

File tree

3 files changed

+252
-18
lines changed

3 files changed

+252
-18
lines changed

02. Algorithms/06. Number Theory/Sieve Of Eratosthenes/Nth_Natural_No.cpp renamed to 02. Algorithms/06. Number Theory/Nth_Natural_Number/Nth_Natural_Number.cpp

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1-
21
//Problem Statement
32
//Given a positive integer N. You have to find Nth natural number after removing all the numbers containing digit 9.
43

54

65
/*
76
Example 1:
8-
97
Input:
108
N = 8
119
Output:
@@ -14,7 +12,6 @@ After removing natural numbers which contains
1412
digit 9, first 8 numbers are 1,2,3,4,5,6,7,8
1513
and 8th number is 8.
1614
Example 2:
17-
1815
Input:
1916
N = 9
2017
Output:
@@ -24,42 +21,27 @@ After removing natural numbers which contains
2421
digit 9, first 9 numbers are 1,2,3,4,5,6,7,8,10
2522
and 9th number is 10.
2623
27-
2824
Your Task:
2925
You don't need to read input or print anything. Complete the function findNth() which accepts an integer N as input parameter and return the Nth number after removing all the numbers containing digit 9.
30-
31-
3226
Expected Time Complexity: O(logN)
3327
Expected Auxiliary Space: O(1)
34-
35-
3628
Constraints:
3729
1 ≤ N ≤ 10^12
38-
3930
*/
4031

4132
/*
4233
Naive Approach: The simplest approach to solve the above problem is to iterate up to N and keep excluding all numbers less than N containing the digit 9. Finally, print the Nth natural number obtained.
43-
44-
45-
46-
4734
Time Complexity: O(N)
4835
Auxiliary Space: O(1)
4936
*/
5037

5138
/*
5239
Efficient Approach: The above approach can be optimized based on the following observations:
53-
54-
5540
It is known that, digits of base 2 numbers varies from 0 to 1. Similarly, digits of base 10 numbers varies from 0 to 9.
5641
Therefore, the digits of base 9 numbers will vary from 0 to 8.
5742
It can be observed that Nth number in base 9 is equal to Nth number after skipping numbers containing digit 9.
5843
So the task is reduced to find the base 9 equivalent of the number N.
59-
6044
Follow the steps below to solve the problem:
61-
62-
6345
Initialize two variables, say res = 0 and p = 1, to store the number in base 9 and to store the position of a digit.
6446
Iterate while N is greater than 0 and perform the following operations:
6547
Update res as res = res + p*(N%9).
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
//Problem Statement
2+
//Given an even number N (greater than 2), return two prime numbers whose sum will be equal to given number. There are several combinations possible. Print only the pair whose minimum value is the smallest among all the minimum values of pairs.
3+
4+
5+
/*
6+
Example 1:
7+
8+
Input: N = 74
9+
Output: 3 71
10+
Explaination: There are several possibilities
11+
like 37 37. But the minimum value of this pair
12+
is 3 which is smallest among all possible
13+
minimum values of all the pairs.
14+
Example 2:
15+
16+
Input: 4
17+
Output: 2 2
18+
Explaination: This is the only possible
19+
prtitioning of 4.
20+
21+
our Task:
22+
You do not need to read input or print anything. Your task is to complete the function primeDivision() which takes N as input parameter and returns the partition satisfying the condition.
23+
24+
Expected Time Complexity: O(N*log(logN))
25+
Expected Auxiliary Space: O(N)
26+
27+
Constraints:
28+
1 ≤ N ≤ 104
29+
*/
30+
31+
//Implementation
32+
33+
#include <bits/stdc++.h>
34+
using namespace std;
35+
36+
class Solution{
37+
public:
38+
vector<int> primeDivision(int n){
39+
vector<int>ans;
40+
vector<int>prime_no;
41+
bool prime[n+1]={0};
42+
int i,j;
43+
for(i=2;i*i<=n;i++){
44+
if(prime[i]==0){
45+
for(j=i*i;j<=n;j+=i){
46+
prime[j]=1;
47+
}
48+
}
49+
}
50+
for(i=2;i<=n;i++){
51+
if(prime[i]==0){
52+
prime_no.push_back(i);
53+
}
54+
}
55+
for(i=0;i<prime_no.size();i++){
56+
int temp=prime_no[i];
57+
for(j=prime_no.size();j>=i;j--){
58+
if(n-temp==prime_no[j]){
59+
ans.push_back(temp);
60+
ans.push_back(prime_no[j]);
61+
break;
62+
}
63+
}
64+
65+
}
66+
return ans;
67+
68+
}
69+
70+
71+
};
72+
73+
int main(){
74+
int t;
75+
cin>>t;
76+
while(t--){
77+
int N;
78+
cin>>N;
79+
80+
Solution ob;
81+
vector<int> ans = ob.primeDivision(N);
82+
cout<<ans[0]<<" "<<ans[1]<<"\n";
83+
}
84+
return 0;
85+
}
86+
87+
//Code is Contributed By krishna_6431
Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
//Problem Statement
2+
//Given two sorted arrays arr1[] and arr2[] of sizes n and m in non-decreasing order. Merge them in sorted order without using any extra space. Modify arr1 so that it contains the first N elements and modify arr2 so that it contains the last M elements.
3+
4+
5+
/*
6+
Example 1:
7+
8+
Input:
9+
n = 4, arr1[] = [1 3 5 7]
10+
m = 5, arr2[] = [0 2 6 8 9]
11+
Output:
12+
arr1[] = [0 1 2 3]
13+
arr2[] = [5 6 7 8 9]
14+
Explanation:
15+
After merging the two
16+
non-decreasing arrays, we get,
17+
0 1 2 3 5 6 7 8 9.
18+
Example 2:
19+
20+
Input:
21+
n = 2, arr1[] = [10, 12]
22+
m = 3, arr2[] = [5 18 20]
23+
Output:
24+
arr1[] = [5 10]
25+
arr2[] = [12 18 20]
26+
Explanation:
27+
After merging two sorted arrays
28+
we get 5 10 12 18 20.
29+
30+
31+
Your Task:
32+
You don't need to read input or print anything. You only need to complete the function merge() that takes arr1, arr2, n and m as input parameters and modifies them in-place so that they look like the sorted merged array when concatenated.
33+
34+
35+
Expected Time Complexity: O((n+m) log(n+m))
36+
Expected Auxilliary Space: O(1)
37+
38+
39+
Constraints:
40+
1 <= n, m <= 5*104
41+
0 <= arr1i, arr2i <= 107
42+
*/
43+
44+
/*
45+
1. Method 1
46+
47+
48+
49+
50+
Algorithm:
51+
52+
53+
1) Iterate through every element of ar2[] starting from last
54+
55+
element. Do following for every element ar2[i]
56+
57+
a) Store last element of ar1[i]: last = ar1[i]
58+
59+
b) Loop from last element of ar1[] while element ar1[j] is
60+
61+
greater than ar2[i].
62+
63+
ar1[j+1] = ar1[j] // Move element one position ahead
64+
65+
j--
66+
67+
c) If any element of ar1[] was moved or (j != m-1)
68+
69+
ar1[j+1] = ar2[i]
70+
71+
ar2[i] = last
72+
73+
In above loop, elements in ar1[] and ar2[] are always kept sorted.
74+
75+
76+
*/
77+
78+
/*
79+
Method 2:
80+
81+
82+
83+
84+
The solution can be further optimized by observing that while traversing the two sorted arrays parallelly, if we encounter the jth second array element is smaller than ith first array element, then jth element is to be included and replace some kth element in the first array. This observation helps us with the following algorithm
85+
86+
87+
88+
89+
Algorithm
90+
91+
92+
1) Initialize i,j,k as 0,0,n-1 where n is size of arr1
93+
94+
2) Iterate through every element of arr1 and arr2 using two pointers i and j respectively
95+
96+
if arr1[i] is less than arr2[j]
97+
98+
increment i
99+
100+
else
101+
102+
swap the arr2[j] and arr1[k]
103+
104+
increment j and decrement k
105+
106+
107+
108+
3) Sort both arr1 and arr2
109+
*/
110+
/*
111+
Method 3:
112+
113+
114+
115+
116+
Algorithm:
117+
118+
119+
1) Initialize i with 0
120+
121+
2) Iterate while loop until last element of array 1 is greater than first element of array 2
122+
123+
if arr1[i] greater than first element of arr2
124+
125+
swap arr1[i] with arr2[0]
126+
127+
sort arr2
128+
129+
incrementing i
130+
*/
131+
132+
//Implementation
133+
#include<bits/stdc++.h>
134+
using namespace std;
135+
class Solution{
136+
public:
137+
long long findNth(long long N)
138+
{
139+
long long ans=0;
140+
long long temp = 1;
141+
long long n = N;
142+
while(n>0){
143+
ans += (temp*(n%9));
144+
n=n/9;
145+
temp = temp*10;
146+
}
147+
return ans;
148+
}
149+
};
150+
151+
int main()
152+
{
153+
int t;
154+
cin>>t;
155+
while(t--)
156+
{
157+
long long n , ans;
158+
cin>>n;
159+
Solution obj;
160+
ans = obj.findNth(n);
161+
cout<<ans<<endl;
162+
}
163+
}
164+
165+
//code is contributed by krishna_6431

0 commit comments

Comments
 (0)