Anand
BAN USERThis is my try.........
I think it will work for all combinations not only for 3........
If there is any mistakes, help me to rectify it.........
/*Given a array of numbers and a number N. Find out all combinations of 3 numbers in array whose sum is N.*/
#include<iostream.h>
#include<conio.h>
int n,a[20],N,s,b[20];
void find_comb(int i,int j,int sum)
{
sum+=a[i];
b[j]=a[i];
if((j==N-1)&&(sum==s))
{
cout<<"{";
for(int k=0;k<=j;k++)
cout<<b[k]<<",";
cout<<"\b},";
}
else if(sum<s)
{
for(i++;i<n;i++)
find_comb(i,j+1,sum);
}
}
void main()
{
clrscr();
cout<<"Enter the lenth of array ";
cin>>n;
cout<<"Enter the numbers......";
for(int i=0;i<n;i++)
{
cin>>a[i];
}
cout<<"Enter the Sum ";
cin>>s;
cout<<"Enter the Number of combinations ";
cin>>N;
for(i=0;i<n-N+1;i++)
find_comb(i,0,0);
cout<<"\b";
getch();
}
This is my try....... Is this inspace algorithm.........?
import java.io.*;
class Inplace
{
public static void main(String q[])
{
DataInputStream dis=new DataInputStream(System.in);
int n1,n2,n,a[]=new int[20],i,j;
System.out.println("Enter the length ");
try
{
n2=Integer.parseInt(dis.readLine());
System.out.println("Enter the elements : ");
n1=n2;
for(i=0;i<n2;i++)
{
a[i]=Integer.parseInt(dis.readLine());
if((i!=0)&&(a[i]<a[i-1]))
{
n1=i;
}
}
i=n1-1;j=n2-1;
while((i>=0)&&(j>=n1))
{
if(a[i]<a[j])
{
j-- ;
}
else
{
int c=a[i],k;
for(k=i;k<j;k++)
a[k]=a[k+1];
a[k]=c;
i--;
j--;
n1--;
}
}
System.out.println("Sorted Numbers are");
for(i=0;i<n2;i++)
System.out.println(a[i]);
}
catch(IOException e)
{
System.out.println(e);
}
}
}
This is my try.......... Is this in space algorithm.........?
import java.io.*;
class Inplace
{
public static void main(String q[])
{
DataInputStream dis=new DataInputStream(System.in);
int n1,n2,n,a[]=new int[20],i,j;
System.out.println("Enter the length ");
try
{
n2=Integer.parseInt(dis.readLine());
System.out.println("Enter the elements : ");
n1=n2;
for(i=0;i<n2;i++)
{
a[i]=Integer.parseInt(dis.readLine());
if((i!=0)&&(a[i]<a[i-1]))
{
n1=i;
}
}
i=n1-1;j=n2-1;
while((i>=0)&&(j>=n1))
{
if(a[i]<a[j])
{
j-- ;
}
else
{
int c=a[i],k;
for(k=i;k<j;k++)
a[k]=a[k+1];
a[k]=c;
i--;
j--;
n1--;
}
}
System.out.println("Sorted Numbers are");
for(i=0;i<n2;i++)
System.out.println(a[i]);
}
catch(IOException e)
{
System.out.println(e);
}
}
}
My try.........
I learnt a lot while trying this code..........
string is not immuttable and about string buffer and string builder etc...........
import java.io.*;
import java.lang.*;
class Binadd
{
public static void main(String a[])
{
String n1,n2;
StringBuffer n3;
char c='0';
int l1,l2,i,j,l;
DataInputStream dis=new DataInputStream(System.in);
try
{
System.out.println("Enter the 2 numbers");
n1=dis.readLine();
n2=dis.readLine();
l1=n1.length();
l2=n2.length();
if(l1>=l2)
{
n3=new StringBuffer(n1);
l=l1-1;
}
else
{
n3=new StringBuffer(n2);
l=l2-1;
}
for(i=l1-1,j=l2-1;(i>=0)&&(j>=0);i--,j--)
{
if(c=='0')
{
if((n1.charAt(i)=='1')&&(n2.charAt(j)=='1'))
{
n3.setCharAt(l--,'0');
c='1';
}
else if((n1.charAt(i)=='0')&&(n2.charAt(j)=='0'))
{
n3.setCharAt(l--,'0');
c='0';
}
else
{
n3.setCharAt(l--,'1');
c='0';
}
}
else
{
if((n1.charAt(i)=='1')&&(n2.charAt(j)=='1'))
{
n3.setCharAt(l--,'1');
c='1';
}
else if((n1.charAt(i)=='0')&&(n2.charAt(j)=='0'))
{
n3.setCharAt(l--,'1');
c='0';
}
else
{
n3.setCharAt(l--,'0');
c='1';
}
}
}
for(;i>=0;i--)
if((n1.charAt(i)=='1')&&(c=='1'))
{
n3.setCharAt(l--,'0');
c='1';
}
else if((n1.charAt(i)=='0')&&(c=='0'))
{
n3.setCharAt(l--,'0');
c='0';
}
else
{
n3.setCharAt(l--,'1');
c='0';
}
for(;j>=0;j--)
if((c=='1')&&(n2.charAt(j)=='1'))
{
n3.setCharAt(l--,'0');
c='1';
}
else if((c=='0')&&(n2.charAt(j)=='0'))
{
n3.setCharAt(l--,'0');
c='0';
}
else
{
n3.setCharAt(l--,'1');
c='0';
}
if(c=='1')
{
n2='1'+n3.toString();
System.out.println(n2);
}
else
System.out.println(n3);
}
catch(IOException e)
{
System.out.println(e);
}
}
}
my Try............
import java.io.*;
class Substr
{
public static void main(String a[])
{
DataInputStream dis =new DataInputStream(System.in);
String src,sub;
int count=0,i,j,n1,n2;
try
{
System.out.println("Enter the source string ");
src=dis.readLine();
System.out.println("Enter the Sub string ");
sub=dis.readLine();
n1=src.length();
n2=sub.length();
for(i=0,j=0;i<n1;)
{
if(src.charAt(i)==sub.charAt(j))
{
i++;
j++;
if(j==n2)
{
count=count+1;
j=0;
}
}
else
{
i=i-j+1;
j=0;
}
}
System.out.println(count);
}
catch(IOException e)
{
System.out.println(e);
}
}
}
In case of c or c++, we can use built in function strstr which will return the first occureence of substr in srcstr................
#include<iostream.h>
#include<string.h>
void main()
{
char src[]="why should i love programming? i love programming because it lets me not feel boring";
char sub[]=" love p";
char *p=strstr(src,sub);
if(p)
cout<<p;
else
cout<<"The string is not present";
}
#include<iostream.h>
#include<stdlib.h>
#include<conio.h>
class Queue{
int queue[20],front,rear;
public:
Queue() {
front=0;
rear=0;}
int get() {
if(front==rear) {
cout<<"Queue is empty";
getch();
exit(1); }
return(queue[front++]); }
void put(int a) {
if(rear==20) {
cout<<"Queue is Full";
getch();
exit(1); }
queue[rear++]=a; } };
void main(){
int A[20],i,j=0,n,k=0;
clrscr();
Queue q;
cout<<"Enter the number of terms :";
cin>>n;
for(i=0;i<n;i++)
cin>>A[i];
for(i=0;i<n;i++) {
if(A[i]==0)
k=1;
else if(A[i]>0)
q.put(A[i]);
else if(A[i]<0)
A[j++]=A[i]; }
if(k==1)
A[j++]=0;
for(;j<n;j++)
A[j]=q.get();
cout<<" The sorted list : "<<endl;
for(i=0;i<n;i++)
cout<<A[i]<<endl;
getch();}
RepColaraJoshi, Floor manager at Thomes
By profession, I am Floor manager in the Thomes store. I am passionate about astrology and read tronto cards, horoscopes ...
Repbelindabest675, Animator at ABC TECH SUPPORT
Hey, my name is Belinda and I completed all my study from New York. And Nowadays I am working as ...
RepFreyaShaw, Attorney at 247quickbookshelp
Excellent verbal and written communication skills to aid in properly presenting positions, arguing cases, and getting positive outcomes for clients ...
RepAdaShipman, abc at 247quickbookshelp
During operation, determine all necessary adjustments to the printing press to maintain safety, quality and productivity standards and make such ...
RepRuzilStaker, Java Freshers at Digital Merkating
Ruzil , a Publicity Specialist with over four years in PR. Also a communicator adept at managing all public relations efforts ...
RepNirvedDavis, abc at 8x8
Professional lifeguard offering expertise in water safety. Worked for more than six years in guarding and controlling the safety of ...
Repaalvinbrowne, Android Engineer at ASAPInfosystemsPvtLtd
Working as an Agricultural laborer at Mars Music I maintain yields like natural products, vegetables, grains, and nuts, or take ...
RepPrankHwa, Animator at Accolite software
I am a Reporter and responsible for delivering updates and analysis on current events with the main goal to keep ...
Repcorinnedonon, Backend Developer at A9
I am Corinne, a highly organised, professional and self-motivated Dietitian with a great passion for nutrition and people’s well-being ...
Repearleneefranks, Accountant at 247quickbookshelp
I am working as an Information clerk and I love my job. I also love to read new Articles related ...
Repjacquelinejlopes48, Analyst at 247quickbookshelp
Hey, I am Jacqueline and I am a court reporter.I had heard a lot about Vashikaran Specialist,now I ...
Rephcr689121, Data Engineer at Autonomy Zantaz
Harrison is an electronic data processor experience in electronic data processor and Promotion. I enjoy providing health resources and how ...
Repzippy28943, Data Scientist at AppNexus
Maxine , a grant writer specializing in researching and analysing grant opportunities, creating funding plans, and conducting proposals and budgets. Last ...
Copied from Stack overflow for others to understand the logic..........
- Anand March 10, 2013First you need to modify the given internal diameters of the rings to fill up the unnecessary gaps. Suppose you have a ring of internal diameter 5 below a ring with internal diameter 2. Any disk of size greater than 2 will not be able to reach that ring. So we can change the internal diameter of the lower ring to 2. Basically we are filling up the gaps.
Use the following algorithm:
min = A[0]
i = 1
if i >= N then STOP
if A[i] < min then min = A[i]
if A[i] > min then A[i] = min
i++
go to step 3
Now that the rings have formed a structure where each lower ring has an internal diameter lesser than or equal to the internal diameter of the upper ring, the next part is fairly simple. We will start inserting the disks from the bottom. If the first disk fits the lowest ring then fine otherwise we move on to the ring above it and so on. You can use an algorithm like:
i = N-1
j = 0
if i < 0 or j >= M then STOP
if A[i] >= B[j] then i--, j++
if A[i] < B[j] then i--
go to step 4
The final value of the variable j is the required answer.