/**
---------------------------------------------------------------------------
problem :
----------
- find first duplicate value in the given array without modifying the array.
---------------------------------------------------------------------------
input :
----------
- int *a : array of integers
- int l : number of elements in a
- int m : loose upper bound for value of an element of array a.
: 0 <= value_of_element_of_a < m
- relation between l and m : l > m
---------------------------------------------------------------------------
output :
----------
- first duplicate value occuring in the array.
---------------------------------------------------------------------------
constraints :
-------------
- space requirement should be O(1)
- after the function returns the array should be same as original.
---------------------------------------------------------------------------
/**/
//-------------------------------------------------------------------------
int find_firstduplicate(int *a, int l, int m)
{
int original = 0;
int index = 0;
int is_index_already_used = 0;
const int adjustment_for_zero_value = 1;
int duplicate = -1;
for(index = 0; index <= m; ++index)
{
is_index_already_used = 0;
original = a[index];
if(a[index] < 0)
{
//means there is already an element with value = index
is_index_already_used = 1;
//reversing the step of procedure to handle zero value
a[index] += adjustment_for_zero_value;
//original value of the element
original = abs(a[index]);
}
if(a[original] >= 0)
{
//negating
a[original] = (- a[original]);
//substracting 1, to handle zero value, since (minus ZERO) = (ZERO)
a[original] = a[original] - adjustment_for_zero_value;
//restoring the fact that index is used.
a[index] -= is_index_already_used;
}
else
{
//duplicate found.
duplicate = original;
break;
}
}
//readjusting the values of array so that they resemble the original.
for(index = 0; index <= m; ++index)
{
if(a[index] < 0)
{
a[index] += adjustment_for_zero_value;
a[index] = - a[index];
}
}
return duplicate;
}
//-------------------------------------------------------------------------
/**
---------------------------------------------------------------------------
solution :
- hint : use the value of element as an index 'i' and negate the value at
index i.
- complexity : O(m)
---------------------------------------------------------------------------
/**/
void main()
{
int m = 5;
int a[] = {1, 0, 3, 0, 4, 5};
int l = sizeof(a) / sizeof(a[0]);
int duplicate = find_firstduplicate(a, l, m);
if(duplicate != -1)
{
cout << "first duplicate = " << duplicate << endl;
}
else
{
cout << "duplicate does not exist." << endl;
}
getch();
}
/**
output :
--------
first duplicate = 0
/**/
Description : { int and char, for and if, while and else, struct, class, thread, mutex, thoughts, switch, >>, &&, patterns, member function, public and private, thoughts and messages, commands, tips and tricks; }
Monday, February 20, 2012
finding duplicate entry in an array.
Friday, October 14, 2011
70 Years, programmed the world.
"C is quirky, flawed, and an enormous success."
-Dennis MacAlistair Ritchie (September 9, 1941 – October 8, 2011)
-Dennis MacAlistair Ritchie (September 9, 1941 – October 8, 2011)
He developed see programming language, though which I was saw possibilities through programming.
#include <stdio.h>
int main(int argc, char *argv[])
{
printf("Hello Heaven\n");
return 0;
}
Sir Dennis's World...
Tuesday, July 5, 2011
Top scorer.
Original Newspaper Article.
TechGig to Launch Season 2 of Indian Programming League
OUR BUREAU NEW DELHI
Technology community site TechGig.com, developed by TimesJobs, will launch the second season of the Great Indian Programming League (GIPL), an innovative method of hiring talented software developers from across the country. TechGig created GIPL to connect with coding buffs who normally work behind the scenes and provide a recruitment platform for companies looking for quality talent.
TechGig.com product head Amit Gupta said: “The IPL is a great way of benchmarking one’s coding skills with some of the best software developers in India. We are now launching Season 2 of the IPL, so there is a lot more action in store.” At GIPL, recruiters looking for high quality technical talent get an objective benchmark to automatically measure the competence of people.
The contest attracts passive candidates to participate even if they aren’t looking for a job. Moreover, the scores are easy to measure, making it easy to find the best people at the top. This renders the contest a great platform to sort and hire. During its first season last month, GIPL got more than 11,000 entries from over 2,700 programmers from across India.
The second contest is coming up due to popular demand from techies and tech firms. In the last season, a large number of coders, programmers and software developers from Delhi NCR, Pune, Bangalore, Hyderabad and Chennai battled it out for iPods, daily movie tickets but more importantly recognition among peers.
Omnitech Infosolutions COO and head of global operations Anurag Shah said: “The Great Indian Programming League is an aspiring initiative taken by techgig.com to not just recognize the talented coders but also provide them a platform to achieve global recognition, which will surely motivate them to feature their best practices in the industry.”
Coding as a field is indeed as promising as other professional fields which require technical as well as domain expertise. Coders play a vital role in the software development and act as a bloodline of the projects. The initiative would bring required awareness and change in perception to help coding attain the deserving limelight, Shah added.
The contest involves writing actual code across five to seven different programming languages, including C #, Java, C and C++, to solve a problem live. The software automatically compiles the code to assess its quality and comprehensiveness in solving the problem.
The second season expects to be more promising going by the reaction of programmers who came from across the country in the first season, just for their love for coding. “Just coding, am mad on coding,” said TCS software developer Vishnu Priya Subramanian while Sameer Namdeo said his motivation was a passion for coding, and chance to evaluate him among peers.
Dotsquares
team lead Deepak Tanwar said GIPL was all about personal enhancement, profile upgrade and opportunity to compete with programmers of different languages.
Users complimented Tech-Gig on the range and complexity of the problems set. Uniprose India product manager Vijendra Kumar pointed out the best part of the contest was that the code could not be seen by other programmers and online instant verification of code.
Winning for software developer Jagdesh from Infosys Technologies Limited was overwhelming while Mahindra Satyam Computer Services Limited project leader Amit Kantilal said: “I feel very proud that I have demonstrated my programming skills. I successfully completed the program to solve the sudoku puzzle which has been on my cards since quite some time.”
| Publication: | The Economic Times Delhi; | Date: Jul 4, 2011; | Section: Corporate; | Page: 5 |
TechGig to Launch Season 2 of Indian Programming League
OUR BUREAU NEW DELHI
Technology community site TechGig.com, developed by TimesJobs, will launch the second season of the Great Indian Programming League (GIPL), an innovative method of hiring talented software developers from across the country. TechGig created GIPL to connect with coding buffs who normally work behind the scenes and provide a recruitment platform for companies looking for quality talent.
TechGig.com product head Amit Gupta said: “The IPL is a great way of benchmarking one’s coding skills with some of the best software developers in India. We are now launching Season 2 of the IPL, so there is a lot more action in store.” At GIPL, recruiters looking for high quality technical talent get an objective benchmark to automatically measure the competence of people.
The contest attracts passive candidates to participate even if they aren’t looking for a job. Moreover, the scores are easy to measure, making it easy to find the best people at the top. This renders the contest a great platform to sort and hire. During its first season last month, GIPL got more than 11,000 entries from over 2,700 programmers from across India.
The second contest is coming up due to popular demand from techies and tech firms. In the last season, a large number of coders, programmers and software developers from Delhi NCR, Pune, Bangalore, Hyderabad and Chennai battled it out for iPods, daily movie tickets but more importantly recognition among peers.
Omnitech Infosolutions COO and head of global operations Anurag Shah said: “The Great Indian Programming League is an aspiring initiative taken by techgig.com to not just recognize the talented coders but also provide them a platform to achieve global recognition, which will surely motivate them to feature their best practices in the industry.”
Coding as a field is indeed as promising as other professional fields which require technical as well as domain expertise. Coders play a vital role in the software development and act as a bloodline of the projects. The initiative would bring required awareness and change in perception to help coding attain the deserving limelight, Shah added.
The contest involves writing actual code across five to seven different programming languages, including C #, Java, C and C++, to solve a problem live. The software automatically compiles the code to assess its quality and comprehensiveness in solving the problem.
The second season expects to be more promising going by the reaction of programmers who came from across the country in the first season, just for their love for coding. “Just coding, am mad on coding,” said TCS software developer Vishnu Priya Subramanian while Sameer Namdeo said his motivation was a passion for coding, and chance to evaluate him among peers.
Dotsquares
team lead Deepak Tanwar said GIPL was all about personal enhancement, profile upgrade and opportunity to compete with programmers of different languages.
Users complimented Tech-Gig on the range and complexity of the problems set. Uniprose India product manager Vijendra Kumar pointed out the best part of the contest was that the code could not be seen by other programmers and online instant verification of code.
Winning for software developer Jagdesh from Infosys Technologies Limited was overwhelming while Mahindra Satyam Computer Services Limited project leader Amit Kantilal said: “I feel very proud that I have demonstrated my programming skills. I successfully completed the program to solve the sudoku puzzle which has been on my cards since quite some time.”
Tuesday, August 3, 2010
Common Pitfall : While using malloc instead of new.
Two mostly used things that help in allocating memory are
Sometimes knowledge of using
Situation [1] (Complication Level 1):
Now to access integers at index from 0 to 9 simply use
Very simple!!!
Situation [2] (Complication Level 2):
Now to access integer P1 of say 4th struct ABC :
Again Very simple!!!
Situation [3] (Complication Level 3):
Now to access integer P1 of ABC which is inside of say 5th struct PQR :
Again Moderately simple!!!
In above 3 situations memory was allocated and then utilized according to the structure\ layout of structs.
Situation [4] (Complication Level 4):
Now try to insert elements in the list P3 of say 4th struct XYZ:
Run Status CRASH!!!
Without reading further think a minute why the code did not worked even though memory was already allocated?
This happened because though memory was allocated for all the data members of list
In a composite data type the data-members are created (here created means calling of their respective constructor) prior to the invocation of composite data type constructor itself. ("Refer how Object creation takes place")
So what will initialize the data-members along with the task of allocating the memory for members.
Answer :
Here 'new' invokes the constructor of both, the data members of constructor, members can be initialized with default values.
Conclusion:
[1]Anyway, in C++
[2]Also try to minimize mixing of
malloc and new. Sometimes knowledge of using
malloc, for allocating memory in common and less complicated situations, can prove wrong in complicated situations. Situation [1] (Complication Level 1):
int *pData = (int*)malloc(10 * sizeof(int)); Now to access integers at index from 0 to 9 simply use
pData[1] or *(pData+2)... Very simple!!!
Situation [2] (Complication Level 2):
struct ABC
{
int P1;
char P2;
};
ABC *pData = (ABC*)malloc(10 * sizeof(ABC)); Now to access integer P1 of say 4th struct ABC :
pData[3].P1 Again Very simple!!!
Situation [3] (Complication Level 3):
struct ABC
{
int P1;
char P2;
};
struct PQR
{
int P1;
char P2;
ABC P3;
};
PQR *pData = (PQR*)malloc(10 * sizeof(PQR)); Now to access integer P1 of ABC which is inside of say 5th struct PQR :
pData[4].P3.P1 Again Moderately simple!!!
In above 3 situations memory was allocated and then utilized according to the structure\ layout of structs.
Situation [4] (Complication Level 4):
struct XYZ
{
int P1;
char P2;
CList P3;
};
XYZ *pData = (XYZ*)malloc(10 * sizeof(XYZ)); Now try to insert elements in the list P3 of say 4th struct XYZ:
pData[3].P3.AddTail(2);
Complie Status OK. Run Status CRASH!!!
Without reading further think a minute why the code did not worked even though memory was already allocated?
This happened because though memory was allocated for all the data members of list
XYZ::P3 but those data members were not initialized with default values. struct XYZ in above example is composite data type (a class or user defined data type) in which the default value initialization of its data-members is normally done in constructor of the class.In a composite data type the data-members are created (here created means calling of their respective constructor) prior to the invocation of composite data type constructor itself. ("Refer how Object creation takes place")
So what will initialize the data-members along with the task of allocating the memory for members.
Answer :
operator new XYZ *pData = new XYZ[10]; Here 'new' invokes the constructor of both, the data members of
struct XYZ and the struct XYZ itself. Inside the CListConclusion:
[1]Anyway, in C++
new is the preferred mechanism for memory allocation. [2]Also try to minimize mixing of
malloc and new. Monday, July 19, 2010
Second technical article.
While working with functions, I found a common pattern. A pattern in which all the parameters constituting the parameter-list were bundled in a struct and that struct was passed as a parameter to the function. Therefore, gave a thought on this, in order to find what can be the pros and cons of this pattern. Outcome was the following article:
Description: An article highlighting advantage of using composite data-type for passing parameters to functions.
Parameter Passing :- by Train vs by Truck.
Description: An article highlighting advantage of using composite data-type for passing parameters to functions.
Parameter Passing :- by Train vs by Truck.
Wednesday, July 14, 2010
C\ C++ Pointer confusion.
Working with pointers by passing them here and there and not getting expected result!!!
Don't go here and there but go directly to :
Pointer Alises.
Don't go here and there but go directly to :
Pointer Alises.
Tuesday, July 13, 2010
First technical article.
Check out my first technical article on codeproject.com.
Description: An article on creating a framework for command processing using Command Design Pattern.
Converting a class into a CommandHandler.
Description: An article on creating a framework for command processing using Command Design Pattern.
Converting a class into a CommandHandler.
Hello World
Subscribe to:
Posts (Atom)
