There is an array [5,3,4,3,0,3,3] random, find the frequency of the most frequent/array length >0.5, if there is to the number of the subscript, if not return-1
var a = [5,3,4,6,3,3,3]
for (Var i=0,ci,r={};ci=a[i];i++) {
if (R[ci]) {
R[ci].idx.push (i);
r[ci].count++;
}else{
R[ci]={idx:[i],count:1}
}
}
var f = -1,l=a.length;
For (Var p in R) {
if (r[p].count/l>0.5) F=r[p].idx;
}
alert (f);
There is an array [5,0,-5,2,-4,5], a number of positive and negative counts, and the frequency of each number
var a = [5,3,4,6,-3,3,-3]
for (Var i=0,ci,r={};ci=a[i++];) {
if (R[math.abs (CI)]) {
R[math.abs (CI)]++
}else{
R[math.abs (CI)]=1;
}
}
var s =[];
For (Var p in R) {
S.push (R[p]);
}
alert (s);
There is a sequence [5,0,2,-6,4] his subsequence can have [5,0],[5,0,2][2,-6],[5,0,2,-6,4] but cannot be [5,2],[5,-6]
For the largest sequence and the maximum sequence of examples is [5,0,2]=7
var a = [ -6,0,2,5,4],max = 0;
for (Var i=0,l=a.length;i<l-1;i++) {
for (Var j=i;j<l;j++) {
for (Var tmp=0,k=i;k<=j;k++) {
TMP = tmp +a[k];
}
if (tmp>max) max = tmp;
}
}
Alert (max);
Winter reply
First question.
<script> function f (arr) {var arr=arr.slice (). Sort (function (a,b) {return b-a;}); var M=arr[math.floor (ARR.LENGTH/2)]; var count=0; for (Var i=0;i<arr.length;i++) if (arr[i]==m) count++; if (COUNT>ARR.LENGTH/2) return m; else return-1; Alert (f ([5,3,4,3,0,3,3])); </script>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]
The second question if you do not consider JS convenient object words ...
<script> function f (arr) {var arr=arr.slice (). Sort (function (a,b) {return Math.Abs (b)-math.abs (a);}); var r=[]; var current; var count; for (Var i=0;i<arr.length;i++) if (Math.Abs (arr[i)) ==current) count++; else {if (current!=undefined) R.push ([Current,count]); Current=math.abs (Arr[i]), count=1; } return R; Alert (f ([5,0,-5,2,-4,5]). Join ("\ n")); </script>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]
The third problem of DP solution
<script> function f (arr) {var sum=0,max=0; for (Var i=0;i<arr.length;i++) {Sum+=arr[i]; sum=sum>0?sum:0; if (Sum>max) max=sum; return to Max; Alert (f ([5,0,2,-6,4])); </script>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]
Third full negative bug
<script> function f (arr) {var sum=max=arr[0]; for (Var i=1;i<arr.length;i++) {sum=arr[i]>=0? sum>0?sum:0) +arr[i]:arr[i]; max=sum>max?sum:max; return to Max; Alert (f ([5,0,2,-6,4])); Alert (f ([ -5,-2,-4,-7,-1])); </script>
[ctrl+a All selected note: If you need to introduce external JS need to refresh to perform]