NoMind
BAN USER- 1of 1 vote
AnswersFor a given Sum and N print all the combinations
For Example Sum = 16 and N=2
Then Answer :
16,0
15,1
14,2
13,3
12,4
11,5
10,6
9,7
8,8
7,9
6,10
5,11
4,12
3,13
2,14
1,15
0,16private void recursivePrint(int sum, int n, int data[], int len, int originalSum) { if (n == 0) { int sum1 = 0; for (int j = 0; j < len; j++) { sum1 += data[j]; } if (sum1 == originalSum) { for (int j = 0; j < len; j++) { System.out.print(data[j] + " "); } System.out.println(); } return; } for (int i = 0; i <=sum; i++) { // System.out.println(" len: "+len+" sum: "+sum+" i: " + i+" n:"+n) data[len] = i; recursivePrint(sum - i, n - 1, data, len + 1, originalSum); } } }
Need a better solution so that we can store previous results by using hashmaps
- NoMind in India for Azure| Report Duplicate | Flag | PURGE
Microsoft Software Developer - 1of 1 vote
AnswersSort an array of 0s, 1s and 2s
- NoMind in India
Given an array A[] consisting 0s, 1s and 2s, write a function that sorts A[]. The functions should put all 0s first, then all 1s and all 2s in last.
Example
Input = {0, 1, 1, 0, 1, 2, 1, 2, 0, 0, 0, 1};
Output = {0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 2}
The problem is similar to http://www.csse.monash.edu.au/~lloyd/tildeAlgDS/Sort/Flag/
But they have added 1 trick into the question, You cannot use high = n;
It means don't calculate the length of array.
Can anyone help me in writing the code for this problem ?| Report Duplicate | Flag | PURGE
Microsoft SDE-2 Algorithm
node * change(node *head){
if(head==NULL || head->next==NULL) return head;
node * current=head,prev,next,current1;
while(current->next !=NULL){
prev=current;
current=current->next;
}
prev->next=NULL;
current->next=head;
head=current;
current=head->next;
while(current->next !=NULL){
current1=current->next;
while(current1->next !=NULL){
prev= current1;
current1=current1->next;
}
prev->next= NULL;
next=current->next;
current->next=current1;
current1->next=next;
current=next;
}
return head;
}
Kadane's algorithm
Initialize:
max_so_far = 0
max_ending_here = 0
Loop for each element of the array
(a) max_ending_here = max_ending_here + a[i]
(b) if(max_ending_here < 0)
max_ending_here = 0
(c) if(max_so_far < max_ending_here)
max_so_far = max_ending_here
return max_so_far
My Output for N =3 and Sum = 16
- NoMind April 23, 20170 0 16
0 1 15
0 2 14
0 3 13
0 4 12
0 5 11
0 6 10
0 7 9
0 8 8
0 9 7
0 10 6
0 11 5
0 12 4
0 13 3
0 14 2
0 15 1
0 16 0
1 0 15
1 1 14
1 2 13
1 3 12
1 4 11
1 5 10
1 6 9
1 7 8
1 8 7
1 9 6
1 10 5
1 11 4
1 12 3
1 13 2
1 14 1
1 15 0
2 0 14
2 1 13
2 2 12
2 3 11
2 4 10
2 5 9
2 6 8
2 7 7
2 8 6
2 9 5
2 10 4
2 11 3
2 12 2
2 13 1
2 14 0
3 0 13
3 1 12
3 2 11
3 3 10
3 4 9
3 5 8
3 6 7
3 7 6
3 8 5
3 9 4
3 10 3
3 11 2
3 12 1
3 13 0
4 0 12
4 1 11
4 2 10
4 3 9
4 4 8
4 5 7
4 6 6
4 7 5
4 8 4
4 9 3
4 10 2
4 11 1
4 12 0
5 0 11
5 1 10
5 2 9
5 3 8
5 4 7
5 5 6
5 6 5
5 7 4
5 8 3
5 9 2
5 10 1
5 11 0
6 0 10
6 1 9
6 2 8
6 3 7
6 4 6
6 5 5
6 6 4
6 7 3
6 8 2
6 9 1
6 10 0
7 0 9
7 1 8
7 2 7
7 3 6
7 4 5
7 5 4
7 6 3
7 7 2
7 8 1
7 9 0
8 0 8
8 1 7
8 2 6
8 3 5
8 4 4
8 5 3
8 6 2
8 7 1
8 8 0
9 0 7
9 1 6
9 2 5
9 3 4
9 4 3
9 5 2
9 6 1
9 7 0
10 0 6
10 1 5
10 2 4
10 3 3
10 4 2
10 5 1
10 6 0
11 0 5
11 1 4
11 2 3
11 3 2
11 4 1
11 5 0
12 0 4
12 1 3
12 2 2
12 3 1
12 4 0
13 0 3
13 1 2
13 2 1
13 3 0
14 0 2
14 1 1
14 2 0
15 0 1
15 1 0
16 0 0