Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ML Project for identifying the suitable condition of a crop #104

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 78 additions & 0 deletions C++ Programs/FirstRepeatingElement.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// Problem Link
// https://practice.geeksforgeeks.org/problems/first-repeating-element4018/1/?category%5B%5D=Arrays&category%5B%5D=Arrays&difficulty%5B%5D=0&page=2&query=category%5B%5DArraysdifficulty%5B%5D0page2category%5B%5DArrays

//Solution

// Initial template for C++

#include <bits/stdc++.h>
using namespace std;

// } Driver Code Ends
// User function template in C++

class Solution {
public:
// Function to return the position of the first repeating element.
int firstRepeated(int arr[], int n) {
int max=n;int k=0;

//find the max to get two array of index max
for(int i=0;i<n;i++){
if(arr[i]>max){
max=arr[i];
}
}
//initialize two arrays with zero
int a[max+1]={};
int b[max+1]={};

//assign values to a and b

for(int i=0;i<n;i++){
if(a[arr[i]]){
b[arr[i]]=1;
k=1;
}
else{
a[arr[i]]=i+1;
}
}

if(k==0){

return -1;
}
else{
int min=max+1;
for(int i=0;i<max+1;i++){
if(a[i] && b[i] && min>a[i]){
min=a[i];
}
}
return min;
}
}
};

// { Driver Code Starts.
int main() {

int t;
// } Driver Code Ends
cin >> t;

while (t--) {
int n;
cin >> n;

int arr[n];

for (int i = 0; i < n; ++i) cin >> arr[i];
Solution ob;
cout << ob.firstRepeated(arr, n) << "\n";
}

return 0;
}

Loading