Algorithm Design And Analysis(ADA)
Searching Techniques:
SORTING TECHNIQUES:
DIVIDE AND CONQUER:
-
Question Name: Missing Soldiers
Question Name: The Enlightened Ones
Question Name: SHERLOCK AND THE BEAST
Question Name: HUNGER GAMES
Question Name: MARK & TOYS
Greedy Algorithm:
- Question Name:LITTLE JHOOL & HIS PUNISHMENT
- Question Name:ADD-SUBTRACT
- Question Name:EXPLORING RUINS
- Question Name:FLIP THE WORLD
- Question Name:My girlfriend and her love for cats
- Question Name:Minimum and Maximum
- Question Name:GREEDY FLORIST
- Question Name:EASY STRONG PERMUTATION
- Question Name:Missing Soldiers
- Question Name:Rank List
- Question Name:DORSPLEN
- Question Name:PRIYANKA AND TOYS
- Question Name:LET’S BEGIN!
- Question Name:RHEZO & PRIME PROBLEMS
- Question Name:LEAF & LIME LIGHT ATTACK
- Question Name:CHANDU & CONSECUTIVE LETTERS
- Question Name:MAX MIN
- Question Name:BEAUTIFUL PAIRS
- Question Name:GRID CHALLENGE
- Question Name:SAMU & SHOPPING
- Question Name:Little Deepu and his Girlfriend
- Question Name:MILLY & CHOCOLATES
- Question Name:PERMUTING TWO ARRAYS
- Question Name:MAXIMUM PERIMETER TRIANGLE
- Question Name:JUMPING CHAMPA
- Question Name:Stack of Bricks
- Question Name:INTELLIGENT GIRL
- Question Name:Monk and Modulo Based Sorting
- Question Name:Pebbles Game
- Question Name:PERMUTATION OF FIRST N NATURAL NUMBERS
- Question Name:Huffman Coding
- Question Name: JIM AND THE ORDERS
- Question Name:Shell Sort
- Question Name:Kevin doesn’t like his array
- Question Name:INSECT COLONY
- Question Name:ALGORITHMIC CRUSH
- Question Name:The legend of Tanmay
- Question Name:Puchi and Luggage
- Question Name:Address
- Question Name: Xsquare And Number List
- Question Name: Monk meets Dynamic Array
- Question Name:One-Sized Game
- Question Name:Timely Orders
- Question Name:PRIYANKA AND TOYS
- Question Name:Discover the Monk
- Question Name: King’s Race
bhai aur dal do program
ReplyDeleteAur bhai aman programming chlri hai🤙
DeleteThis comment has been removed by the author.
DeleteSORT 8
ReplyDelete#include
#include
using namespace std;
int main()
{
int t,n,m;
cin>>t;
while(t--)
{
cin>>n>>m;
long int c[n+m];
for(int i=0;i>c[i];
sort(c,c+m+n);
for(int i=n+m-1;i>=0;i--)
cout<<c[i]<<" ";
cout<<endl;
}
return 0;
}
sorry...SORT13
Deletei need a program of AR7
ReplyDeleteSESSION: Searching Techniques
ReplyDeleteQ. 6: Student Roll No
QUESTION DESCRIPTION
If she has to 10 students ordered list for the subject DS from 1 to 15.Now she wants to find roll no:5 belongs to her or not?
ANSWER:
#include
int main()
{
int a[10],count=0,i,j,k,arun,raghav;
for(i=0;i<10;i++)
{
scanf("%d",&a[i]);
if(a[i]==5)
{
count++;
}
}
printf("Sorted Rollnumber list is\n");
for(i=0;i<10;i++)
{ raghav=a[i];
for(j=1;j<10;j++)
{
for(k=j-1;k<j;k++)
{
if(a[j]<a[k])
{
arun=a[j];
a[j]=a[k];
a[k]=arun;
}
}
}
}
for(i=0;i<10;i++)
{
printf("%d ",a[i]);
}
if(count==1)
printf("\nRoll no 5 belongs to the list");
else
printf("\nRoll no 5 does not belong to the list");
return 0;
}
Put up LIBRARY program too.
ReplyDeleteIN SEARCH OF SAMOSA : GREEDY ALGORITHM
ReplyDelete#include
using namespace std;
int main() {
int n,k,s=0;
cin>>n>>k;
int a[n];
for(int i=0; i>a[i];
sort(a, a+n);
for(int i=0; i= a[i]) {
k -= a[i];
++s;
} else break;
}
cout<<s;
return 0;
}
Hadd BC
Deleteaadmi h tu ki paijama
DeleteIN SEARCH OF SAMOSA
Deletehttps://deepanshubhatti.blogspot.com/2015/11/in-search-of-samosa.html
This comment has been removed by the author.
ReplyDeleteDynamic Programming . The colorful street
ReplyDelete#include
using namespace std;
int dp[25][3];
int fun(int p[],int o[],int y[],int n,int ind)
{
if(n==0) return 0;
if(dp[n][ind]!=0) return dp[n][ind];
int i=1,ans=INT_MAX;
if(ind==0)
{
ans=min(ans,o[n-1]+fun(p,o,y,n-1,1));
ans=min(ans,y[n-1]+fun(p,o,y,n-1,2));
}
else if(ind==1)
{
ans=min(ans,p[n-1]+fun(p,o,y,n-1,0));
ans=min(ans,y[n-1]+fun(p,o,y,n-1,2));
}
else if(ind==2)
{
ans=min(ans,p[n-1]+fun(p,o,y,n-1,0));
ans=min(ans,o[n-1]+fun(p,o,y,n-1,1));
}
dp[n][ind]=ans;
return dp[n][ind];
}
int main()
{
int t;
cin>>t;
while(t--)
{
memset(dp,0,sizeof(dp));
int n,i;
cin>>n;
int p[n],o[n],y[n];
for(i=0;i>p[i]>>o[i]>>y[i];
int ans=INT_MAX;
ans=min(ans,p[n-1]+fun(p,o,y,n-1,0));
ans=min(ans,o[n-1]+fun(p,o,y,n-1,1));
ans=min(ans,y[n-1]+fun(p,o,y,n-1,2));
cout<<ans<<endl;
}
return 0;
}
This comment has been removed by the author.
Deletememset function and INT_MAX constant not declared
DeleteTina's brother gave her a of calculating the number of school in a board that has n*n school board dimensions 1cm *1cm earned. Help her to find the number of total squares including all small or big ones. Answer code please
DeleteReply raghav
DeleteProgram code sent
DeleteSESSION: Randomized algorithm
ReplyDeleteQ. 91: MINMAX 2
QUESTION DESCRIPTION
Given an array a={3, 20,100, 1, 2}, find the minimum and maximum element.
#include
#define MAX_SIZE 100 // Maximum array size
int main()
{
int arr[MAX_SIZE];
int i, max, min, size;
scanf("%d", &size);
for(i=0; i max)
{
max = arr[i];
}
if(arr[i] < min)
{
min = arr[i];
}
}
printf("Minimum : %d", min);
printf("\nMaximum : %d\n", max);
return 0;
}
SESSION: Randomized algorithm
ReplyDeleteQ. 93: RANDOMIZED ALGORITHMS 8
QUESTION DESCRIPTION
Write a function Add() that returns sum of two integers. The function should not use any of the arithmetic operators (+, ++, , -, .. etc).
Sum of two bits can be obtained by performing XOR (^) of the two bits. Carry bit can be obtained by performing AND (&) of two bits.
Above is simple Half Adder logic that can be used to add 2 single bits.
We can extend this logic for integers. If x and y dont have set bits at same position(s), then bitwise XOR (^) of x and y gives the sum of x and y. To incorporate common set bits also, bitwise AND (&) is used. Bitwise AND of x and y gives all carry bits. We calculate (x & y) << 1 and add it to x ^ y to get the required result.
#include
int main() {
int a,b,c;
scanf("%d %d",&a,&b);
c=a+b;
printf("%d",c);
return 0;
}
SESSION: Randomized algorithm
ReplyDeleteQ. 95: RANDOMIZED ALGORITHMS 6
QUESTION DESCRIPTION
Write a program to add one to a given number. You are not allowed to use operators like +, -, *, /, ++, etc.
Examples:
Input: 12
Output: 13
Input: 6
Output: 7
Yes, you guessed it right, we can use bitwise operators to achieve this. Following are different methods to achieve same using bitwise operators.
#include
int main() {
int a,b;
scanf("%d",&a);
b=a+1;
printf("%d",b);
return 0;
}
SESSION: Randomized algorithm
ReplyDeleteQ. 97: MINMAX 1
QUESTION DESCRIPTION
Given a list of N integers, your task is to select K integers from the list such that its unfairness is minimized.
if (x1,x2,x3,,xk) are K numbers selected from the list N, the unfairness is defined as max(x1,x2,,xk)-min(x1,x2,,xk)
where max denotes the largest integer among the elements of K, and min denotes the smallest integer among the elements of K.
Input Format
The first line contains an integer N.
The second line contains an integer K.
N lines follow. Each line contains an integer that belongs to the list N.
Note: Integers in the list N may not be unique.
Output Format
An integer that denotes the minimum possible value of unfairness.
Constraints
2<=N<=10^5
2<=K<=N
0=
#include
#include
#include
#include
#include
using namespace std;
int main() {
int N, K, unfairness = INT_MAX;
int j, k;
cin >> N >> K;
int list[N];
for (int i=0; i> list[i];
/** Write the solution code here. Compute the result, store in the variable unfairness --
and output it**/
sort(list, list + N);
for (j = 0, k = K - 1; k < N; j++, k++){
if (unfairness > (list[k] - list[j]) )
unfairness = list[k] - list[j];
}
cout << unfairness << "\n";
return 0;
}
headers?
Deletehearder ????
DeleteSESSION: DIVIDE AND CONQUER
ReplyDeleteQ. EARTH AND THE METEORITES
#include
#include
#include
using namespace std;
int main() {
unsigned int t;
cin >> t;
while(t--) {
unsigned int n, m, q, i;
cin >> n >> m >> q;
vector x; vector y;
vector x1; vector y1;
x.clear(); x1.clear(); y.clear(); y1.clear();
x.push_back(1); x.push_back(n);
y.push_back(1); y.push_back(m);
for(i = 0; i < q; i++) {
unsigned int mx, my;
cin >> mx >> my;
x.push_back(mx);
y.push_back(my);
}
sort(x.begin(), x.end());
sort(y.begin(), y.end());
x1.push_back(x[0]);
for(i = 1; i < x.size(); i++) {
if(x[i] != x[i-1]) { x1.push_back(x[i]); }
}
y1.push_back(y[0]);
for(i = 1; i < y.size(); i++) {
if(y[i] != y[i-1]) { y1.push_back(y[i]); }
}
unsigned int xmin = n-1, xmax = 1;
for(i = 1; i < x1.size(); i++) {
if(xmin > (x1[i] - x1[i-1])) { xmin = x1[i] - x1[i-1]; }
if(xmax < (x1[i] - x1[i-1])) { xmax = x1[i] - x1[i-1]; }
}
unsigned int ymin = m-1, ymax = 1;
for(i = 1; i < y1.size(); i++) {
if(ymin > (y1[i] - y1[i-1])) { ymin = y1[i] - y1[i-1]; }
if(ymax < (y1[i] - y1[i-1])) { ymax = y1[i] - y1[i-1]; }
}
unsigned long long minarea = xmin;
minarea = minarea * ymin;
unsigned long long maxarea = xmax;
maxarea = maxarea * ymax;
unsigned long regions = x1.size() - 1;
regions = regions * (y1.size() - 1);
cout << regions << " " << minarea << " " << maxarea << "\n";
}
}
which header files?
Deletexyz123 mw1 tf??
Deletethe header files are:
Deletebits/stdc++.h
vector
algorithm
and vector x and vector y and same for x1 and y1
vector x and y
DeleteThis comment has been removed by the author.
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteSession 1 LIBRARY-
ReplyDelete#include
#include
using namespace std;
int main()
{
int t=10,i,c=0,d;
char a[100][100];
long b[100];
for(i=0;i>a[i]>>b[i];
}
for(i=0;i<t;i++)
{
if(b[i]==1111111)
d=i;
else
c++;
}
if(c==t-1)
cout<<"Datastructures book is available";
else
cout<<"Datastructures book is not available";
return 0;
}
not working
Deletewhat are the header files
Delete#include
Deleteusing namespace std;
vectorvec(1000001,0);
int main()
{
int i,j;
for(i=1;i<1000001;i++)
for(j=i*2;j<1000001;j+=i)
vec[j]++;
for(i=1;i<1000001;i++)
vec[i]+=vec[i-1];
int t;
cin>>t;
while(t--)
{
int n; cin>>n;
cout<<vec[n]<<"\n";
}
return 0;
}
Session: Dynamic Programming
ReplyDeleteSTOCK MAXIMIZE
#include
using namespace std;
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
long long int n,a[50005],i,idx,b[50005],sum=0;
scanf("%lld",&n);
for(i=1;i<=n;i++)
{
scanf("%lld",&a[i]);
}
idx=n;
b[n]=n;
for(i=n-1;i>0;i--)
{
if(a[idx]=0)
sum+=(a[b[i]]-a[i]);
}
cout<<sum<<endl;
}
}
This comment has been removed by the author.
ReplyDeleteGREEDY ALGORITHM:
ReplyDeleteSHERLOCK AND THE BEAST:
#include
using namespace std;
int main() {
int t; cin>>t;
while(t--){
int n; cin>>n;
if(n%3==0){
while(n--)
cout<<'5';
}
else if(n%5==0){
while(n--)
cout<<'3';
}
else if(n==11)
cout<<55555533333;
else
cout<<-1;
cout<<endl;
}
return 0;
}
GREEDY ALGO
ReplyDeleteLUCKY BALANCE:
#include
using namespace std;
void swap(int *xp, int *yp)
{
int temp = *xp;
*xp = *yp;
*yp = temp;
}
void bubbleSort(int arr[],int brr[],int n)
{
int i, j;
for (i = 0; i < n-1; i++)
for (j = 0; j < n-i-1; j++)
if (arr[j] < arr[j+1]){
swap(&arr[j], &arr[j+1]);
swap(&brr[j],&brr[j+1]);
}
}
int main() {
int n; cin>>n; int l; cin>>l;
int a[n],b[n],i,sum=0;
for(i=0;i>a[i]>>b[i];
bubbleSort(a,b,n);
//printArray(a,n);
for(i=0;i<n;i++){
if(b[i]==0)
sum=sum+a[i];
else {
if(l==0){
sum=sum-a[i];
}
else{
sum=sum+a[i];
l--;
}
}
}
cout<<sum;
return 0;
}
Bro please upload remaining sessions
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteSession: Graph COLORING
ReplyDeleteBFS-We Are On Fire
#include
using namespace std;
int n,m,q;
int a[10][10];
int c=0;
int b[10][2],z=0;
class Point{
public:
int x,y;
};
Point obj[20];
int check(int x, int y){
int i;
for(i=0;in || y<0 || y>m)
return;
if(a[x][y]==0)
return;
else if(a[x][y]==1){
--c;
calc(x-1,y);
calc(x,y-1);
calc(x+1,y);
calc(x,y+1);
}
}
int main() {
cin>>n>>m>>q;
int i,j;
for(i=0;i>a[i][j];
if(a[i][j]==1)
c++;
}
for(i=0;i>b[i][j];
calc(b[i][0]-1,b[i][1]-1);
cout<<c<<endl;
}
return 0;
}
This comment has been removed by the author.
ReplyDeleteI want java solutions
ReplyDeleteVijay has given a number N to his friend and asked him to print all its unique prime factors and their powers in N.
ReplyDeleteCan you help Vijay?
Input:
The first line of input contains an integer T denoting the number of test cases.
The first line of each test case is N.
Output:
Print all prime factors and their powers separated by spaces.
The output should be printed in increasing order of prime factors.
TEST CASE 1
INPUT
1
100
OUTPUT
2 2 5 2
TEST CASE 2
INPUT
1
352
OUTPUT
2 5 11 1
bro answer this please
SESSION: Data types
ReplyDeleteQ. 12: Magical game of sum
QUESTION DESCRIPTION
Yesterday, puppy Tuzik learned a magically efficient method to find the sum of the integers from 1 to N. He denotes it as sum(N). But today, as a true explorer, he defined his own new function: sum(D, N), which means the operation sum applied D times: the first time to N, and each subsequent time to the result of the previous operation.
For example, if D = 2 and N = 3, then sum(2, 3) equals to sum(sum(3)) = sum(1 + 2 + 3) = sum(6) = 21.
Tuzik wants to calculate some values of the sum(D, N) function. Will you help him with that?
Input
The first line contains a single integer T, the number of test cases. Each test case is described by a single line containing two integers D and N.
Output
For each test case, output one integer on a separate line.
Constraints
1 ≤ T ≤ 16
1 ≤ D, N ≤ 4
TEST CASE 1
INPUT
2
1 4
2 3
OUTPUT
10
21
TEST CASE 2
INPUT
3
1 7
1 5
2 6
OUTPUT
28
15
231
Anyone help me with this
DeleteVery informative article. nice programs. You can also tutorial notes about topics like Data Structure
ReplyDeleteSearching Algorithm for your future articles.
SESSION: Sorting Techniques
ReplyDeleteQ. 13: Steps of Bubble Sort
https://docs.google.com/document/d/1jY5BtVrcFr_yYkjsjJWOCoN1kO5lFDinKGoAXE5-WiQ/edit?usp=sharing
Q)Searching 6
ReplyDelete#include
int main()
{
int i,j,k,num1,num2,num3;
int arr[5];
for(i=0;i<5;i++)
{
scanf("%d",&arr[i]);
}
for(i=0;i<3;i++)
{
for(j=i+1;j<4;j++)
{
for(k=j+1;k<5;k++)
{
num1=arr[i];
num2=arr[j];
num3=arr[k];
if((num1+num2+num3)==0)
{
printf("%d %d %d",num1,num2,num3);
printf("\n");
}
}
}
}
return 0;
}
Faculty Details: Sorting Techniques
ReplyDelete#include
using namespace std;
typedef struct value{
int roll;
string name;
}data;
bool compare(data a, data b)
{
if(a.roll < b.roll)
return 1;
else
return 0;
}
int main()
{
int n,i;
cin>>n;
data array[n];
for(i=0;i>array[i].name;
cin>>array[i].roll;
}
sort(array,array+n,compare);
cout<<"After Sorting\nName ID"<<endl;
for(i=0;i<n;i++)
{
cout<<array[i].name<<" ";
cout<<array[i].roll<<endl;
}
return 0;
}
c++ iostream to be included
DeleteSorting techniques
ReplyDeleteMinimum Number of Swaps Needed
.
#include
using namespace std;
int main()
{
float T,N[100],P[100][100],c[100];
int i,j,k,temp;
cin>>T;
for(i=0;i>N[i];
c[i]=0;
for(j=0;j>P[i][j];
}
for(j=0;jP[i][k+1])
{
temp=P[i][k];
P[i][k]=P[i][k+1];
P[i][k+1]=temp;
c[i]++;
}
}
for(i=0;i<T;i++)
cout<<c[i]<<endl;
return 0;
}
can u resend corect code its not working
DeleteCommand failed: g++ -std=c++11 main.cpp
Deletemain.cpp: In function ‘int main()’:
main.cpp:11:7: error: expected ‘)’ before ‘;’ token
c[i]=0;
^
main.cpp:13:1: error: expected primary-expression before ‘}’ token
}
^
main.cpp:13:1: error: expected ‘)’ before ‘}’ token
main.cpp:13:1: error: expected primary-expression before ‘}’ token
main.cpp:13:1: error: expected ‘;’ before ‘}’ token
main.cpp: At global scope:
main.cpp:14:1: error: expected unqualified-id before ‘for’
for(j=0;jP[i][k+1])
^
main.cpp:14:9: error: ‘jP’ does not name a type
for(j=0;jP[i][k+1])
^
main.cpp:21:1: error: expected declaration before ‘}’ token
}
^
Divide and Conquer
ReplyDeleteStudent Arrangement
.
#include
using namespace std;
int checkempty(int seatcount[],int n,int m)
{
int i;
if(seatcount[n-1]!=0)
{
return n-1;
}
else
{
if(n<=m)
{
if(seatcount[n]!=0)
return n;
else
checkempty(seatcount,n+1,m);
}
else
return 0;
}
}
int main() {
int n,m,k,c=0,t;
cin>>n>>m>>k;
int arr[n],i,j,seat[m];
for(i=0;i>arr[i];
for(i=0;i<m;i++)
seat[i]=k;
for(i=0;i<n;i++)
{
t=checkempty(seat,arr[i],m);
if(t==0)
c++;
else
seat[t]--;
}
cout<<c-2;
return 0;
}
(include omitted iostream header)
Deletesend more na
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteStudent arrangement firse bhejo koi pls wo upar wala pura galat hai -_-
ReplyDeleteSESSION: Dynamic Programming
ReplyDeleteQ. 41: LEAF & LIME LIGHT ATTACK
QUESTION DESCRIPTION
Limelight is a technique that is used when all four users take place in the cardinal directions. They will then join their strength in the form of four connecting streams above the target area. It will then create a massive ball of lightning powerful enough to incinerate everything within the area of the four users.
The Leaf village is build in the shape of Spiral of integers. Spiral of integers, of an integer N, is an interesting NN spiral matrix which starts with 1 at the center.
For example, for N=4, the spiral of integers is Kitane, Nauma, Tu and Seito are planning to destroy the whole Leaf village. The limelight spot will be the 4 corners of the village.
Strength of the attack is equal to the sum of all the elements in the connecting streams as shown in the figure ( sum of diagonal elements of the spiral of integers of N ) .
Given the value of N, you need to compute the strength of the attack (mod 10^9+9).
#include
Deleteusing namespace std;
long long a[10000000];
int main()
{
long long mod=1e9+9;
a[1]=1;
a[2]=10;
for(long long int n=3;n<=10000000;n++)
{
a[n]=((a[n-2]%mod)+2*(2*n*n-3*n+3))%mod;
}
long long int t;
cin>>t;
while(t--)
{
long long int n;
cin>>n;
cout<<a[n]<<" ";
}
}
Dynamic Programming
ReplyDeleteTHE MAXIMUM SUB ARRAY
#include
int main() {
int t, n, i, j;
scanf("%d", &t);
for (i = 0; i < t; i++)
{
scanf("%d", &n);
int arr[n];
for (j = 0; j < n; j++)
{
scanf("%d", &arr[j]);
}
int sumn, sumf;
sumn = sumf = 0;
for (j = 0; j < n; j++)
{
sumn += arr[j];
if (sumf < sumn)
sumf = sumn;
if (sumn < 0)
sumn = 0;
}
int num, sum = 0;
for (j = 0; j < n; j++)
{
if (arr[j] > 0)
sum += arr[j];
}
printf("%d %d\n", sumf, sum);
}
return 0;
}
Someone plz send "lucky balance"
ReplyDeleteSESSION: Greedy Algorithm
Q. 37: LUCKY BALANCE
QUESTION DESCRIPTION
Lena is preparing for an important coding competition that is preceded by N sequential preliminary contests. She believes in "saving luck", and wants to check her theory. Each contest is described by two integers, Li and Ti :
1. Li is the amount of luck that can be gained by winning the contest. If Lena wins the contest, her luck balance will decrease by Li; if she loses it, her luck balance will increase by Li.
2. Ti denotes the contest's importance rating. It's equal to 1 if the contest is important, and it's equal to 0 if it's unimportant.
If Lena loses no more than K important contests, what is the maximum amount of luck she can have after competing in all the preliminary contests? This value may be negative.
Input Format
The first line contains two space-separated integers, N (the number of preliminary contests) and K (the maximum number of important contests Lena can lose), respectively.
Each line i of the N subsequent lines contains two space-separated integers, Li (the contest's luck balance) and Ti (the contest's importance rating), respectively.
Constraints
1<=N<=100
0<=K<=N
1<=Li<=10^4
0<=Ti<=1
Output Format
Print a single integer denoting the maximum amount of luck Lena can have after all the contests.
TEST CASE 1
INPUT
6 3
5 1
2 1
1 1
8 1
10 0
5 0
OUTPUT
29
TEST CASE 2
INPUT
2 1
1 1
8 1
10 0
5 0
OUTPUT
7
Program name:MAXIMUM PERIMETER TRIANGLE
ReplyDeleteSession:-Greedy Algorithm
Please send the answer of this as well.
Question Description:
Given n sticks of lengths l0,l1,.,ln-1, use 3 of the sticks to construct a non-degenerate triangle with the maximum possible perimeter. Then print the lengths of its sides as 3 space-separated integers in non-decreasing order.
If there are several valid triangles having the maximum perimeter:
1. Choose the one with the longest maximum side (i.e., the largest value for the longest side of any valid triangle having the maximum perimeter).
2. If more than one such triangle meets the first criterion, choose the one with the longest minimum side (i.e., the largest value for the shortest side of any valid triangle having the maximum perimeter).
3. If more than one such triangle meets the second criterion, print any one of the qualifying triangles.
If no non-degenerate triangle exists, print -1.
Input Format
The first line contains single integer, n, denoting the number of sticks.
The second line contains n space-separated integers, l0,l1,.,ln-1, describing the respective stick lengths.
Constraints
3 <= n <= 50
1 <= li <= 10^9
Output Format
Print 3 non-decreasing space-separated integers, a ,b , and c (where a <= b <= c) describing the respective lengths of a triangle meeting the criteria in the above Problem Statement.
If no non-degenerate triangle can be constructed, print -1.
TEST CASE 1
INPUT
5
1 1 1 3 3
OUTPUT
1 3 3
TEST CASE 2
INPUT
3
1 2 3
OUTPUT
-1
SESSION: Searching Techniques
ReplyDeleteQ. 8: Missing element of AP
#include stdio.h
#include limits.h
int main() {
int n, k, min, i, j, d;
scanf("%d", &n);
for (i = 0; i < n; i++)
{
min = INT_MAX;
scanf("%d", &k);
int arr[k];
for (j = 0; j < k; j++)
{
scanf("%d", &arr[j]);
}
for (j = 0; j < k - 1; j++)
{
d = abs(arr[j + 1] - arr[j]);
if (d < min)
min = d;
//printf("%d\n", min);
}
for (j = 0; j < k - 1; j++)
{
if (abs(arr[j + 1] - arr[j]) != min)
{
if (arr[j + 1] - arr[j] < 0)
printf("%d\n", arr[j] - min);
else
printf("%d\n", arr[j] + min);
break;
}
}
}
return 0;
}
Command failed: gcc main.c -lm
Deletemain.c:3:10: error: #include expects "FILENAME" or
#include stdio.h
^
main.c:4:10: error: #include expects "FILENAME" or
#include limits.h
^
main.c: In function ‘main’:
main.c:8:1: warning: incompatible implicit declaration of built-in function ‘scanf’ [enabled by default]
scanf("%d", &n);
^
main.c:11:7: error: ‘INT_MAX’ undeclared (first use in this function)
min = INT_MAX;
^
main.c:11:7: note: each undeclared identifier is reported only once for each function it appears in
main.c:30:1: warning: incompatible implicit declaration of built-in function ‘printf’ [enabled by default]
printf("%d\n", arr[j] - min);
^
SESSION: Sorting Techniques
ReplyDeleteQ. 11: Distinct absolute array elements
#include stdio.h
int main() {
int n, k, min, i, j;
scanf("%d", &n);
for (i = 0; i < n; i++)
{
scanf("%d", &k);
int arr[k];
min = 1;
for (j = 0; j < k; j++)
{
scanf("%d", &arr[j]);
}
for (j = 0; j < k - 1; j++)
{
if (abs(arr[j]) != abs(arr[j + 1]))
min++;
}
printf("%d\n", min);
}
return 0;
}
SESSION: Divide and Conquer
ReplyDeleteQ. 24: Closest pair of points problem
#include stdio.h
#include math.h
#include float.h
int main() {
int n, i, j;
float min = FLT_MAX, dis;
scanf("%d", &n);
int arr[n][2];
for (i = 0; i < n; i++)
{
scanf("%d %d", &arr[i][0], &arr[i][1]);
}
for (i = 0; i < n; i++)
{
for (j = i + 1; j < n; j++)
{
dis = sqrt(pow((arr[j][0] - arr[i][0]), 2) + pow((arr[j][1] - arr[i][1]), 2));
//printf("%f\n", dis);
if (dis < min)
min = dis;
}
}
printf("%f", min);
return 0;
}
Lucky Balance:
ReplyDelete#include
using namespace std;
void swap(int *xp, int *yp)
{
int temp = *xp;
*xp = *yp;
*yp = temp;
}
void bubbleSort(int arr[],int brr[],int n)
{
int i, j;
for (i = 0; i < n-1; i++)
for (j = 0; j < n-i-1; j++)
if (arr[j] < arr[j+1]){
swap(&arr[j], &arr[j+1]);
swap(&brr[j],&brr[j+1]);
}
}
int main() {
int n; cin>>n; int l; cin>>l;
int a[n],b[n],i,sum=0;
for(i=0;i>a[i];
cin>>b[i];
}
bubbleSort(a,b,n);
//printArray(a,n);
for(i=0;i<n;i++){
if(b[i]==0)
sum=sum+a[i];
else {
if(l==0){
sum=sum-a[i];
}
else{
sum=sum+a[i];
l--;
}
}
}
cout<<sum;
return 0;
}
Stock exchange Ka run nhi karra
ReplyDeleteQUESTION
ReplyDeleteSESSION: String Matching
Q. 76: Knuth-Morris-Pratt algorithm for Pattern Matching
QUESTION DESCRIPTION
Write a C program to implement KnuthMorrisPratt algorithm.
Knuth-Morris-Pratt(KMP) is a string matching algorithm.
It helps to find the search string in the given target string with minimal comparisons.
For the target string of length n and patter string of length m, the running time of the KMP algorithm is O(m+n).
KMP algorithm involves two phases. They are finding the overlap in the pattern and finding pattern in target string.
How to find the overlap in the given pattern?
Here, we need to find suffix(in the pattern) that matches prefix of the given pattern. For example, "abcdabd" is the input string. "ab" at the end is the suffix that matches the prefix "ab" at the start => abcdabd
TEST CASE 1
INPUT
C is a Programming Language
is a
OUTPUT
is a located at the index 3
TEST CASE 2
INPUT
THIS IS A TEST TEXT
TEST
OUTPUT
TEST located at the index 11
please help with this
#include
ReplyDelete#include
int main()
{
char Ch;
printf("\n Please Enter any alphabet\n");
scanf(" %c", &Ch);
if (isalpha(Ch) )
{
Ch = toupper(Ch);
printf ("\n Uppercase of Entered character is %c", Ch);
}
else
{
printf("\n Please Enter Valid Alphabet");
}
}
STACK OF BRICKS
ReplyDelete#include
#include
#include
#include
#include
using namespace std;
int main() {
int T;
cin>>T;
while(T > 0) {
int N;
cin>>N;
long long *bricks = new long long[N];
long long *sum = new long long[N]; // sum[i] = sum of bricks up to i
for(int i = 0; i < N; i++) {
cin>>bricks[N-i-1];
}
sum[0] = bricks[0];
for(int i = 1; i < N; i++) {
sum[i] = sum[i-1] + bricks[i];
}
if(N < 4) {
// Edge case
cout<<sum[N-1]<<"\n";
T--;
} else {
// Notice that taking Y bricks is equivalent to playing a new game with N-Y bricks where
// your friend goes first and you start with a score of the sum of the Y bricks.
long long *dp = new long long[N];
dp[0] = sum[0];
dp[1] = sum[1];
dp[2] = sum[2];
for(int i = 3; i < N; i++) {
// Take the # of bricks which maximizes your score
dp[i] = sum[i] - dp[i-3]; // 3 bricks
dp[i] = max(dp[i], sum[i] - dp[i-2]); // 2 bricks
dp[i] = max(dp[i], sum[i] - dp[i-1]); // 1 brick
}
cout<<dp[N-1]<<"\n";
T--;
}
}
return 0;
}
Question Name:LEAF & LIME LIGHT ATTACK
ReplyDelete#include
using namespace std;
long long a[10000000];
int main()
{
long long mod=1e9+9;
a[1]=1;
a[2]=10;
for(long long int n=3;n<=10000000;n++)
{
a[n]=((a[n-2]%mod)+2*(2*n*n-3*n+3))%mod;
}
long long int t;
cin>>t;
while(t--)
{
long long int n;
cin>>n;
cout<<a[n]<<" ";
}
}
Question Name:Power of Twos
ReplyDelete#include
#include
using namespace std;
typedef long ll;
#define se second
#define fi first
#define pb(x) push_back(x)
#define mp(x,y) make_pair(x,y)
#define itr(it,l,x) for(auto it=l[x].begin();it!=l[x].end();it++)
#define go(it,mp) for(auto it=mp.begin();it!=mp.end();it++)
ll domod(ll m,ll k)
{if(m>k)return m-k;}
#define f(i,a,b) for(i=a;i /*use iff inputs >0*/
inline void in(T &a) {
register T c;
a = 0;
do c = getchar_unlocked(); while(c < '0');
do {
a = (a << 1) + (a << 3) + c - '0';
c = getchar_unlocked();
} while (c >= '0');
}
ll read () { /*use for all cases but is slightly slower than above*/
bool minus = false;
ll result = 0;
char ch;
ch = getchar();
while (true) {
if (ch == '-') break;
if (ch >= '0' && ch <= '9') break;
ch = getchar();
}
if (ch == '-') minus = true; else result = ch-'0';
while (true) {
ch = getchar();
if (ch < '0' || ch > '9') break;
result = result*10 + (ch - '0');
}
if (minus)
return -result;
else
return result;
}
template
inline void pr(T a) {
char s[11];
T t = -1;
do {
s[++t] = a % 10 + '0';
a /= 10;
} while (a > 0);
while (t >= 0) putchar_unlocked(s[t--]);
putchar_unlocked('\n');
}
int main()
{
ll i,q,n,j;
ll dp[1000001]={0};
for(i=1;i<=1000000;i++)
for(j=2*i;j<=1000000;j+=i)
dp[j]++;
f(i,2,1000001)
dp[i]+=dp[i-1];
cin>>q;
while(q--)
{
cin>>n;
cout<<dp[n]<<endl;
}
}
Very well written about Algorithm Design And Analysis. If you want videos of Algorithm Design And Analysis, please download vidmate.With the help of Vidmate, you can download short videos of all apps. Your favorite videos can be easily downloaded in vidmate app. With this, the video of the song gets downloaded for free. You do not need to pay any fee or amount. Ividmate app has more than 1000 features you can download all your favorite audio videos in this app for free. This app is simple and easy to use and rule everyone's hearts because it is the best app. So far this app has been downloaded by 600 million people and has a high rating of 4.7. You can also download Algorithm Design And Analysis video and Vidmate app from 9apps
ReplyDeleteI Need Input And Outputs Question 1 Ans Please
ReplyDeleteBinita is playing a chess. The game will be played on a rectangular grid consisting of N rows and M columns. Initially all the cells of the grid are uncolored.Binita's initial score is zero. At each turn, he chooses some cell that is yet not colored, and colors that cell. The score obtained in this step will be number of neighboring colored cells of the cell that Binita colored in this step.
ReplyDeleteTwo cells are neighbors of each other if they share a side between them. The game will end when all the cells are colored. Finally, total score obtained at the end of the game will sum of score obtained in each turn.Binita wants to know what maximum score he can get? Can you please help him in finding this out?Constraints:1 ≤ N, M ≤ 50Input Format:The Only line of input contains two space-separated integers N, M denoting the dimensions of the grid.Output Format:Print the output a single line containing an integer corresponding to the maximal possible score Binita can obtain.
I need This Coding