int sumRange(int i, int j) { if (i > j | j >= nums.size()) { return 0; } return findSum(j) - findSum(i-1); }
void printMaxSumIS(int arr[], int n){ L[0].push_back(arr[0]); for (int i = 1; i < n; i++) { for (int j = 0; j < i; j++) { if ((arr[i] > arr[j]) && (findSum(L[i]) < findSum(L[j]))) L[i] = L[j]; } L[i].push_back(arr[i]); } int main(){ }
// rather than asking "does this node start a path with the sum", we ask "does this node complete a path with the sum?" void findSum(treeNode *node, int sum){ if(node == NULL){ return; } treeNode *n = node; int tmp = 0; for(int i=0; n != NULL; i++){ tmp += n->data; if(tmp == sum){ printPath(head, i); } n = n->parent; } findSum(node->left, sum); findSum(node->right, sum); }
int main() { int t,r,n,mod,ans,alfa,array; int a,d; long long int first,sum; //printf("%d\n",mulmod(223631,147020,8550886)); scanf("%d",&t); while(t--) { scanf("%d %d %d",&a,&d,&r); scanf("%d %d",&n,&mod); if(n == 1) { ans = a % mod; printf("%d\n",ans); continue; } alfa = (n-1)/2; array = findSum(r,alfa,mod);//r^alfa + ... + 1 sum = array; first = a%mod; first = (first*modular_pow(r,alfa,mod))%mod;//(a*r^alfa)%mod if(n%2 == 1) sum = (sum - 1%mod)%mod; sum = ((d%mod)*sum)%mod; sum = (first + sum)%mod; printf("%lld\n",sum); } return 0; }
int maxPathSum(TreeNode *root) { // Start typing your C/C++ solution below // DO NOT write int main() function //int max=INT_MIN; //findSum(root,0,max); //return max; return findSum(root,0,INT_MIN); }
string addBinary(string a, string b) { if(a.length() < b.length()) a.swap(b); findSum(a, b); return a; }
int findSum(TreeNode *r,int cur,int max){ int le,ri; int value; value=cur+r->val; if(value>max) max=value; if(value<0) value=0; if(r->left==NULL && r->right==NULL) return max; else if(r->left==NULL) return findSum(r->right,value,max); else if(r->right==NULL) return findSum(r->left,value,max); else{ le=findSum(r->left,value,max); ri=findSum(r->right,value,max); return le>ri?le:ri; } }
int main(int argc, char* argv[]) { int input[] = {-2,1,-3,4,-1,2,1,-5,4}; //int input[] = {-2}; int len = sizeof(input)/sizeof(int); int max = input[0]; int startIndex = 0, endIndex = 0; int i, j, k, subSeqSum; // brute force implementation for (i=0; i<len; i++) { for (j=i; j<len; j++) { subSeqSum = findSum(input, i,j); if (subSeqSum > max) { startIndex = i; endIndex = j; max = subSeqSum; } } } printf(" ---Largest sub sequence----- \n"); for (k=startIndex; k<= endIndex; k++) { printf("%d ", input[k]); } printf("\nSum = %d\n", max); // one pass algorithm max = input[0]; subSeqSum = 0; startIndex = endIndex = 0; int newStartIndex = 0; for (i=0; i<len; i++) { subSeqSum = subSeqSum + input[i]; if (subSeqSum < input[i]) { subSeqSum = input[i]; newStartIndex = i; } if (subSeqSum > max) { max = subSeqSum; startIndex = newStartIndex; endIndex = i; } } printf(" ---Largest sub sequence----- \n"); for (k=startIndex; k<= endIndex; k++) { printf("%d ", input[k]); } printf("\nSum = %d\n", max); return 0; }
int findSum(int i) { if (i >= nums.size() | i < 0) return 0; try { return mymap.at(i); } catch (const std::out_of_range& oor) { int sum = nums[i] + findSum(i-1); mymap[i] = sum; return sum; } }
int _tmain(int argc, _TCHAR* argv[]) { int len; int *a; int i; printf("Enter length: "); scanf("%d", &len); printf("Enter elements: \n"); a = (int *) malloc(sizeof(int) * len); for (i = 0; i < len; i++) scanf("%d", &a[i]); printf("\n"); printf("Sum: %d", findSum(a, len)); getch(); return 0; }
void findSum(vector<int>& candidates, int target, vector<int> cur, set<vector<int>>&s, int index) { if (target == 0) { s.insert(cur); return; } if (index >= candidates.size()) return; for (int i = index; i < candidates.size(); i++) { if (candidates[i] <= target) { cur.push_back(candidates[i]); findSum(candidates, target - candidates[i], cur, s, i + 1); cur.pop_back(); } } }
vector<vector<int>> combinationSum2(vector<int>& candidates, int target) { vector<vector<int>> ans; vector<int> cur; set<vector<int>> s; if (candidates.empty()) return ans; sort(candidates.begin(), candidates.end()); findSum(candidates, target, cur, s, 0); for (auto it = s.begin(); it != s.end(); it++) { ans.push_back(*it); } return ans; }
void getCommand(double arr[], size_t size) { char buffer[100]; CONSOLE_SCREEN_BUFFER_INFO SBInfo; HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE); GetConsoleScreenBufferInfo(hConsole, &SBInfo); if(SBInfo.dwCursorPosition.Y > BOTBORDER - 3) { clearZone(0, 1, RIGHTBORDER - 1, BOTBORDER - 2, COLORDEF); setCursorPosition(0, 1); } printf(" >> "); fgets(buffer, 100,stdin); if(!strcmp(buffer, "help\n")) { if(SBInfo.dwCursorPosition.Y > BOTBORDER - 20) { clearZone(0, 1, RIGHTBORDER - 1, BOTBORDER - 2, COLORDEF); setCursorPosition(0, 1); } puts(HELPINFO); } else if(!strcmp(buffer, "nullify\n")) { nullifyArr(arr, size); printArr(arr, size); } else if(strstr(buffer, "change")) { unsigned int index; int status; double value; printArr(arr, size); status = sscanf(buffer, "change %u %lf", &index, &value); if(status==2 && index < size) { changeNumByInd(arr, size, index, value); printArrEl(arr, size, index); } else { setConsoleColor(COLORERROR); puts("Inappropriate format"); setConsoleColor(COLORDEF); } } else if(strstr(buffer, "random")) { double from, to; int status = sscanf(buffer, "random %lf %lf", &from, &to); if(status==2) { initRandArray(arr, size, from, to); printArr(arr, size); } else { setConsoleColor(COLORERROR); puts("Inappropriate format"); setConsoleColor(COLORDEF); } } else if(!strcmp(buffer, "reverse\n")) { int i; reverseArr(arr, size); clearZone(RIGHTBORDER, 0, LENGTH - 1, 0, COLORSELECT); setConsoleColor(COLORSELECT); setCursorPosition(RIGHTBORDER + 1, 0); printf("REVERSED array : "); printArr(arr, size); setCursorPosition(0, SBInfo.dwCursorPosition.Y + 1); puts("Press anything to reverse back..."); getch(); setConsoleColor(COLORSELECT); clearZone(RIGHTBORDER, 0, LENGTH - 1, 0, COLORWORKBACK); setCursorPosition(RIGHTBORDER + (LENGTH - (RIGHTBORDER))/3, 0); setConsoleColor(COLORWORKBACK); printf("Array : "); clearZone(0, SBInfo.dwCursorPosition.Y + 1,RIGHTBORDER - 1, SBInfo.dwCursorPosition.Y + 1, COLORDEF); setCursorPosition(0, SBInfo.dwCursorPosition.Y + 1); reverseArr(arr, size); printArr(arr, size); } else if(!strcmp(buffer, "sum\n")) { double sum = findSum(arr, size); clearZone(0, BOTBORDER + 3, LENGTH - 1, BOTBORDER + 3, COLORWORKCOM); setCursorPosition(LENGTH/4, BOTBORDER + 3); setConsoleColor(COLORWORKCOM); printf("The sum of the array is : %.2f", sum); setConsoleColor(COLORDEF); setCursorPosition(0, SBInfo.dwCursorPosition.Y + 1); } else if(!strcmp(buffer, "nnegative\n")) { int nnegative = numOfNegElements(arr, size); clearZone(0, BOTBORDER + 3, LENGTH - 1, BOTBORDER + 3, COLORWORKCOM); setCursorPosition(LENGTH/4, BOTBORDER + 3); setConsoleColor(COLORWORKCOM); printf("The number of negative elements of the array is : %i", nnegative); setConsoleColor(COLORDEF); setCursorPosition(0, SBInfo.dwCursorPosition.Y + 1); } else if(strstr(buffer, "shift")) { unsigned int nSH; int status = sscanf(buffer, "shift %u", &nSH); if(status) { shiftArrR(arr, size, nSH); printArr(arr, size); } else { setConsoleColor(COLORERROR); puts("Inappropriate format"); setConsoleColor(COLORDEF); } } else if(strstr(buffer, "cyclesh")) { unsigned int nSH; int status = sscanf(buffer, "cyclesh %u", &nSH); if(status) { cycleShiftArrR(arr, size, nSH); printArr(arr, size); } else { setConsoleColor(COLORERROR); puts("Inappropriate format"); setConsoleColor(COLORDEF); } } else if(strstr(buffer, "powall")) { double power; int status = sscanf(buffer, "powall %lf", &power); if(status) { powEachElement(arr, size, power); printArr(arr, size); } else { setConsoleColor(COLORERROR); puts("Inappropriate format"); setConsoleColor(COLORDEF); } } else if(!strcmp(buffer, "firstmin\n")) { int imin = getMinimumIndex(arr, size); printArr(arr, size); printArrEl(arr, size, imin); clearZone(0, BOTBORDER + 3, LENGTH - 1, BOTBORDER + 3, COLORWORKCOM); setCursorPosition(LENGTH/4, BOTBORDER + 3); setConsoleColor(COLORWORKCOM); printf("The index and value of first minimum element is : %i, %.2f", imin, arr[imin]); setConsoleColor(COLORDEF); setCursorPosition(0, SBInfo.dwCursorPosition.Y + 1); } else if(!strcmp(buffer, "swapmaxmin\n")) { int swap1, swap2; printArr(arr, size); swapLastMaxAndMin(arr, size, &swap1, &swap2); printArrEl(arr, size, swap1); printArrEl(arr, size, swap2); } else if(!strcmp(buffer, "exit\n")) { exit(EXIT_SUCCESS); } else { puts("Command not found. Check help"); } getCommand(arr, size); }
int findSum(TreeNode* root) { if (!root) return 0; // cout << root->val + findSum(root->left) + findSum(root->right) << endl; return root->val + findSum(root->left) + findSum(root->right); }
int findTilt(TreeNode* root) { if (!root) return 0; else return findTilt(root->left) + findTilt(root->right) + abs(findSum(root->left) - findSum(root->right)); }