0% found this document useful (0 votes)
2 views

CODEVITA C PROBLEMS SOLUTION

The document outlines the CODEVITA 2023 problems for the C programming language, specifically for non-circuit branches, detailing various problem statements along with their corresponding concepts and faculty names. It includes specific problem examples, such as the Consecutive Sum Problem, and provides code solutions for calculating sums of consecutive integers. Additionally, it features a flowchart and sample input/output for clarity.

Uploaded by

Harish T
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

CODEVITA C PROBLEMS SOLUTION

The document outlines the CODEVITA 2023 problems for the C programming language, specifically for non-circuit branches, detailing various problem statements along with their corresponding concepts and faculty names. It includes specific problem examples, such as the Consecutive Sum Problem, and provides code solutions for calculating sums of consecutive integers. Additionally, it features a flowchart and sample input/output for clarity.

Uploaded by

Harish T
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

CODEVITA 2023 – C LANUGAUE FOR NON-CIRCUIT BRANCH

RNO PROBLEM STATEMENT CONCEPTS


FACULTY NAME
S1 PrimeConstruction Structures
Mr.S.Senthil Kumar
S2 WorkerSolution Operators
Ms.Bhavani Ramesh
S3 Jugs And CupsSolution Pointers
Mrs.Shunmuga Sundari
S4 kth largest factor of N Arrays & its types
ProblemSolution Dr.Kavitha

S5 Counting Rock Sample Functions parameters


ProblemSolution Ms.Rajeswari

S6 Staircase Problem Solution File Concepts


Mr.Kumaran
S7 Consecutive Sum Functions Declaration&
ProblemSolution Dr.Bhuvaneswari Recursion

S8 Collecting Candies Control


ProblemSolution Mr.Vinoth(MCT) Statements(Looping
statement)
S9 Bank Compare Control
ProblemSolution Mrs. S. Mahalakshmi Statements(Conditional
Statement)
S10 Divine Divisors String manipulations
ProblemSolution Mrs.M.Sindhu

S11 Minimize the sum Basic concepts(data


Ms.Akila
types & variables)
Problem 1: Consecutive Sum Problem Solution
Given a positive integer N, how many ways can we write it as a sum of
consecutive positive integers?
Example 1:
Input:
5

Output:
2

Explanation:
5 = 5 = 2 + 3
Example 2:
Input:
9

Output:
3

Explanation:
9 = 9 = 4 + 5 = 2 + 3 + 4
Example 3:
Input:
15

Output:
4

Explanation:
15 = 15 = 8 + 7 = 4 + 5 + 6 = 1 + 2 + 3 + 4 + 5
Note:1 <= N <= 10 ^ 9.
Thoughts:

1. If N = i * j, then we want to find i consecutive numbers with


average j For example, 15 = 3 * 5, then 4 + 5 + 6, 3 numbers with
average 5; and 15 = 5 * 3, then 1 + 2 + 3 + 4 + 5, 5 numbers with
average 3 But one constraint is that i must be odd, since if i is even k +
1, k + 2, ... k + i. The average = (ik + (1 + i)*i/2) / i = k + (1 + i) / 2,
cannot be an integer. (Original Post)

2. Simple Code, but obscure logic (Original Post)

Code 1
class Solution:
def consecutiveNumbersSum(self, N: int) -> int:
if N == 1:
return 1
res = 1
for i in range(2, int(N ** .5 + 1)):
if N % i == 0:
if i % 2 == 1: # If i is odd, then we can form a sum of
length i
res += 1
j = (N // i) # Check the corresponding N // i
if i != j and j % 2 == 1:
res += 1
if N % 2 == 1: # If N is odd(2k + 1). Then N = k + k + 1, not
included above
res += 1

return res
Code 2
class Solution {
public:
int consecutiveNumbersSum(int N) {
int res = 0;
for(int i=1; i*(i-1)<=2*N; i++) {
if((2*N+i-i*i)%(2*i)==0&&(2*N+i-i*i)/(2*i)>0) {
res++;
}
}
return res;
}
};

Problem 2 : Given an integer N, the task is to find the number of


ways to represent this number as a sum of 2 or more
consecutive natural numbers.
Examples:

Input: N = 15
Output: 3
Explanation:
15 can be represented as:

1. 1 + 2 + 3 + 4 + 5
2. 4 + 5 + 6
3. 7 + 8
Input: N = 10
Output: 1

Approach: The idea is to represent N as a sequence of length L+1 as:


N = a + (a+1) + (a+2) + .. + (a+L)
=> N = (L+1)*a + (L*(L+1))/2
=> a = (N- L*(L+1)/2)/(L+1)
We substitute the values of L starting from 1 till L*(L+1)/2 < N
If we get ‘a’ as a natural number then the solution should be counted.
Problem 3: Compute the sum of consecutive odd numbers from a given pair of
integers
#include <stdio.h>

int main() {

int x, y, i, total = 0; // Declare variables for user


input and calculations

printf("\nInput a pair of numbers (for example 10,2):");

printf("\nInput first number of the pair: ");

scanf("%d", &x); // Read the first number

printf("\nInput second number of the pair: ");

scanf("%d", &y); // Read the second number

if (x < y) {

return 0; // If x is less than y, exit the program

printf("\nList of odd numbers: ");

for (i = y; i <= x; i++) {

if ((i % 2) != 0) {

printf("%d\n", i); // Print odd numbers within the


range

total += i; // Add odd numbers to the total

printf("Sum=%d\n", total); // Print the total sum

return 0;

}
Output:
Input a pair of numbers (for example 10,2):
Input first number of the pair: 10

Input second number of the pair: 2

List of odd numbers:


3
5
7
9
Sum=24
Flowchart:

You might also like