Posted on by Kalkicode
Code Mathematics

# Find the only repetitive number between 1 to n-1

To find the only repetitive number between 1 to n-1, where n is the total number of integers in the range, you can use the following approach:

1. Calculate the sum of all integers from 1 to n-1 using the formula `(n-1)*(n)/2`.

2. Traverse the given array of integers and calculate the sum of all the integers in the array.

3. The repetitive number will be the difference between the sum of all integers from 1 to n-1 and the sum of integers in the array.

## Program Solution

``````/*
C program for
Find the only repetitive number between 1 to n-1
*/
#include <stdio.h>

void findSingleRepetitive(int num[], int n)
{
int sum = 0;
// Sum of all array elements
for (int i = 0; i < n; ++i)
{
sum += num[i];
}
// Calculate single repeated element by using sum of all array element
// Minus sum of array element from 1 to n.
int result = sum - ((n *(n - 1)) / 2);
// Display calculated result
printf("\n %d", result);
}
int main(int argc, char const *argv[])
{
// array elements, which is every element in range
// of 1 to n and 1 elements are repetitive
int num[] = {
3 , 1 , 5 , 4 , 5 , 2
};
// Get the size of number of elements
int n = sizeof(num) / sizeof(num[0]);
// Test
findSingleRepetitive(num, n);
return 0;
}``````

#### Output

`` 5``
``````/*
Java program for
Find the only repetitive number between 1 to n-1
*/
public class Repetitive
{
public void findSingleRepetitive(int[] num, int n)
{
int sum = 0;
// Sum of all array elements
for (int i = 0; i < n; ++i)
{
sum += num[i];
}
// Calculate single repeated element by using sum of all array element
// Minus sum of array element from 1 to n.
int result = sum - ((n * (n - 1)) / 2);
// Display calculated result
System.out.print("\n " + result);
}
public static void main(String[] args)
{
// array elements, which is every element in range
// of 1 to n and 1 elements are repetitive
int[] num = {
3 , 1 , 5 , 4 , 5 , 2
};
// Get the size of number of elements
int n = num.length;
// Test
}
}``````

#### Output

`` 5``
``````// Include header file
#include <iostream>
using namespace std;
/*
C++ program for
Find the only repetitive number between 1 to n-1
*/
class Repetitive
{
public: void findSingleRepetitive(int num[], int n)
{
int sum = 0;
// Sum of all array elements
for (int i = 0; i < n; ++i)
{
sum += num[i];
}
// Calculate single repeated element by using sum of all array element
// Minus sum of array element from 1 to n.
int result = sum - ((n *(n - 1)) / 2);
// Display calculated result
cout << "\n " << result;
}
};
int main()
{
// array elements, which is every element in range
// of 1 to n and 1 elements are repetitive
int num[] = {
3 , 1 , 5 , 4 , 5 , 2
};
// Get the size of number of elements
int n = sizeof(num) / sizeof(num[0]);
// Test
return 0;
}``````

#### Output

`` 5``
``````// Include namespace system
using System;
/*
Csharp program for
Find the only repetitive number between 1 to n-1
*/
public class Repetitive
{
public void findSingleRepetitive(int[] num, int n)
{
int sum = 0;
// Sum of all array elements
for (int i = 0; i < n; ++i)
{
sum += num[i];
}
// Calculate single repeated element by using sum of all array element
// Minus sum of array element from 1 to n.
int result = sum - ((n * (n - 1)) / 2);
// Display calculated result
Console.Write("\n " + result);
}
public static void Main(String[] args)
{
// array elements, which is every element in range
// of 1 to n and 1 elements are repetitive
int[] num = {
3 , 1 , 5 , 4 , 5 , 2
};
// Get the size of number of elements
int n = num.Length;
// Test
}
}``````

#### Output

`` 5``
``````package main
import "fmt"
/*
Go program for
Find the only repetitive number between 1 to n-1
*/

func findSingleRepetitive(num[] int, n int) {
var sum int = 0
// Sum of all array elements
for i := 0 ; i < n ; i++ {
sum += num[i]
}
// Calculate single repeated element by using sum of all array element
// Minus sum of array element from 1 to n.
var result int = sum - ((n * (n - 1)) / 2)
// Display calculated result
fmt.Print("\n ", result)
}
func main() {

// array elements, which is every element in range
// of 1 to n and 1 elements are repetitive
var num = [] int {3 , 1 , 5 , 4 , 5 , 2}
// Get the size of number of elements
var n int = len(num)
// Test
findSingleRepetitive(num, n)
}``````

#### Output

`` 5``
``````<?php
/*
Php program for
Find the only repetitive number between 1 to n-1
*/
class Repetitive
{
public	function findSingleRepetitive(\$num, \$n)
{
\$sum = 0;
// Sum of all array elements
for (\$i = 0; \$i < \$n; ++\$i)
{
\$sum += \$num[\$i];
}
// Calculate single repeated element by using sum of all array element
// Minus sum of array element from 1 to n.
\$result = \$sum - ((int)((\$n * (\$n - 1)) / 2));
// Display calculated result
echo("\n ".\$result);
}
}

function main()
{
// array elements, which is every element in range
// of 1 to n and 1 elements are repetitive
\$num = array(3, 1, 5, 4, 5, 2);
// Get the size of number of elements
\$n = count(\$num);
// Test
}
main();``````

#### Output

`` 5``
``````/*
Node JS program for
Find the only repetitive number between 1 to n-1
*/
class Repetitive
{
findSingleRepetitive(num, n)
{
var sum = 0;
// Sum of all array elements
for (var i = 0; i < n; ++i)
{
sum += num[i];
}
// Calculate single repeated element by using sum of all array element
// Minus sum of array element from 1 to n.
var result = sum - (parseInt((n * (n - 1)) / 2));
// Display calculated result
process.stdout.write("\n " + result);
}
}

function main()
{
// array elements, which is every element in range
// of 1 to n and 1 elements are repetitive
var num = [3, 1, 5, 4, 5, 2];
// Get the size of number of elements
var n = num.length;
// Test
}
main();``````

#### Output

`` 5``
``````#    Python 3 program for
#    Find the only repetitive number between 1 to n-1
class Repetitive :
def findSingleRepetitive(self, num, n) :
sum = 0
i = 0
#  Sum of all list elements
while (i < n) :
sum += num[i]
i += 1

#  Calculate single repeated element by using sum of all list element
#  Minus sum of list element from 1 to n.
result = sum - (int((n * (n - 1)) / 2))
#  Display calculated result
print("\n ", result, end = "")

def main() :
#  list elements, which is every element in range
#  of 1 to n and 1 elements are repetitive
num = [3, 1, 5, 4, 5, 2]
#  Get the size of number of elements
n = len(num)
#  Test

if __name__ == "__main__": main()``````

#### Output

``  5``
``````#    Ruby program for
#    Find the only repetitive number between 1 to n-1
class Repetitive
def findSingleRepetitive(num, n)
sum = 0
i = 0
#  Sum of all array elements
while (i < n)
sum += num[i]
i += 1
end

#  Calculate single repeated element by using sum of all array element
#  Minus sum of array element from 1 to n.
result = sum - ((n * (n - 1)) / 2)
#  Display calculated result
print("\n ", result)
end

end

def main()
#  array elements, which is every element in range
#  of 1 to n and 1 elements are repetitive
num = [3, 1, 5, 4, 5, 2]
#  Get the size of number of elements
n = num.length
#  Test
end

main()``````

#### Output

`` 5``
``````/*
Scala program for
Find the only repetitive number between 1 to n-1
*/
class Repetitive()
{
def findSingleRepetitive(num: Array[Int], n: Int): Unit = {
var sum: Int = 0;
var i: Int = 0;
// Sum of all array elements
while (i < n)
{
sum += num(i);
i += 1;
}
// Calculate single repeated element by using sum of all array element
// Minus sum of array element from 1 to n.
var result: Int = sum - ((n * (n - 1)) / 2);
// Display calculated result
print("\n " + result);
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: Repetitive = new Repetitive();
// array elements, which is every element in range
// of 1 to n and 1 elements are repetitive
var num: Array[Int] = Array(3, 1, 5, 4, 5, 2);
// Get the size of number of elements
var n: Int = num.length;
// Test
}
}``````

#### Output

`` 5``
``````import Foundation;
/*
Swift 4 program for
Find the only repetitive number between 1 to n-1
*/
class Repetitive
{
func findSingleRepetitive(_ num: [Int], _ n: Int)
{
var sum: Int = 0;
var i: Int = 0;
// Sum of all array elements
while (i < n)
{
sum += num[i];
i += 1;
}
// Calculate single repeated element by using sum of all array element
// Minus sum of array element from 1 to n.
let result: Int = sum - ((n * (n - 1)) / 2);
// Display calculated result
print("\n ", result, terminator: "");
}
}
func main()
{
// array elements, which is every element in range
// of 1 to n and 1 elements are repetitive
let num: [Int] = [3, 1, 5, 4, 5, 2];
// Get the size of number of elements
let n: Int = num.count;
// Test
}
main();``````

#### Output

``  5``
``````/*
Kotlin program for
Find the only repetitive number between 1 to n-1
*/
class Repetitive
{
fun findSingleRepetitive(num: Array < Int > , n: Int): Unit
{
var sum: Int = 0;
var i: Int = 0;
// Sum of all array elements
while (i < n)
{
sum += num[i];
i += 1;
}
// Calculate single repeated element by using sum of all array element
// Minus sum of array element from 1 to n.
val result: Int = sum - ((n * (n - 1)) / 2);
// Display calculated result
print("\n " + result);
}
}
fun main(args: Array < String > ): Unit
{
// array elements, which is every element in range
// of 1 to n and 1 elements are repetitive
val num: Array < Int > = arrayOf(3, 1, 5, 4, 5, 2);
// Get the size of number of elements
val n: Int = num.count();
// Test
}``````

#### Output

`` 5``

## Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

Categories
Relative Post