Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Kartik Purohit CST assignment (1,2,3) #111

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added 11-05-2020/Ques1.JPG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
31 changes: 31 additions & 0 deletions 11-05-2020/Ques1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
Question 1:
Single Number
Given an array of integers, every element appears twice except for one. Find that single
one. Note: Your algorithm should have a linear runtime complexity. Could you implement it without
using extra memory?
*/
#include <iostream>
#include <bits/stdc++.h>
using namespace std;

int main()
{ vector<int> a;
int n,p,e=0;
cout<<"Enter the no. Of Element In array:"<<endl;
cin>>n;
cout<<"Enter an Array"<<endl;
for(int i =0;i<n;i++)
{
cout<<"Enter Element:";
cin>>p;
a.push_back(p);
}

for (int i = 0;i<n;i++)
{
e = e^a[i];
}
cout<<"The Single One:"<<e<<endl;
}

Binary file added 11-05-2020/Ques2.JPG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
38 changes: 38 additions & 0 deletions 11-05-2020/Ques2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
Square Root of Integer
Given an integar A. Compute and return the square root of A.
If A is not a perfect square, return floor(sqrt(A)).

*/
#include <iostream>
#include<math.h>
using namespace std;

int main()
{
int n;
cout<<"Enter the number:";
cin>>n;
float h = n;
float l = 1;
float mid;

for(int i = 1;i<n;i++)
{
mid = (l+h)/2;
if(mid*mid > n)
{
h = mid-1;
}
else
{if(mid*mid < n)
{
l = mid+1;
}
}
}
cout<<"Using B search:"<<mid<<endl;

cout<<"Using sqrt fun:"<<sqrt(n);
}

Binary file added 11-05-2020/Ques3.JPG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
22 changes: 22 additions & 0 deletions 11-05-2020/Ques3.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*Question 3:
Maximum height of the staircase
Given an integer A representing the square blocks. The height of each square block is 1.
The task is to create a staircase of max height using these blocks. The first stair would
require only one block, the second stair would require two blocks
and so on. Find and return the maximum height of the staircase

*/
#include<iostream>
#include<bits/stdc++.h>
#include<math.h>
using namespace std;
int main(){
int n;
cout<<"Enter the no. of Blocks:"<<endl;
cin>>n;
int x;
x= (-1+sqrt(1+8*n))/2;
cout<<"The Height is "<<x<<endl;

}

Binary file added 11-05-2020/Ques4.JPG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
53 changes: 53 additions & 0 deletions 11-05-2020/Ques4.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*

Count of paths in a grid
Given an integer A, find and return the number of paths in a grid of size (A x A) that
starts from (1, 1) and reaches (A, A) without crossing the major diagonal. Since the result can be large, return the result modulo (10^9 + 7)
*/

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

int main()
{
int r;
cout<<"Enter A:"<<endl;
cin>>r;
vector<vector<int>> a(r ,vector<int>(r,0));
cout<<"Array:"<<endl;
for(int i =0;i<a.size();i++)
{ for(int j =0;j<a[i].size();j++)
{
cout<<" "<<a[i][j];
}
cout<<endl;
}
for(int i =1;i<a.size();i++){
a[i][0]=1;
}

for(int i =1;i<a.size();i++)
{
for(int j =1;j<a.size();j++)
{
if (i<j)
{
continue;
}
else{
a[i][j] = a[i-1][j] + a[i][j-1];
}
}
}
cout<<"Array:"<<endl;
for(int i =0;i<a.size();i++)
{ for(int j =0;j<a[i].size();j++)
{
cout<<" "<<a[i][j];
}
cout<<endl;
}
cout<<"\n\nTotal no. of path"<<a[a.size()-1][a.size()-1]<<endl;

}
Binary file added 11-05-2020/Ques5.JPG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
40 changes: 40 additions & 0 deletions 11-05-2020/Ques5.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
Given an integer array A of N integers, find the pair of integers in the array which have
minimum XOR value. Report the minimum XOR value. You have an array A[] with N elements. We have 2 types of operation available on this
array :
. We can split a element B into 2 elements C and D such that B = C + D.
. We can merge 2 elements P and Q as one element R such that R = P^Q i.e XOR of P
and Q. You have to determine whether it is possible to make array A[] containing only 1
element 0 after several splits and/or merge?
*/

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

int main()
{ vector<int> a;
int n,p;
cout<<"Enter the no. Of Element In array:"<<endl;
cin>>n;
cout<<"Enter an Array"<<endl;
for(int i =0;i<n;i++)
{
cout<<"Enter Element:";
cin>>p;
a.push_back(p);
}


int i,s,m=INT_MAX;
sort(a.begin(),a.end());
for(int i =0;i<a.size();i++)
{
s=a[i]^a[i-1];
if(m>s)
m=s;
}
cout<<"Min X-or Value:"<<m<<endl;

}

Binary file added 12-05-2020/Q1_gcd.JPG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
33 changes: 33 additions & 0 deletions 12-05-2020/Q1_gcd.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
//The logic behind it is GCD of consecutive numbers is always 1 but if numbers are equal return number
// if(A==B)
// return A;
// else
// return "1";
//GCD OF A AND B
#include<iostream>
using namespace std;
int gcd(int a,int b)
{
int q,r;
q=a/b;
r=a%b;
if (r==0){
return b;
}
else{
gcd(b,r);
}
}
int main(){
int x,y,a,b;
cout<<"Enter the Numbers For GCD:";
cin>>x>>y;
cout<<endl;
a=(x>y?x:y);
b=((x+y)-a);
cout<<"Greatest divisor that divides both "<<x<<" and "<<y<< " is " <<(b==1?1:gcd(a,b));
return 0;
}



Binary file added 12-05-2020/Q2_ABC.JPG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
36 changes: 36 additions & 0 deletions 12-05-2020/Q2_ABC.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*

Scooby has 3 three integers A, B and C.

Scooby calls a positive integer special if it is divisible by B and it is divisible by C. You need to tell number of special integers less than or equal to A.


*/

#include<iostream>
using namespace std;
int gcd(int a,int b)
{
int q,r;
q=a/b;
r=a%b;
if (r==0){
return b;
}
else{
gcd(b,r);
}
}

int main(){

int a,b,c;
cout<<"Enter A:";
cin>>a;
cout<<"\nEnter B:";
cin>>b;
cout<<"\nEnter C:";
cin>>c;
int lcm=(b*c)/gcd(b,c);
cout<<"Total nos between range 1 to "<<a<<" that are divisible by "<<b<<" and "<<c<<" are:"<<a/lcm;
}
Binary file added 13-05-2020/Q1_3.JPG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
29 changes: 29 additions & 0 deletions 13-05-2020/Q1_3.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
You have an array lets say a vector or a static array
that arrays contains some numbers.You have to remove minimum number of
elements from array such that sum of adjacent elements is always even

*/
#include<bits/stdc++.h>
using namespace std;
int main(){
int n;
cout<<"How May Array elements:"<<endl;
cin>>n;
vector<int> A(n);
cout<<"Enter an Array"<<endl;
for(int i =0;i<A.size();i++)
{
cout<<"Enter Element:";
cin>>A[i];
cout<<endl;
}
int i,o=0,e=0;
for(i=0;i<A.size();++i){
if(A[i]%2==0)
++e;
else
++o;
}
cout<<"Minimum number of elements to be removed is "<<min(o,e)<<endl;
}
Binary file added 13-05-2020/Q2_3.JPG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
32 changes: 32 additions & 0 deletions 13-05-2020/Q2_3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#For an given array,count the number of subarrays with even number only.

def even(l):
f = False
for i in l:
if i % 2 == 0:
f = True
else:
f = False
break
return f

n = int(input("Enter No. of elements:"))
l = []
print("Enter Array:")
for i in range(n):
l.append(int(input("Enter Elements:")))

print(l)
sub_arr = []
for i in range(0, n):
for j in range(i, n):
temp = []
for k in range(i, j + 1):
temp += [l[k]]
sub_arr += [temp]

sub_arr_even = []
sub_arr_even += list(filter(even, sub_arr))

print("Sub Arrays:", sub_arr_even)
print("Total:", len(sub_arr_even))
Binary file added 13-05-2020/Q3_3.JPG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
30 changes: 30 additions & 0 deletions 13-05-2020/Q3_3.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//GCD OF A AND B + LCM

#include<iostream>
using namespace std;
int gcd(int a,int b)
{
int q,r;
q=a/b;
r=a%b;
if (r==0){
return b;
}
else{
gcd(b,r);
}
}
int main(){
int x,y,a,b;
cout<<"Enter the Numbers For GCD:";
cin>>x>>y;
cout<<endl;
a=(x>y?x:y);
b=((x+y)-a);
int gd = (b==1?1:gcd(a,b));
cout<<"Greatest divisor that divides both "<<x<<" and "<<y<< " is " <<gd;
cout<<"\nLCM IS:"<<(a*b)/gd;
return 0;
}


Binary file added 13-05-2020/Q4_3.JPG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading