Sunday, August 2, 2009

How in c++ i can check if array(n*n) have numbers from 1 to n?

I would sort the array from smallest to largest, then see if a number is skipped. If you want to save time, delete repeats.





int highest_probed_num = 0;





for ( int i = 0; i %26lt; array.length-1; i++){


if ( (array[i] != array[i+1]) || (array[i] != array[i+1]+1)){


return false;


}


if ( (array[i] == n)){ break;} //this is incase there are numbers higher than N AND you only care about numbers between 1 and N.


}





return true;





PS. SW, I think "array(n*n)" means that his array is n^2 elements in it instead of a double array.

How in c++ i can check if array(n*n) have numbers from 1 to n?
when you say have numbers from 1 to n, do you mean between 1 and n?





for(int row = 0; row %26lt; n; ++n) {


for(int col = 0; col %26lt;n; ++col) {


if (array(row,col) %26lt; 1 ) || (array(row,col) %26gt; n )) {


++failure;


}








!UPDATE


Kat: Could be n*n. Can't figure if he wants to number from 1 to n in each row/col, or 1 to n^2, or what.





Couldn't figure if array(n*n) meant array[n*n] or array[n][n], I took it to be the later.





If you are right...





int i = 1, diff = 0, failures=0;


while(i%26lt;n*n) {


diff = array[i] - array[i-1];


failures+=(diff%26lt;0 || diff %26gt;1) ?:1:0;


++i;


}


No comments:

Post a Comment