sameepsheth
BAN USERAssuming loop is OK and we are trying to find different number using XOR.
 if you have pairs of matching numbers aka odd number of elements that just XOR all elements will give result.
 for event number of elements you must remember the first number in a temporary variable, XOR n1 numbers. If the resulting number is same as first number then return nth number as a different number, else XOR the number and return that as a result.
ex.
int differentNumber(int input[], int n) {
int res = input[0];
if (n <=2)
return res;
for (int i = 0; i < n  2; i++)
res ^= input[i];
if (n % 2 != 0 ) {
return (res ^ input[n1]);
} else {
if ( res == input[0]) {
return input[n1];
} else {
return res ^ input[n  1];
}
}
}

sameepsheth
August 05, 2014 Assuming it's int a[5][5] is equivalent to 5*5 = 25th int which would be 25*4 = 100 bytes from th e start. and with similar logic a[7][9] would be 63th int. difference between the two is 63  25 = 38 so 38 * 4 = 152. add that to 1000 so that address would be 1152.
 sameepsheth August 05, 2014can you clarify the question? You have an array and you are searching for a value in it. that's a simple sequential search. or is the array sorted? what role does roration play?
 sameepsheth January 30, 2014#include <iostream>
using namespace std;
#define ASSERT(x);
const int TOTAL_ALPHABET = 26;
//
// input  the input stream to search
// size  size of the input stream in characters
//
// returns first non repeat charcter if found else returns char equivalent of 1
char firstnonrepeatingcharacter(char* input, int size)
{
char result = 1;
char char_index[TOTAL_ALPHABET];
int char_count[TOTAL_ALPHABET] = {0};
int chartoint;
int charindex = 1;
// scan the input once O(n)
for (int i = 0; i < size; i++)
{
chartoint = tolower(input[i])'a';
if (++char_count[chartoint] == 1)
{
ASSERT(charindex <= TOTAL_ALPHABET);
char_index[++charindex] = tolower(input[i]);
}
}
// scan char index array to find first letter with char count 1 O(1) operation
for (int i = 0; i <= charindex; i++)
{
if (char_count[char_index[i]'a'] == 1)
return char_index[i];
}
return result;
}

sameepsheth
January 30, 2014 Open Chat in New Window
 sameepsheth August 06, 2014