kathireson
BAN USER
#include <iostream>
using namespace std;
// function to increment the counters
int inc(int i, int a[], int n,int k)
{
if (i<=0)
{
return (++a[0]);
}
a[i]++;
if(a[i] >= (n - (k-i)))
{
a[i] = inc(i-1, a, n, k) + 1;
}
return a[i];
}
// function to check the end of loop
bool end_of_loop(int a[], int k, int n)
{
bool res = true;
for(int i = 0; i < k; i++)
{
res = res && (a[i] >= (n -(k+i-1)));
}
return res;
}
int main()
{
// input array
int arr[] = {1,2,3,4,5,6,7,8};
// total number of elements in array
int N = 8;
// combination size
int K = 4;
// indexes of size K
int a[K];
for(int i = 0; i<K;i++)
{
a[i] = i;
}
bool loop = end_of_loop(arr,K,N-1);
int count = 0;
//loop till all combinations are printed
while(!loop)
{
for(int i = 0; i < K; i++)
{
cout << arr[a[i]] <<" - ";
}
cout <<endl;
count++;
// increment the counters
inc(K-1, a, N,K-1);
// check for end of loop
loop = end_of_loop(a,K,N-1);
}
// total no of combinations
cout << "Count -- "<<count<<endl;
return 0;
}
- kathireson April 08, 2013Whoa! I completely overlooked that scenario. I guess your answer is the correct one.
- kathireson March 24, 2013yup.. didn't test it before posting it. Thanks for pointing out. Corrected now.
void maxproduct( int num[], int length)
{
if(length >= 1)
{
int currStart, currEnd, maxStart, maxEnd;
double currProd, prevProd, maxProd;
prevProd = currProd = maxProd = num[0];
currStart = 0;
currEnd = -1;
int i = 1;
for( ; i < length; i++)
{
currProd = currProd * num[i];
if(currProd < prevProd)
{
currEnd = i;
if( prevProd > maxProd)
{
maxStart = currStart;
maxEnd = currEnd-1;
maxProd = prevProd;
}
currStart = i;
currProd = num[i];
}
prevProd = currProd;
}
if (currProd > maxProd) // if the value at the end is greater...
{
maxStart = currStart;
maxEnd = i-1;
maxProd = currProd;
}
cout << "Range is : "<<maxStart<<" - "<<maxEnd<<" = "<<maxProd<<endl;
}
else
{
cout << "Empty Array"<<endl;
}
}
There should not be a new string and allocating new memory for the extra characters alone will break the indexing of the string.
- kathireson March 24, 2013This logic has the possibility of returning the smallest element in the tree right? I guess there should be an extra check for non leaf node
- kathireson March 22, 2013void maxproduct( int num[], int length)
{
int currStart, currEnd, maxStart, maxEnd;
double currProd, prevProd, maxProd;
prevProd = currProd = maxProd = num[0] // assuming non empty array, add checks
currStart = 0;
currEnd = -1;
int i = 1;
for( ; i < length; i++)
{
currProd = currProd * num[i];
if(currProd < prevProd)
{
currEnd = i;
if( prevProd > maxProd)
{
maxStart = currStart;
maxEnd = currEnd;
maxProd = prevProd;
}
currStart = i;
currProd = num[i];
}
prevProd = currProd;
}
if(currEnd == -1 || currEnd < currStart)
{
currEnd = i;
if (currProd > maxProd)
{
maxStart = currStart;
maxEnd = currEnd;
maxProd = currProd;
}
}
cout << "Range is : "<<maxStart<<" - "<<maxEnd<<" = "<<maxProd<<endl;
}
The minimum no of moves will be 2*pow(n,2) + 3*n. Where n is the no of monsters.
The sequence for AAAA_BBBB is: (_ is the empty space)
AAA_ABBBB
AAABA_BBB
AAABAB_BB
AAAB_BABB
AA_BABABB
A_ABABABB
ABA_ABABB
ABABA_ABB
ABABABA_B
ABABABAB_
A_ABABAB_
ABA_ABAB_
A_A_ABAB_
A_ABA_AB_
ABA_A_AB_
A_A_A_AB_
A_A_ABA__
A_ABA_A__
ABA_A_A__
A_A_A_A__
A_A_A__A_
A_A_A___A
A_A_A____
A_A__A___
A_A___A__
A_A____A_
A_A_____A
A_A______
A__A_____
A___A____
A____A___
A_____A__
A______A_
A_______A
A________
_A_______
__A______
___A_____
____A____
_____A___
______A__
_______A_
________A
A classic kd tree problem in finding 'n' nearest neighbours for a given 2D point. Check this link : en.wikipedia.org/wiki/K-d_tree#Nearest_neighbour_search
- kathireson March 09, 2013
Repsophiaddelano, Android Engineer at 247quickbookshelp
Extensive experience setting up, installing, and maintaining a variety of audio equipment. I Work with loudspeakers, amplifiers,microphones, and more ...
Replarrymeisterl71, Analyst at ABC TECH SUPPORT
Working as Food cooking machine tender at Adray it's almost 10 years . Here I Operate or tend cooking equipment ...
RepI am 27 year old. I am from Toro, United states. I work in Jacobs as Mechanical drafter. I love ...
Repcarmenrhargis, Associate at Achieve Internet
Hi, I am Gladys, I live in Florida, USA, I am working as a project manager in a Life’s ...
Repmelonydmaxwell, maintenence engineer at AMD
Hi, I am working as a health information technician and my work is to collect and maintain a patient's ...
Repwilliamchansen95, Computer Scientist at 247quickbookshelp
I am working as a manager in Lionel Kiddie City company. I really enjoy my job. I like to play ...
Repcardiroy, Backend Developer at Accolite software
Hi,I am from Taxes, USA. Enthusiastic multilingual translator with years involvement with Spanish-English translations.Looking to additionally improve interpretation ...
Repcherylthurber, Developer Advocate at Absolute Softech Ltd
Hi,I am from Taxes, USA. Passionate multilingual translator with 2.5 years experience in Spanish-English translations.Looking to further ...
Repmarierlewis2, AT&T Customer service email at ASAPInfosystemsPvtLtd
I am Marie and I live in Los Angeles USA, I have a strong base in marketing and I believe ...
Repkentyoung319, Applications Developer at ABC TECH SUPPORT
I am KentYoung . I am working as an Orthotics technician at The Wiz . As an orthotic technician,I manufacture orthotic ...
Repmariadbulter222, Android Engineer at ABC TECH SUPPORT
Hey, I am a corporate investigator. I love this work. My hobby like write story and different types of article ...
RepValerieHill, abc at 8x8
I am a highly motivated development and community advocate with over 2 years experience fostering strong community relations product advancements ...
RepRomiTerry, Member Technical Staff at Cleartrip.com
Romi , a Dynamic Property Manager highly regarded for increasing and maintaining a high level of occupancy adhering to all regulations ...
Repopalphelan234, Associate at 247quickbookshelp
I am a specialized Cardiac and vascular nurse at the Circus World . Here I meet different people and observe their ...
RepEileenWenda, DIGITAL MARKETING at Accenture
I am Eileen , a football Referee skilled at maintaining a safe environment for both players and observers, inspecting the playing ...
- kathireson July 24, 2016